zoukankan      html  css  js  c++  java
  • .net core 实现基于 JSON 的多语言

    .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<ValuesController> localizer)
        {
            _localizer = localizer;
        }
    
        // GET: api/<controller>
        [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

    More

    扩展增加了 CultureBased 方式,这样就方便将某一种语言的语言包打包,也方便了扩展,支持其他语言的时候只需要导入一个其他语言的语言包即可

    在线示例: https://reservation.weihanli.xyz/

  • 相关阅读:
    可爱的中国电信 请问我们的电脑还属于我们自己吗?
    了解客户的需求,写出的代码或许才是最优秀的............
    DELPHI DATASNAP 入门操作(3)简单的主从表的简单更新【含简单事务处理】
    用数组公式获取字符在字符串中最后出现的位置
    在ehlib的DBGridEh控件中使用过滤功能(可以不用 MemTableEh 控件 适用ehlib 5.2 ehlib 5.3)
    格式化json返回的时间
    ExtJs中使用Ajax赋值给全局变量异常解决方案
    java compiler level does not match the version of the installed java project facet (转)
    收集的资料(六)ASP.NET编程中的十大技巧
    收集的资料共享出来(五)Asp.Net 权限解决办法
  • 原文地址:https://www.cnblogs.com/createwell/p/12747546.html
Copyright © 2011-2022 走看看