Json Localizer library for .NetCore Asp.net projects
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 NetCore.
An extension method is available for IServiceCollection
.
You can have a look at the method here
A set of options is available. You can define them like this :
services.AddJsonLocalization(options => {
options.CacheDuration = TimeSpan.FromMinutes(15);
options.ResourcesPath = "mypath";
options.FileEncoding = Encoding.GetEncoding("ISO-8859-1");
options.SupportedCultureInfos = new HashSet<CultureInfo>()
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR")
};
});
$"{_env.WebRootPath}/Resources/"
_. Base path of your resources. The plugin will browse the folder and sub-folders and load all present JSON files.MissingTranslationLogBehavior = MissingTranslationLogBehavior.CollectToJSON
, defaults to MissingTranslations-<locale>.json
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:
If you need a base shared files, just add a file named localization.shared.json in your ResourcesPath
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);
Version 2.2.0+ allows you to clean cache. It's usefull when you want's tu update in live some translations.
Example
public class HomeController{
private readonly IJsonStringLocalizer _localizer;
public HomeController(IJsonStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
_localizer.ClearMemCache(new List<CultureInfo>()
{
new CultureInfo("en-US")
});
}
}
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)
Platform Support
Platform | Version |
---|---|
NetCore | 7.0.0+ |
Blazor Server | 7.0.0+ |
Blazor Wasm | 7.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.
For more information : https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/issues/46
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
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.
For more information : https://github.com/AlexTeixeira/Askmethat-Aspnet-JsonLocalizer/issues/64
Because of the way Blazor Wasm works, the plugin will not be able to load files from the server. To avoid this, you should embed your files in the project and use the following code :
<EmbeddedResource Include="Resources\localization.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
The second specificities is the management of the language files. Blazor Wasm uses the file path as Assembly name, so you can't have multiple files with the same name.
For example, if you have a file named localization.json in the folder Resources, you can't have another file starting with name localization in the folder Resources, a file with the name localization.fr.json will throw an exception.
So you should have different folder for each language culture.
After talking with others Devs about my package, they asked my about performance.
BenchmarkDotNet v0.14.0, EndeavourOS
AMD Ryzen 9 7950X3D, 1 CPU, 32 logical and 16 physical cores
.NET SDK 9.0.100
[Host] : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
DefaultJob : .NET 9.0.0 (9.0.24.52809), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen0 | Gen1 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|---|---|---|---|---|
Localizer | 32.80 ns | 0.119 ns | 0.111 ns | 32.63 ns | 32.98 ns | 1.00 | 0.00 | - | - | - | NA |
JsonLocalizer | 14.69 ns | 0.161 ns | 0.150 ns | 14.47 ns | 14.99 ns | 0.45 | 0.00 | 0.0010 | - | 48 B | NA |
JsonLocalizerWithCreation | 45,616.81 ns | 869.905 ns | 966.897 ns | 44,252.47 ns | 47,202.03 ns | 1,390.89 | 29.11 | 0.6104 | 0.4883 | 31008 B | NA |
I18nJsonLocalizerWithCreation | 64,513.78 ns | 1,275.916 ns | 2,548.143 ns | 61,644.40 ns | 71,427.74 ns | 1,967.07 | 77.22 | 5.1270 | 4.8828 | 88483 B | NA |
JsonLocalizerWithCreationAndExternalMemoryCache | 4,186.98 ns | 59.294 ns | 55.463 ns | 4,067.49 ns | 4,269.49 ns | 127.66 | 1.69 | 0.1144 | 0.1068 | 5824 B | NA |
JsonLocalizerDefaultCultureValue | 84.08 ns | 0.875 ns | 0.776 ns | 82.64 ns | 85.64 ns | 2.56 | 0.02 | 0.0052 | - | 264 B | NA |
LocalizerDefaultCultureValue | 105.14 ns | 1.347 ns | 1.260 ns | 102.46 ns | 107.33 ns | 3.21 | 0.04 | 0.0129 | - | 216 B | NA |
Michael Monsour |
Luka Gospodnetic |
Christoph Sonntag |
Nacho |
Ashley Medway |
Serhii Voitovych |
James Hill |
Ferenc Czirok |
rohanreddyg |
rickszyr |
ErikApption |
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