NetPro.Globalization
6.0.15
See the version list below for details.
dotnet add package NetPro.Globalization --version 6.0.15
NuGet\Install-Package NetPro.Globalization -Version 6.0.15
<PackageReference Include="NetPro.Globalization" Version="6.0.15" />
paket add NetPro.Globalization --version 6.0.15
#r "nuget: NetPro.Globalization, 6.0.15"
// Install NetPro.Globalization as a Cake Addin #addin nuget:?package=NetPro.Globalization&version=6.0.15 // Install NetPro.Globalization as a Cake Tool #tool nuget:?package=NetPro.Globalization&version=6.0.15
NetPro.Globalization使用
国际化多语言使用,支持官方资源文件的方式外底层增加了sqlite持久化存储方式,便于跨语言跨项目共享和检查遗漏的多语言
此项目通过修改了Localization.SqlLocalizer源码获得支持,特此感谢damienbod
使用
初始化服务
无依赖方式
public void ConfigureServices(IServiceCollection services)
{
services.AddGlobalization();
}
public void Configure(IApplicationBuilder application)
{
//置于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)
//.AddSupportedCultures(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);
}
appsetting.json
{
"Globalization": {
"UIQueryStringKey": "language",//请求的query携带的多语言参数名
"ConnectionString": "Data Source=LocalizationRecords.sqlite", //sqlite地址
"Cultures": [
"zh-CN",
"en-US"
],
"Annotations": true, //是否打开注册数据注解本地化服务
"Record": false //不存在是否记录(自动插入数据库默认语系),默认true
}
}
依赖于的环境使用
直接引用NetPro.Globalization nuget包,加入如下json配置即可
appsetting.json
{
"Globalization": {
"UIQueryStringKey": "language",//请求的query携带的多语言参数名
"ConnectionString": "Data Source=LocalizationRecords.sqlite", //sqlite地址
"Cultures": [
"zh-CN",
"en-US"
],
"Annotations": true, //是否打开注册数据注解本地化服务
"Record": false //不存在是否记录(自动插入数据库默认语系),默认true
}
}
业务使用
[ApiController]
[Route("/api/v1/[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly IStringLocalizer<Localization.SqlLocalizer.IntegrationTests.SharedResource> _aboutLocalizerizer;//共享多语言资源
public WeatherForecastController(IStringLocalizer<Localization.SqlLocalizer.IntegrationTests.SharedResource> aboutLocalizerizer)
{
_aboutLocalizerizer = aboutLocalizerizer;
}
[HttpGet("pay/create")]
public string Get()
{
var cultureui = CultureInfo.CurrentUICulture.ToString();
var culture = CultureInfo.CurrentCulture.ToString();
//原生用法,底层会处理sqlite持久化
return _aboutLocalizerizer["Name"];//会从SharedResource资源下查询Name对应的多语言,查询不到进入指定sqlite中查询,继续查询不到插入Name.当前语言代码
}
}
客户端处理
客户端请求接口时,依次支持,query,header,cookie等三种方式携带多语言标识
query 默认 ui-culture; 支持修改UIQueryStringKey
节点覆盖默认参数名
header 默认 Accept-Language
cookie 默认为 ".AspNetCore.Culture" 既:(Microsoft.AspNetCore.Localization.CookieRequestCultureProvider.DefaultCookieName)
tips
感谢KamenRiderKuuga开发的多语言翻译辅助工具Translator
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.EntityFrameworkCore (>= 3.0.0 && < 6.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 3.0.0 && < 6.0.0)
- Microsoft.EntityFrameworkCore.Relational (>= 3.0.0 && < 6.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 3.0.0 && < 6.0.0)
- NetPro.Startup (>= 6.0.15)
-
net6.0
- Microsoft.EntityFrameworkCore (>= 6.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 6.0.0)
- Microsoft.EntityFrameworkCore.Relational (>= 6.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 6.0.0)
- NetPro.Startup (>= 6.0.15)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on NetPro.Globalization:
Repository | Stars |
---|---|
LeonKou/NetPro
🌈An enhanced version with clean architecture of asp.netcore,efficiently manage startup,Support for netcore3.1/6.0
|
Version | Downloads | Last updated |
---|---|---|
6.0.16 | 314 | 7/24/2023 |
6.0.15 | 797 | 7/19/2022 |
6.0.14 | 530 | 7/10/2022 |
6.0.13 | 575 | 6/15/2022 |
6.0.12 | 479 | 6/15/2022 |
6.0.11 | 478 | 6/15/2022 |
6.0.10 | 479 | 6/11/2022 |
6.0.9 | 494 | 6/8/2022 |
6.0.8 | 528 | 5/26/2022 |
6.0.8-beta.3 | 152 | 5/24/2022 |
6.0.8-beta.2 | 123 | 5/24/2022 |
6.0.7 | 486 | 5/18/2022 |
6.0.6 | 599 | 4/28/2022 |
6.0.5 | 558 | 3/30/2022 |
6.0.5-beta.20 | 118 | 4/27/2022 |
6.0.5-beta.19 | 123 | 4/25/2022 |
6.0.5-beta.18 | 122 | 4/22/2022 |
6.0.5-beta.17 | 120 | 4/16/2022 |
6.0.5-beta.16 | 194 | 4/8/2022 |
6.0.5-beta.15 | 122 | 4/8/2022 |
6.0.5-beta.14 | 131 | 4/7/2022 |
6.0.5-beta.13 | 132 | 4/7/2022 |
6.0.5-beta.12 | 117 | 4/6/2022 |
6.0.5-beta.11 | 158 | 4/6/2022 |
6.0.5-beta.10 | 143 | 3/31/2022 |
6.0.5-beta.9 | 136 | 3/26/2022 |
6.0.5-beta.8 | 148 | 3/22/2022 |
6.0.5-beta.7 | 136 | 3/21/2022 |
6.0.5-beta.6 | 146 | 3/14/2022 |
6.0.5-beta.5 | 154 | 3/2/2022 |
6.0.5-beta.4 | 149 | 2/22/2022 |
6.0.5-beta.3 | 136 | 2/18/2022 |
6.0.5-beta.2 | 126 | 2/18/2022 |
6.0.5-beta.1 | 140 | 2/16/2022 |
6.0.4 | 576 | 2/10/2022 |
6.0.3 | 598 | 2/9/2022 |
6.0.3-beta.9 | 121 | 2/10/2022 |
6.0.3-beta.7 | 150 | 1/27/2022 |
6.0.3-beta.6 | 138 | 1/19/2022 |
6.0.3-beta.5 | 154 | 1/17/2022 |
6.0.3-beta.4 | 148 | 1/16/2022 |
6.0.3-beta.3 | 144 | 1/14/2022 |
6.0.3-beta.2 | 139 | 1/13/2022 |
6.0.3-beta.1 | 160 | 1/11/2022 |
6.0.2 | 365 | 1/6/2022 |
6.0.1 | 1,025 | 12/3/2021 |
3.1.11 | 403 | 11/17/2021 |
3.1.10 | 454 | 7/29/2021 |