JonPSmith / AuthPermissions.AspNetCore

This library provides extra authorization and multi-tenant features to an ASP.NET Core application.

Home Page:https://www.thereformedprogrammer.net/finally-a-library-that-improves-role-authorization-in-asp-net-core/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error localization / translation feature request

idan-h opened this issue · comments

commented

Hey, I would like to help refactor the errors in a way that will enable easy translation.
We can return error code with parmeters instead of string, or use microsoft localization methods.

Hi @idan-h,

I already have a library called EfCore.GenericServices.AspNetCore that can convert the errors in a IStatusGeneric return into the ASP.NET Core Model or a ASP.NET Core Web API - see this article on how it works.

Also note that the GenericServices.StatusGeneric library, which the IStatusGeneric comes from, holds errors in the ValidationResult class, which can contain the parameter(s) linked to the error text.

In AuthP I use the IStatusGeneric on nearly every method, and for any error that is linked to a parameter I set the MemberNames parameter in the ValidationResult class . For instance, look for the code snippet status.AddError( in the AuthP code, e.g. look at AuthRolesAdminService and you will see me setting the parameter linked to various errors.

What I haven't done is to used the EfCore.GenericServices.AspNetCore library the AuthP examples. There is so much code in the repo I take the easy way out and just list all the error messages.

So, the "return error code with parameters instead of string" is already been handled. I would be interested of how you would "enable easy translation" because I thought that would be done in the ASP.NET Core part, not inside the AuthP library.

commented

Cool, I will check it out.

You are right, it can be done on the ASP.NET Core side, but in my opinion it is best to keep the translation closest to the source.

So we can make translation easier by concentrating all the error strings in a single place, so if someone wants to translate to a new language all he needs is to know both languages.

We can do that by creating some static class which will contain all the messages, or by already implementing the localization the microsoft way inside the library.

Hi @idan-h,

I tried the static class for translation on a project but I didn't like it at all, especially when the error message had dynamic parts. So, no I don't want that added to the AuthP library.

If you really want to do something, then creating an example of using the AuthP library with a Blazor frontend is people are asking for. @akema-trebla was looking at this (see issue #38), but think @akema-trebla is too busy to work on it.

I suggested that a Web API backend + Blazor WASM frontend version of the Example3's multi-tenant project would be great, but its not trivial and its a LOT of work.

commented

I might do that in the future as I am working on blazor wasm project.

I didn't see any implementation of status code of some kind.. Maybe I'm missing something?

Maybe we can add an optional parmeter to the error objects in IStatusGeneric which accepts an Enum or an Integer, which will contain the error code. This way translation will still be easier than looking at the code to find every possible error string and then translating it.

Example:

        public new IStatusGeneric<T> AddError<TEnum>(string errorMessage, TEnum errorCode,  params string[] propertyNames) 
         { 
             if (errorMessage == null) throw new ArgumentNullException(nameof(errorMessage)); 
             _errors.Add(new ErrorGeneric(Header, new ValidationResult(errorMessage, propertyNames, (int)errorCode))); 
             return this; 
         }

I might do that in the future as I am working on blazor wasm project.

That would be great!

... use microsoft localization methods.

You have a point, but its not trivial to create a easy-to-use localization . I'm busy at the moment but I will look at this later.

Hi @JonPSmith

Pardon my tardiness. Yes, I've been quite busy and not been able to deliver as quickly as I'd like. I however have the API implemented and should probably push that.

I made an adjustment to the way we add query filters though, to allow for multiple filters on an entity. Probably should make this a separate issue.

@idan-h how far have you gone with the Blazor sample? Maybe you could pickup from where I left off?

commented

Hi @JonPSmith

Pardon my tardiness. Yes, I've been quite busy and not been able to deliver as quickly as I'd like. I however have the API implemented and should probably push that.

I made an adjustment to the way we add query filters though, to allow for multiple filters on an entity. Probably should make this a separate issue.

@idan-h how far have you gone with the Blazor sample? Maybe you could pickup from where I left off?

I didn't start yet, where did you left off?

commented

I might do that in the future as I am working on blazor wasm project.

That would be great!

... use microsoft localization methods.

You have a point, but its not trivial to create a easy-to-use localization . I'm busy at the moment but I will look at this later.

Its not very complicated tbh, pretty straight forward with IStringLocalizer and resx files

Hi @akema-trebla,

Glad to hear from you. No problem on taking some time on this as no one is getting paid for this. It would be nice if you could add a comment on the original #38 issue on your progress. Also, if you have questions, then use the #38 issue and not this issue.

I made an adjustment to the way we add query filters though, to allow for multiple filters on an entity. Probably should make this a separate issue.

If the multiple filters feature is only in your Web API backend project, then that's fine but it mustn't change any of the AuthP library. I work hard to not to change a feature used in previous versions as that effects people who started on a older version. Also keeping the documentation takes a LOT of time.

Hi @idan-h,

Its not very complicated tbh, pretty straight forward with IStringLocalizer and resx files

I said "its not trivial to create a easy-to-use localization".

commented

easy-to-use

How do you define easy to use?

Do you like the resx solution combined with dependency injection (IStringLocalizer)?

Here is Microsoft documentation:
https://learn.microsoft.com/en-us/dotnet/core/extensions/localization

Version 4.1.0 contains localization service - see this document for an overview.

You might like the feedback on whether adding locational to the AuthP was needed by users - click this to see the Twitter vote on AuthP localization.

commented

You might like the feedback on whether adding locational to the AuthP was needed by users - click this to see the Twitter vote on AuthP localization.

This is great Jon, thank you! I'm looking forward using it, very much needed.