-
Notifications
You must be signed in to change notification settings - Fork 161
Supporting multiple languages
The 4.1.0 version (or higher) AuthP library has a built-in localization system using my Net.LocalizeMessagesAndErrors library. By default it is turned off so you can use the AuthP library without having to set up the .NET localization service and resource files. To turn on localization in your code that uses the AuthP library you need to:
- Register the .NET localization service in your ASP.NET Core.
-
Register the AuthP localization service in your
Program
class. - Create resource file(s) for the AuthP messages for other languages.
- Add localization to your own code.
I have covered this in my Net.LocalizeMessagesAndErrors documentation which links to apps that have .NET localization set up, so please use that to set up the .NET localization service.
You can also look at the Example1's Program
class, which has been setup such that the user can select what culture they want.
You add the SetupAuthPLocalization<TResource>
extension method within the registering of the AuthP library. The code below, taken from the Program
class in Example1, that sets up the AuthP localization of its backend code.
builder.Services.RegisterAuthPermissions<Example1Permissions>()
.SetupAuthPLocalization<AuthPLocalizeResource>(supportedCultures)
// other AuthP's registering left out
The AuthPLocalizeResource
class in the Example1 ASP.NET Core's project is there to define part of the resource file(s) name that will hold the AuthP messages in the extra languages you want to support (see next section about this). You can have a class with a different or location, but it must be in the ASP.NET Core's project.
The supportedCulture
parameter should be set to an array of culture names your app supports, e.g new[] { "en", "fr" }. This list can usually come from your code to register the .NET localization service.
NOTE: See a more detailed list of the possible supportedCulture
settings in the Startup code -> SetupAuthPLocalization section.
The AuthP library has >100 messages (mostly error messages) which you need changed into the non-English languages. For this you need a resource file for every language, other than English.
To help you to set up each resource file there is a file called AuthPermissions - list of localizable keys & messages - english.csv which contains the Name
and Value
of each AuthP possible messages, but the Value
is in English. You have to translate the English Value
to the language(s) you want to support.
This is a tedious job, but having the the complete set of Name
/ Value
will make the process easier, and you can be sure you have covered all the messages.
NOTE: in the article about the Net.LocalizeMessagesAndErrors library I give a tip on how to build resource files by using Excel to input the Name / Value entries and then use code to create the resource file.
There are three steps you need to go though to localize the code in your application, as listed below:
- a. Setting up resource file(s) for your code / frontend.
- b. Localize your code / front-end.
- c. How to handle the messages from the AuthP services.
The following sub-sections with cover these parts:
In step 3 you added a resource file for the AuthP messages. Now you need to create resource file(s) for your code and front-end. The process is the same as step three, but the Names / Values are defined by you.
In the Example1 web app I added a class called AppLocalizeResource
which defined the name of the resource file. I was using the Net.LocalizeMessagesAndErrors library so I only needed a French resource file, which had the name AppLocalizeResource.fr.resx.
In your own code you can localize your code in any way you want to - using the .NET localization services, or my Net.LocalizeMessagesAndErrors library, or both.
If you want to use .NET localization services then try the Microsoft docs. NOTE: There are links to other information at the end of this section one of my articles.
If you want to use Net.LocalizeMessagesAndErrors library, then look at its documentation. But be aware, in the AuthP library the default culture of the messages is "en" (generic English), so if English isn't the main culture, then you shouldn't use the Net.LocalizeMessagesAndErrors library.
I used the Net.LocalizeMessagesAndErrors's ISimpleLocalizer
service, which is targetted at simple messages used in front-end code, in the Example1 web app - here is a link to the Create Role razor code where I used the ISimpleLocalizer
service. NOTE: you have to manually register the ISimpleLocalizer
- see this document for how you do that.
I didn't used the IDefaultLocalizer
service (which is registered by AuthP) because the Example1 web app didn't have any backend code, but there is backend code you might use it. The IDefaultLocalizer
has a more localize key settings
Any AuthP service that can return errors returns a status, which contains a list of errors (empty if no errors) and a Message
, which will contain a success message, if there are no errors. It also has a IsValid
and HasErrors
bool properties that are true and false respectively if there are no errors. See the IStatusGeneric
interface for full information.
Version 4.1.0 has a localized the success and error messages returned, so you can show these to the user. In the Example1 web app I also added the NuGet called EfCore.GenericServices.AspNetCore
which can copy the IStatusGeneric
errors into the ASP.NET.Core's ModelState
, which will display the error in the correct place - see the Create Role Page code on line 36.
Click this to see the English / French screenshot with the error shown next to the Role Name.
- Intro to multi-tenants (ASP.NET video)
- Articles in date order:
- 0. Improved Roles/Permissions
- 1. Setting up the database
- 2. Admin: adding users and tenants
- 3. Versioning your app
- 4. Hierarchical multi-tenant
- 5. Advanced technique with claims
- 6. Sharding multi-tenant setup
- 7. Three ways to add new users
- 8. The design of the sharding data
- 9. Down for maintenance article
- 10: Three ways to refresh claims
- 11. Features of Multilingual service
- 12. Custom databases - Part1
- Videos (old)
- Authentication explained
- Permissions explained
- Roles explained
- AuthUser explained
- Multi tenant explained
- Sharding explained
- How AuthP handles sharding
- How AuthP handles errors
- Languages & cultures explained
- JWT Token refresh explained
- Setup Permissions
- Setup Authentication
- Startup code
- Setup the custom database feature
- JWT Token configuration
- Multi tenant configuration
- Using Permissions
- Using JWT Tokens
- Creating a multi-tenant app
- Supporting multiple languages
- Unit Test your AuthP app