Askmethat-Aspnet-JsonLocalizer:用于.NetStandard和.NetCore Asp.net项目

  • S5_161424
  • 317.4KB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-04-09 09:38
Askmethat-Aspnet-JsonLocalizer 用于.NetStandard和.NetCore Asp.net项目的Json Localizer库 努吉特 建立 项目 该库允许用户在ASP.NET应用程序中使用JSON文件而不是RESX。 该代码尝试最符合Microsoft准则。 该库与NetStandard&NetCore兼容。 组态 扩展方法可用于IServiceCollection 。 你可以在看看方法 选件 提供了一组选项。 您可以这样定义它们: services . AddJsonLocalization ( options => { options
# Askmethat-Aspnet-JsonLocalizer Json Localizer library for .NetStandard and .NetCore projects #### Nuget [![NuGet](]( #### Build [![Build status](]( # 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 : ``` cs 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") }; }); ``` ### Current Options - **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. - **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* ### 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 paramters from [IStringLocalizer](, if last parameters is a boolean, pluralization will be activated. Pluralization is available with IStringLocalizer, IViewLocalizer and HtmlStringLocalizer : **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. **Example** ``` cs public class HomeController{ private readonly IJsonStringLocalizer _localizer; public HomeController(IJsonStringLocalizer<HomeController> localizer) { _localizer = localizer; _localizer.ClearMemCache(new List<CultureInfo>() { new CultureInfo("en-US") }); } } ``` # Information **Platform Support** |Platform|Version| | ------------------- | :------------------: | |NetCore|3.0.0+| |NetStandard|2.1.0+| |Blazor Server|3.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 core. For more information : # Localization mode 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 : ``` 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 : # Performances After talking with others Devs about my package, they asked my about performance. ``` ini BenchmarkDotNet=v0.12.0, OS=macOS 10.15.4 (19E287) [Darwin 19.4.0] Intel Core i7-5557U CPU 3.10GHz (Broadwell), 1 CPU, 4 logical and 2 physical cores .NET Core SDK=3.1.101 [Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT DefaultJob : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT ``` | Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated | |------------------------------------------------ |--------------:|-------------:|-------------:|--------------:|--------------:|---------:|--------:|--------:|--------:|-------:|----------:| | Localizer | 108.02 ns | 0.240 ns | 0.187 ns | 107.74 ns | 108.44 ns | 1.00 | 0.00 | - | - | - | - | | JsonLocalizer | 72.37 ns | 0.422 ns | 0.
    • json
      使用NodeJS和Microsoft Bot Framework的#codepunk实验机器人 该机器人解析了codepunk.io网站的RSS feed,然后将回答以下问题: “什么是新的?” “最近的博客文章是什么?” “最后一个播客是什么?” 它以链接,...
    • json
    • Newtonsoft_Json
      使用方法在我博客里有说 Json的序列和反序列化
    • JSONClass代码
    • JSON序列化
    • CSharpJsonPatchGenerator:建立在Microsoft.AspNetCore.JsonPatch上,通过比较
      基于Microsoft.AspNetCore.JsonPatch构建,以通过比较或观察对C#对象的更改来生成JSON补丁文档。 通过比较两个对象来生成JSON补丁 观察对象实例的更改并生成代表这些更改的JSON补丁 安装 从程序包管理器控制台: ...
    • CoreHal.Json.Microsoft:扩展CoreHal以使用Microsoft的System.Text.Json库创建
      微软微软 扩展CoreHal以使用Microsoft的System.Text.Json库创建Json
    • SkypeChatExtractor:Microsoft提供的Json聊天记录的提取器
      SkypeChatExtractor Microsoft提供的Json聊天提取器
    • jsonc:带有注释的JSON
      Microsoft VS Code在其配置文件(例如settings.json , keybindings.json , launch.json等)中也使用此格式。 该套餐提供什么 JSONC for Go提供了将JSONC转换和解组为纯JSON的功能。 它还提供了从磁盘读取JSONC...
    • Json Read tool
      C#读取json格式,并写成提取csv文件, 支持的格式如下: { "type": "String", "compoperator": "LOG", "expectedvalue": "", "value": "3", "name": "L002", "description": "Log Hardware ID", "result": ...