Json Localizer library for .NetStandard and .NetCore Asp.net projects
Nuget
Build
Project
This library allows users to use JSON files instead of RESX in an ASP.NET application.
The code tries to be most compliant with Microsoft guidelines.
The library is compatible with NetStandard & NetCore.
Configuration
An extension method is available for IServiceCollection.
You can have a look at the method here
Options
A set of options is available.
You can define them like this :
SupportedCultureInfos : _Default value : List containing only default culture and CurrentUICulture. Optionnal array of cultures that you should provide to plugin. _(Like RequestLocalizationOptions)
ResourcesPath : Default value : $"{_env.WebRootPath}/Resources/". Base path of your resources. The plugin will browse the folder and sub-folders and load all present JSON files.
AdditionalResourcePaths : Default value : null. Optionnal array of additional paths to search for resources.
CacheDuration : Default value : 30 minutes. We cache all values to memory to avoid loading files for each request, this parameter defines the time after which the cache is refreshed.
FileEncoding : default value : UTF8. Specify the file encoding.
IsAbsolutePath : _default value : false. Look for an absolute path instead of project path.
UseBaseName : _default value : false. Use base name location for Views and constructors like default Resx localization in ResourcePathFolder. Please have a look at the documentation below to see the different possiblities for structuring your translation files.
Caching : _default value: MemoryCache. Internal caching can be overwritted by using custom class that extends IMemoryCache.
PluralSeparator : _default value: |. Seperator used to get singular or pluralized version of localization. More information in Pluralization
MissingTranslationLogBehavior : _default value: LogConsoleError. Define the logging mode
LocalizationMode : _default value: Basic. Define the localization mode for the Json file. Currently Basic and I18n. More information in LocalizationMode
MissingTranslationsOutputFile : This enables to specify in which file the missing translations will be written when MissingTranslationLogBehavior = MissingTranslationLogBehavior.CollectToJSON, defaults to MissingTranslations.json
IgnoreJsonErrors: This properly will ignore the JSON errors if set to true. Recommended in production but not in development.
Search patterns when UseBaseName = true
If UseBaseName is set to true, it will be searched for lingualization files by the following order - skipping the options below if any option before matches.
If you use a non-typed IStringLocalizer all files in the Resources-directory, including all subdirectories, will be used to find a localization. This can cause unpredictable behavior if the same key is used in multiple files.
If you use a typed localizer, the following applies - Namespace is the "short namespace" without the root namespace:
Nested classes will use the translation file of their parent class.
If there is a folder named "Your/Namespace/And/Classname", all contents of this folder will be used.
If there is a folder named "Your/Namespace" the folder will be searched for all json-files beginning with your classname.
Otherwise there will be searched for a json-file starting with "Your.Namespace.And.Classname" in your Resources-folder.
If there any .shared.json file at base path, all the keys that do not exist in other files will be added.
If you need a base shared files, just add a file named localization.shared.json in your ResourcesPath
Pluralization
In version 2.0.0, Pluralization was introduced.
You are now able to manage a singular (left) and plural (right) version for the same Key.
PluralSeparator is used as separator between the two strings.
For example : User|Users for key Users
To use plural string, use parameters from IStringLocalizer, if last parameters is a boolean, pluralization will be activated.
Pluralization is available with IStringLocalizer, IViewLocalizer and HtmlStringLocalizer :
In version 3.1.1 and above you can have multiple pluralization, to use it, you should
use IJsonStringLocalizer interface and this method LocalizedString GetPlural(string key, double count, params object[] arguments)
localizer.GetString("Users", true);
Clean Memory Cache
Version 2.2.0+ allows you to clean cache.
It's usefull when you want's tu update in live some translations.
As you know, Blazor Server does not provide IHtmlLocalizer. To avoid this, you can now use
from IJsonStringLocalizer this method MarkupString GetHtmlBlazorString(string name, bool shouldTryDefaultCulture = true)
Information
Platform Support
Version 3.2 and bellow
Platform
Version
NetCore
3.0.0+
NetStandard
2.1.0+
Blazor Server
3.0.0+
Version 4 and above
Platform
Version
NetCore
5.0.0+
NetStandard
2.1.0+
Blazor Server
5.0.0+
Blazor Wasm
5.0.0+
WithCulture method
WhithCulture method is not implemented and will not be implemented. ASP.NET Team, start to set this method Obsolete for version 3 and will be removed in version 4 of asp.net core.
As asked on the request #64, Some user want to have the possiblities to manage file with i18n way.
To answer this demand, a localization mode was introduced with default value Basic. Basic version means the the one describe in the previous parts
I18n
To use the i18n file management, use the the option Localization mode like this : cs LocalizationMode = LocalizationMode.I18n.
After that, you should be able to use this json :
{
"Name": "Name",
"Color": "Color"
}
File name
File name are important for some purpose (Culture looking, parent culture, fallback).
Please use this pattern : [fileName].[culture].json
If you need a fallback culture that target all culture, you can create a file named localisation.json. Of course, if this file does not exist, the chosen default culture is the fallback.
Important: In this mode, the UseBaseName options should be False.
A special thanks to @Compufreak345 for is hard work. He did a lot for this repo.
A special thanks to @EricApption for is work to improve the repo and making a very good stuff on migrating to net6 and System.Text.Json & making it working for blazor wasm
请发表评论