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

栏目: IT技术 · 发布时间: 5年前

内容简介:上次我们提到了,微软默认提供基于资源文件的多语言本地化,个人感觉使用起来不是太方便,没有 json 看起来直观,于是动手造了一个轮子, dotnet core 基于 json 的本地化组件需要引用 nuget 包 WeihanLi.Extensions.Localization.Json注册服务:

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

Intro

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

GetStarted

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

注册服务:

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

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:

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

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

Use

用法和之前是一样的

Controller 示例:

Razor 视图示例:

资源文件示例:

Samples

  • AspNetCore3.1 Basic Sample

  • ActivityReservation


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

How to Solve It

How to Solve It

Zbigniew Michalewicz、David B. Fogel / Springer / 2004-03-01 / USD 59.95

This book is the only source that provides comprehensive, current, and detailed information on problem solving using modern heuristics. It covers classic methods of optimization, including dynamic pro......一起来看看 《How to Solve It》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试