19°

asp.net core 基于 JSON 实现多语言

asp.net core 基于 JSON 实现多语言

Intro

上次我们提到了,微软默认提供基于资源文件的多语言本地化,个人感觉使用起来不是太方便,没有 json 看起来直观,于是动手造了一个轮子, dotnet core 基于 json 的本地化组件

GetStarted

需要引用 nuget 包 WeihanLi.Extensions.Localization.Json WeihanLi.Extensions.Localization.Json

注册服务:

services.AddJsonLocalization(options =>
    {
        options.ResourcesPath = Configuration.GetAppSetting("ResourcesPath");
        options.ResourcesPathType = ResourcesPathType.TypeBased; // 默认方式和微软找资源的方式类似
        // options.ResourcesPathType = ResourcesPathType.CultureBased; // 在对应的 culture 子目录下寻找资源文件,可以参考后面的示例
    });

中间件配置(如果是asp.net core,和之前一样):

app.UseRequestLocalization();

That's it~

添加你的资源文件

TypeBased 资源文件的路径

For Types:

Home/Index => Controllers/HomeController

资源路径:

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

示例:

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

For Razor 视图:

示例:

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

CultureBased 资源文件路径

For Types:

Home/Index => Controllers/HomeController

资源路径:

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

示例:

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

For Razor 视图:

示例:

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

Copy your resource files to output:

需要设置将资源文件拷贝到输出目录,否则会找不到资源文件,可以在启动项目项目文件中加入以下示例代码:

<ItemGroup>
<Content Update="Resources\**\*.json">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

上面的配置会将 Resources 目录下的所有 json 文件拷贝到输出目录下,可以根据自己的需要进行修改

Use

用法和之前是一样的

Controller 示例:

public class ValuesController : Controller
{
    private readonly IStringLocalizer<ValuesController> _localizer;
public ValuesController(IStringLocalizer&lt;ValuesController&gt; localizer)
{
    _localizer = localizer;
}

// GET: api/&lt;controller&gt;
[HttpGet]
public string Get()
{
    return _localizer["Culture"];
}

}

Razor 视图示例:

@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@using WeihanLi.Extensions.Localization.Json.Sample.Controllers
@inject IHtmlLocalizer<HomeController> HtmlLocalizer
@inject IStringLocalizer<HomeController> StringLocalizer
@inject IViewLocalizer ViewLocalizer
@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<div>string: @StringLocalizer["Hello"]</div>

<div>html: @HtmlLocalizer["Hello"]</div>

<div>view: @ViewLocalizer["Hello"]</div>

资源文件示例:

{
  "Culture": "中文"
}

Samples

本文转载自博客园,原文链接:https://www.cnblogs.com/weihanli/p/json-based-localization-in-aspnetcore.html

全部评论: 0

    我有话说: