ASP.NET Core中提供了一些本地化服务和中间件,可将网站本地化为不同的语言文化。ASP.NET Core中我们可以使用Microsoft.AspNetCore.Localization库来实现本地化。但是默认只支持使用资源文件方式做多语言存储,很难在实际场景中使用。有没有可能支持官方资源文件的基础上还能支持动态添加修改多语言呢,答案是有的,那就是NetPro.Globalization
Package Manager方式:
Install-Package NetPro.Globalization 6.0.10
.NET CLI 方式:
dotnet add package NetPro.Globalization --version 6.0.10
PackageReference:
<PackageReference Include="NetPro.Globalization" Version="6.0.10" />
.NET CLI 方式:
paket add NetPro.Globalization --version 6.0.10
如基于NetPro环境的使用,比较简单,在执行安装步骤后配置多语言持久化的sqlite地址即可
"Globalization": {
"UIQueryStringKey": "language", //请求的query携带的多语言参数名,默认为language
"ConnectionString": "Data Source=Globalization.db;Cache=Shared", //sqlite地址
"Cultures": [
"zh-CN",
"en-US"
],
"Annotations": true, //是否打开注册数据注解本地化服务
"Record": true //不存在是否记录(自动插入数据库默认语系),默认true
}
如不是基于NetPro环境的使用,比需要手动进行各项初始化
public void ConfigureServices(IServiceCollection services)
{
services.AddGlobalization();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//置于app.UseRouting()后便可;
var configuration = app.ApplicationServices.GetService<IConfiguration>();
var globalization = configuration.GetSection(nameof(Globalization)).Get<Globalization>();
var cultures = globalization?.Cultures ?? new string[] { };
var localizationOptions = new RequestLocalizationOptions()
.AddSupportedUICultures(cultures)
;
localizationOptions.RequestCultureProviders.Insert(0, new QueryStringRequestCultureProvider { UIQueryStringKey = globalization.UIQueryStringKey });
localizationOptions.RequestCultureProviders.Insert(1, new AcceptLanguageHeaderRequestCultureProvider());
localizationOptions.RequestCultureProviders.Insert(2, new CookieRequestCultureProvider());
app.UseRequestLocalization(localizationOptions);
}
public class GlobalizationDemoController : ControllerBase
{
private readonly IStringLocalizer<NetPro.Globalization.Globalization> _localizer;//建议使用NetPro.Globalization.Globalization 统一的类提供字符串承载,过多语言碎片
public GlobalizationDemoController(
, IStringLocalizer<NetPro.Globalization.Globalization> localizer)
{
_localizer = localizer;
}
/// <summary>
/// 多语言国际化示例
/// </summary>
[HttpGet("globalization")]
public IActionResult Globalization()
{
var localMsg = _localizer["当前时间为"] + $":{DateTime.Now}";
return Ok(new { localMsg });
}
}
客户端请求接口时,依次支持,query,header,cookie等三种方式携带多语言标识
language
; 支持修改UIQueryStringKey
节点覆盖默认参数名,推荐query方式Accept-Language
.AspNetCore.Culture
既:(Microsoft.AspNetCore.Localization.CookieRequestCultureProvider.DefaultCookieName)中文环境:
request http://localhost:5001/api/globalizationdemo/globalization?language=zh-CN
response localMsg: "当前时间为:2022/6/11 14:47:01"
英文环境:
request http://localhost:5001/api/globalizationdemo/globalization?language=en-US
response localMsg: "当前时间为:2022/6/11 14:47:01"
发现依然中文,原因是找不到对应的语言,会自动将_localizer[string]中的字符串作为默认值插入,此时只需要手动再插入一条en-US的翻译即可
再次请求:
request http://localhost:5001/api/globalizationdemo/globalization?language=en-US
response localMsg: "This time is:2022/6/11 14:50:25"
除了以上支持sqlite数据库存储多语言信息,同时也支持微软默认的资源文件方式,并且使用方式与原生一致,不会有新的学习成本。
NetPro.Globalizatio: https://github.com/LeonKou/NetPro/tree/dev_6.0/src/Library/NetPro.Globalization
NetPro框架Github地址: https://github.com/LeonKou/NetPro