.net开发体系里,大部分本地化的实现都是用资源文件实现(.resx),asp.net core中的多语Culture是指区域性的对象,而UICulture 该对象表示资源管理器在运行时查找区域性特定资源时所用的当前用户接口区域性。

asp.net core实现也是通过添注入本地化服务,和添加中间件来实现的,例如下:

using Microsoft.Extensions.Localization;var builder = WebApplication.CreateBuilder(args);
//添加本地化中间件,Resources
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");var app = builder.Build();
//应用三种本地化资源
var supportedCultures = new[] { "zh-CN", "ja-JP", "en-US" };
var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0]).AddSupportedCultures(supportedCultures).AddSupportedUICultures(supportedCultures);
localizationOptions.ApplyCurrentCultureToResponseHeaders = true;
app.UseRequestLocalization(localizationOptions);
//通过向服务容器中获取StringLocalizer来获取具体本地化数据
app.MapGet("/demo", (IStringLocalizer<SharedResource> sharedLocalizer) =>
{return sharedLocalizer["ok"].Value;
});app.Run();
//ShareResource类型,不需要实现
public class SharedResource
{
}

.resx文件类型

.resx文档可视化界面

.resx文件xml数据,分描述部分和数据部分

<?xml version="1.0" encoding="utf-8"?>
<root><!-- Microsoft ResX Schema Version 2.0The primary goals of this format is to allow a simple XML format that is mostly human readable. The generation and parsing of the various data types are done through the TypeConverter classes associated with the data types.Example:... ado.net/XML headers & schema ...<resheader name="resmimetype">text/microsoft-resx</resheader><resheader name="version">2.0</resheader><resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader><resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader><data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data><data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data><data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64"><value>[base64 mime encoded serialized .NET Framework object]</value></data><data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"><value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value><comment>This is a comment</comment></data>There are any number of "resheader" rows that contain simple name/value pairs.Each data row contains a name, and value. The row also contains a type or mimetype. Type corresponds to a .NET class that support text/value conversion through the TypeConverter architecture. Classes that don't support this are serialized and stored with the mimetype set.The mimetype is used for serialized objects, and tells the ResXResourceReader how to depersist the object. This is currently not extensible. For a given mimetype the value must be set accordingly:Note - application/x-microsoft.net.object.binary.base64 is the format that the ResXResourceWriter will generate, however the reader can read any of the formats listed below.mimetype: application/x-microsoft.net.object.binary.base64value   : The object must be serialized with : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter: and then encoded with base64 encoding.mimetype: application/x-microsoft.net.object.soap.base64value   : The object must be serialized with : System.Runtime.Serialization.Formatters.Soap.SoapFormatter: and then encoded with base64 encoding.mimetype: application/x-microsoft.net.object.bytearray.base64value   : The object must be serialized into a byte array : using a System.ComponentModel.TypeConverter: and then encoded with base64 encoding.--><xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xsd:import namespace="http://www.w3.org/XML/1998/namespace" /><xsd:element name="root" msdata:IsDataSet="true"><xsd:complexType><xsd:choice maxOccurs="unbounded"><xsd:element name="metadata"><xsd:complexType><xsd:sequence><xsd:element name="value" type="xsd:string" minOccurs="0" /></xsd:sequence><xsd:attribute name="name" use="required" type="xsd:string" /><xsd:attribute name="type" type="xsd:string" /><xsd:attribute name="mimetype" type="xsd:string" /><xsd:attribute ref="xml:space" /></xsd:complexType></xsd:element><xsd:element name="assembly"><xsd:complexType><xsd:attribute name="alias" type="xsd:string" /><xsd:attribute name="name" type="xsd:string" /></xsd:complexType></xsd:element><xsd:element name="data"><xsd:complexType><xsd:sequence><xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /><xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /></xsd:sequence><xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /><xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /><xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /><xsd:attribute ref="xml:space" /></xsd:complexType></xsd:element><xsd:element name="resheader"><xsd:complexType><xsd:sequence><xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /></xsd:sequence><xsd:attribute name="name" type="xsd:string" use="required" /></xsd:complexType></xsd:element></xsd:choice></xsd:complexType></xsd:element></xsd:schema><resheader name="resmimetype"><value>text/microsoft-resx</value></resheader><resheader name="version"><value>2.0</value></resheader><resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader><resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader><data name="cancel" xml:space="preserve"><value>Cancel</value></data><data name="no" xml:space="preserve"><value>No</value></data><data name="ok" xml:space="preserve"><value>Yes</value></data>
</root>

.NET6之MiniAPI(十一):本地化相关推荐

  1. .NET6之MiniAPI(三十):结束篇(附链接)

    不知不觉来到了<.NET6之MiniAPI>的第三十篇,回顾之前的篇幅,主要涉及如下: HTTP请求,应答 Request 桂素伟,公众号:桂迹.NET6之MiniAPI(二):reque ...

  2. .NET6之MiniAPI(十九):NLog

    在本系例文章的第八篇中,我们聊过官方的日志实现,即<.NET6之MiniAPI(八):日志>.但官方的日志功能更多是提供了一个实现基础,对于要求一个灵活,强大,方便的日志体系,官方的还是有 ...

  3. .NET6之MiniAPI(二十一):限流

    限流,可以网络的基础设施进行配置实现,也可以在网关的地方进行限流,但服务本身的限流也不可或缺,因为当多副本时,一个副本故障,流量对于其他副本来说会提高,如果超过其承受请求量的范围,会引起服务连续崩塌, ...

  4. .NET6之MiniAPI(十八):OpenAPI swagger

    从本篇开始,介绍一些很不错的三方库,来丰富MiniAPI的使用. 在创建MiniAPI项目时,模板提供了一个是否启用OpenAPI的选项,足见这个三方库的优势和强大. OpenAPI为我们测试API提 ...

  5. .NET6之MiniAPI(二十九):UnitTest

    MiniAPI的单元测试与asp.net web api的单元测试大体是相同的(毕竟都是asp.net core),只是在小细节上有一些差异,文章中会说到这点. 本文测试框架是XUnit,Mock框架 ...

  6. .NET6之MiniAPI(二十七):Metrics

    应用的各种Metrics是保证应用健康稳定运行的基础,特别对于一些可用性有所要求的应用,本文介绍prometheus-net这个三方指示库. prometheus-net的工作原理是,在应用内部埋点, ...

  7. .NET6之MiniAPI(十五):跨域CORS(下)

    前一篇的跨域请求的方式是松宽的方式,毕竟跨域有安全风险,应尽量少的允许访问必要资源,本篇分别从请求方法,请求头和请求凭据方面了解跨域设置. 请求方法: api项目,get,post是默认访问,这里只设 ...

  8. .NET6之MiniAPI(十四):跨域CORS(上)

    为了说明跨请求,创建了两个项目,一个mini api,端口是5001,另一个razor page项目,端口是5280.5280项目会在页面用ajax的方式来请求5001,形成跨域请求,由于是本地测试, ...

  9. .NET6之MiniAPI(十二):引入EntityFramewor

    说明:本篇重点说明MiniAPI引入EntityFramework,EF的使用不是本篇的重点 本篇是在MiniAPI中使用EntityFramework,所以先奉上创建数据的脚本,数据库是SQL Se ...

最新文章

  1. es6 中的generator函数控制流程
  2. Apache端口设置多域名和转发请求(虚拟域名)
  3. python判断txt文件是否为空_Python中的空值怎么判断
  4. 在MATLAB中读取同一路径下多个txt或mat文件
  5. WebService入门Demo
  6. Microsoft AJAX Client Library规范的实例
  7. 15. HTML 块 div span
  8. 在Sql Server 2008上安装SDE 9.3
  9. 大数据平台对企业有什么作用
  10. 如何计算给定一个unigram语言模型_数位语音处理Lesson3-初识前端信号处理、语言模型...
  11. ios html5 苹方字体,iOS中使用自定义字体-苹方字体
  12. php cms 新闻采集,自动新闻采集软件快速入门图文详细教程
  13. win10录屏_不为人知的win10隐藏功能,一键录屏,你还不知道?
  14. 什么是负边沿触发_边沿触发 - FreeSaber - 博客园
  15. 【中科院】分子生物学-朱玉贤第四版-笔记-第10讲 分子生物学操作技术
  16. linux系统windows模拟器下载,Linux开源模拟器Wine 0.9.54版下载
  17. java 2022-09-21T10:41:00.000+0800 转换成 yyyy-MM-dd HH:mm:ss
  18. Stress Tests
  19. 软考报名已全部截止,疫情之下,这些要求不满足不得进入考场
  20. ASP.Net MVC从客户端中检测到有潜在危险的 Request.Form 值

热门文章

  1. Nginx配置文件详细说明(转)
  2. 话里话外:流程图绘制初级:六大常见错误
  3. python如何安装panda数据库_在Pycharm中安装Pandas库方法(简单易懂)
  4. 小程序 node.js mysql_基于Node.js+MySQL开发的开源微信小程序B2C商城(页面高仿网易严选)...
  5. 在类中用class时数据是共有还是私有_jvm学习笔记之class文件的加载、初始化
  6. sublime python配置运行
  7. docker swarm的应用----docker集群的构建
  8. 使用jquery+css实现瀑布流布局
  9. Android 4.X 系统加载 so 失败的原因分析
  10. Cannot resolve the collation conflict between SQL_Latin1_General_CP1_CI_AS and Latin1_General_100...