Alomso.NetCore.JsonLocalizer 2.2.3

dotnet add package Alomso.NetCore.JsonLocalizer --version 2.2.3                
NuGet\Install-Package Alomso.NetCore.JsonLocalizer -Version 2.2.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Alomso.NetCore.JsonLocalizer" Version="2.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Alomso.NetCore.JsonLocalizer --version 2.2.3                
#r "nuget: Alomso.NetCore.JsonLocalizer, 2.2.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Alomso.NetCore.JsonLocalizer as a Cake Addin
#addin nuget:?package=Alomso.NetCore.JsonLocalizer&version=2.2.3

// Install Alomso.NetCore.JsonLocalizer as a Cake Tool
#tool nuget:?package=Alomso.NetCore.JsonLocalizer&version=2.2.3                

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.

Options

A set of options is available. You can define them like this :

    CultureInfo[] supportedCultures = new[]
        {
                new CultureInfo("en"),
                new CultureInfo("vi")
        };

    _ = services.AddJsonLocalization(options =>
    {
        options.ResourcesPath = "Resources";
        options.UseBaseName = true;
        options.CacheDuration = TimeSpan.FromDays(15);
        options.SupportedCultureInfos = supportedCultures.ToHashSet();
    });

    _ = services.Configure<RequestLocalizationOptions>(options =>
    {

        options.DefaultRequestCulture = new RequestCulture("en");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
    });

Current Options

  • DynamicKey : Support dynamic key.
  • FormatWithParameters : Format with parameters.
  • 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.
  • 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.

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

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.

Example

public enum DynamicEnum
{
    App = 0,
    Hello = 1,
    Format = 2,
    Language = 3
}

public class HomeController{
  private readonly IJsonStringLocalizer _localizer;
  public HomeController(IJsonStringLocalizer<HomeController> localizer)
  {
      _localizer = localizer;
      _localizer.ClearMemCache(new List<CultureInfo>()
      {
          new CultureInfo("en-US")
      });
  }
  public IActionResult Index()
  {
      ViewData["Hello"] = _localizer[DynamicEnum.Hello] + " " + _localizer[DynamicEnum.Format, new { app = _localizer[DynamicEnum.App], lang = _localizer[DynamicEnum.Language] }];
      return View();
  }
}
//Resources/Controllers/HomeController/HomeController.vi.json
{
  "Hello": "Xin chào",
  "App": "Ứng dụng Demo",
  "Language": "Tiếng việt",
  "Save": "Lưu",
  "Format": "Xin chào {app} - ngôn ngữ {lang}"
}
{
     "AUTH_LOGIN_FAIL": "Đăng nhập thất bại!",
     "AUTH_NOT_ALLOWED": "Tài khoản {username} không được phép truy cập!",
}

// L = localizer;

Message = L[ErrorCode.AUTH_LOGIN_FAIL] 
    => Đăng nhập thất bại!
Message = L[ErrorCode.AUTH_NOT_ALLOWED, new {username = "Tuyn Doan"}] 
    => Tài khoản Tuyn Doan không được phép truy cập!
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.2.3 722 12/22/2019