项目作者: WeihanLi

项目描述 :
dotnetcore json based localization support
高级语言: C#
项目地址: git://github.com/WeihanLi/WeihanLi.Extensions.Localization.Json.git
创建时间: 2020-01-09T11:48:09Z
项目社区:https://github.com/WeihanLi/WeihanLi.Extensions.Localization.Json

开源协议:MIT License

下载


WeihanLi.Extensions.Localization.Json WeihanLi.Extensions.Localization.Json

Intro

dotnet JSON file based localization

Build

AzureDevOps Build Status

Github Build Status

GetStarted

register required services:

  1. services.AddJsonLocalization(options =>
  2. {
  3. options.ResourcesPath = Configuration.GetAppSetting("ResourcesPath");
  4. options.ResourcesPathType = ResourcesPathType.TypeBased; // by default, looking for resourceFile like Microsoft do
  5. // options.ResourcesPathType = ResourcesPathType.CultureBased; // looking for resource file in culture sub dir see details follows
  6. });

middleware config(the same with before):

  1. app.UseRequestLocalization();

That’s it~

Add your resource files

TypeBasedResourcePath

For Types:

Home/Index => Controllers/HomeController

the resource path looking for:

  • Controllers/HomeController.[cultureName].json

for example:

  • Resources/Controllers/HomeController.en.json
  • Resources/Controllers/HomeController.zh.json

For RazorViews:

for example:

  • Resources/Views/Home/Index.en.json
  • Resources/Views/Home/Index.zh.json

CultureBasedResourcePath

For Types:

Home/Index => Controllers/HomeController

the resource path looking for:

  • Resources/[cultureName]/Controllers/HomeController.json

for example:

  • Resources/en/Controllers/HomeController.json
  • Resources/zh/Controllers/HomeController.json

For RazorViews:

for example:

  • Resources/en/Views/Home/Index.json
  • Resources/zh/Views/Home/Index.json

Copy your resource files to output:

you had to set resource files copy to output to make it works normal

add the follows sample config to your startup project file:

  1. <ItemGroup>
  2. <Content Update="Resources\**\*.json">
  3. <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  4. </Content>
  5. </ItemGroup>

the config above is made to make sure your json resource files in Resources dir copied to output, change it if you need

Use

just like what you do before:

Controller sample:

  1. public class ValuesController : Controller
  2. {
  3. private readonly IStringLocalizer<ValuesController> _localizer;
  4. public ValuesController(IStringLocalizer<ValuesController> localizer)
  5. {
  6. _localizer = localizer;
  7. }
  8. // GET: api/<controller>
  9. [HttpGet]
  10. public string Get()
  11. {
  12. return _localizer["Culture"];
  13. }
  14. }

Razor View Sample:

  1. @using Microsoft.AspNetCore.Mvc.Localization
  2. @using Microsoft.Extensions.Localization
  3. @using WeihanLi.Extensions.Localization.Json.Sample.Controllers
  4. @inject IHtmlLocalizer<HomeController> HtmlLocalizer
  5. @inject IStringLocalizer<HomeController> StringLocalizer
  6. @inject IViewLocalizer ViewLocalizer
  7. @{
  8. ViewData["Title"] = "Index";
  9. }
  10. <h2>Index</h2>
  11. <div>string: @StringLocalizer["Hello"]</div>
  12. <div>html: @HtmlLocalizer["Hello"]</div>
  13. <div>view: @ViewLocalizer["Hello"]</div>

Resource file sample:

  1. {
  2. "Culture": "English"
  3. }

Samples

Contact

Contact me via weihanli@outlook.com if you need