翼度科技»论坛 编程开发 .net 查看内容

理解ASP.NET Core - 全球化&本地化&多语言(Globalization and Localization

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
注:本文隶属于《理解ASP.NET Core》系列文章,请查看置顶博客或点击此处查看全文目录
概述

在众多知名品牌的网站中,比如微软官网、YouTube等,我们经常可以见到“切换页面语言”的功能,我们可以选择最适合的语言浏览页面内容。毫无疑问,为网站提供多种语言,页面内容本地化,大大扩展了受众范围,提升了用户体验。
名词术语

为了更好地理解下面的内容,我们先来了解一下行业内通用的名词术语:

  • Globalization (G11N):全球化,即使应用支持不同语言和区域的过程。G11N 是首字母、尾字母和它们之间字母的个数组成的,下同,不再赘述。
  • Localization (L10N):本地化,即针对特定语言和区域自定义全球化应用的过程。
  • Internationalization (I18N):国际化,又称为多语言,包含了全球化和本地化。
  • Culture:区域性,即一种语言文化或区域。
  • Neutral Culture:非特定区域性,即具有指定语言但不具有区域的区域性。例如“zh”、“en”,仅仅表示中文或英文,并没有包含指定地区,如大陆、香港、台湾等。
  • Specific Culture: 特定区域性,即具有指定语言和区域的区域性。例如“zh-CN”、“zh-HK”。
  • Parent Culture: 父区域性,例如“zh”就是“zh-CN”和“zh-HK”的父区域性。
区域代码查询:https://www.venea.net/web/culture_code
实现本地化

一般情况下,统一使用英文作为多语言的字典Key,在 Web 刚进入开发阶段时,最好就支持多语言,否则后续改造的工作量会比较大。当然,你可以选择使用中文作为 Key,不过并不太推荐,毕竟你总不能要求懂阿拉伯语的人要懂中文。
本地化器

ASP.NET Core 提供了多种本地化工具:

  • IStringLocalizer
  • IStringLocalizerFactory
  • IHtmlLocalizer
  • IViewLocalizer
IStringLocalizer

IStringLocalizer和IStringLocalizer可以在运行时提供区域性资源,使用非常简单,就像操作字典一样,提供一个 Key,就能获取到指定区域的资源。另外,它还允许 Key 在资源中不存在,此时返回的就是 Key 自身。我们下面称这个 Key 为资源名。
下面是他们的结构定义:
  1. public interface IStringLocalizer
  2. {
  3.     // 通过资源名获取本地化文本,如果资源不存在,则返回 name 自身
  4.     LocalizedString this[string name] { get; }
  5.    
  6.     // 通过资源名获取本地化文本,并允许将参数值填充到文本中,如果资源不存在,则返回 name 自身
  7.     LocalizedString this[string name, params object[] arguments] { get; }
  8.     // 获取所有的本地化资源文本
  9.     IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures);
  10. }
  11. public interface IStringLocalizer<out T> : IStringLocalizer
  12. {
  13. }
复制代码
在服务类中使用本地化


  • 首先,注入本地化服务,并启用中间件
  1. var builder = WebApplication.CreateBuilder(args);
  2. // 注册服务
  3. builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
  4. var app = builder.Build();
  5. // 启用中间件
  6. app.UseRequestLocalization(options =>
  7. {
  8.     var cultures = new[] { "zh-CN", "en-US", "zh-TW" };
  9.     options.AddSupportedCultures(cultures);
  10.     options.AddSupportedUICultures(cultures);
  11.     options.SetDefaultCulture(cultures[0]);
  12.    
  13.     // 当Http响应时,将 当前区域信息 设置到 Response Header:Content-Language 中
  14.     options.ApplyCurrentCultureToResponseHeaders = true;
  15. });
  16. app.MapControllerRoute(
  17.     name: "default",
  18.     pattern: "{controller=Home}/{action=Index}/{id?}");
  19. app.Run();
复制代码
首先,我们通过AddLocalization注册了IStringLocalizerFactory和IStringLocalizer,并指定了资源的根目录为“Resources”。
然后,我们又通过UseRequestLocalization启用了中间件RequestLocalizationMiddleware。默认情况下,该中间件支持的区域文化仅为当前区域文化,即CultureInfo.CurrentCulture和CultureInfo.CurrentUICulture,我们可以通过AddSupportedCultures和AddSupportedUICultures自定义设置多个支持的区域文化:

  • Culture:影响日期、时间、数字或货币的展示格式
  • UICulture:影响查找哪些区域文化资源(如.resx、json文件等),也就是说,如果这里未添加某区域文化A,即使添加了对应区域文化A的资源文件,也无发生效。一般 Culture 和 UICulture 保持一致。
另外,当我们的服务接收到一个请求时,如果该请求未指明当前的区域文化,就会使用默认的,这里我们通过SetDefaultCulture指定了默认区域文化为 zh-CN
最后,通过设置ApplyCurrentCultureToResponseHeaders为true,将当前区域信息设置到Http响应头的Content-Language中。

  • 接下来,我们新建“Resources/Controllers”目录,在 Resources 目录下新建2个共享资源文件,在 Controllers 目录中新建2个HomeController类的资源文件,目录结构如下:
  1. - Resources
  2. - Controllers
  3.    - HomeController.en-US.resx
  4.    - HomeController.zh-CN.resx
  5. - SharedResource.en-US.resx
  6. - SharedResource.zh-CN.resx
复制代码
并填充内容如下:

  • SharedResource.en-US.resx
名称值CurrentTimeCurrent Time:

  • SharedResource.zh-CN.resx
名称值CurrentTime当前时间:

  • HomeController.en-US.resx
名称值HelloWorldHello, World!

  • HomeController.zh-CN.resx
名称值HelloWorld你好,世界!
这些文件默认为“嵌入的资源”

  • 为了优雅地使用共享资源,我们在项目根目录下创建SharedResource伪类,用来代理共享资源。
  1. public class SharedResource
  2. {
  3.     // 里面是空的
  4. }
复制代码

  • 最后,我们在HomeController中尝试一下效果
  1. public class HomeController : Controller
  2. {
  3.     // 用于提供 HomeController 的区域性资源
  4.     private readonly IStringLocalizer<HomeController> _localizer;
  5.     // 通过代理伪类提供共享资源
  6.     private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
  7.     public HomeController(
  8. <ItemGroup>
  9.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  10.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  11. </ItemGroup>IStringLocalizer<HomeController> localizer,
  12. <ItemGroup>
  13.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  14.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  15. </ItemGroup>IStringLocalizer<SharedResource> sharedLocalizer
  16.     )
  17.     {
  18. <ItemGroup>
  19.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  20.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  21. </ItemGroup>_localizer = localizer;
  22. <ItemGroup>
  23.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  24.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  25. </ItemGroup>_sharedLocalizer = sharedLocalizer;
  26.     }
  27.     [HttpGet]
  28.     public IActionResult GetString()
  29.     {
  30. <ItemGroup>
  31.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  32.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  33. </ItemGroup>var content = $"当前区域文化:{CultureInfo.CurrentCulture.Name}\n" +
  34. <ItemGroup>
  35.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  36.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  37. </ItemGroup>    $"{_localizer["HelloWorld"]}\n" +
  38. <ItemGroup>
  39.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  40.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  41. </ItemGroup>    $"{_sharedLocalizer["CurrentTime"]}{DateTime.Now.ToLocalTime()}\n";
  42. <ItemGroup>
  43.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  44.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  45. </ItemGroup>return Content(content);
  46.     }
  47. }
复制代码
访问{your-host}/home/getstring,使用默认的区域文化zh-CN,获取结果如下:
  1. 当前区域文化:zh-CN
  2. 你好,世界!
  3. 当前时间:2023/6/2 11:19:08
复制代码
此时查看响应头信息,可以发现
  1. Content-Language: zh-CN
复制代码
下面,我们通过 url 传递参数culture,指定区域文化为en-US,访问{your-host}/home/getstring?culture=en-US,获取结果如下:
  1. 当前区域文化:en-US
  2. Hello, World!
  3. Current Time:6/2/2023 11:47:50 AM
复制代码
此时的响应头信息:
  1. Content-Language: en-US
复制代码
如果你的本地化果并不是预期的,并且当前区域文化没问题的情况下,可以通过SearchedLocation查看资源搜索位置(如 _localizer["HelloWord"].SearchedLocation),检查资源放置位置是否有误。
在模型验证中使用本地化

好了,我们已经掌握了本地化在服务类中的使用方法,接下来,一起来看下在模型验证中如何使用本地化。

  • 首先通过调用AddDataAnnotationsLocalization注册数据注解本地化服务:
  1. builder.Services
  2.     .AddControllersWithViews()
  3.     .AddDataAnnotationsLocalization();
复制代码

  • 接着在 Dtos 目录下新建RegisterDto模型类:
  1. public class RegisterDto
  2. {
  3.     [Required(ErrorMessage = "UserNameIsRequired")]
  4.     public string UserName { get; set; }
  5.     [Required(ErrorMessage = "PasswordIsRequired")]
  6.     [StringLength(8, ErrorMessage = "PasswordLeastCharactersLong", MinimumLength = 6)]
  7.     public string Password { get; set; }
  8.     [Compare("Password", ErrorMessage = "PasswordDoNotMatch")]
  9.     public string ConfirmPassword { get; set; }
  10. }
复制代码
其中 ErroMessage 赋值的均为本地化资源Key
3. 然后在“Resources/Dtos”目录下添加资源文件:

  • RegisterDto.en-US.resx
名称值PasswordDoNotMatchThe password and confirmation password do not matchPasswordIsRequiredThe Password field is requiredPasswordLeastCharactersLongThe Password must be at least {2} characters longUserNameIsRequiredThe UserName field is required

  • RegisterDto.zh-CN.resx
名称值PasswordDoNotMatch两次密码输入不一致PasswordIsRequired请输入密码PasswordLeastCharactersLong密码长度不能小于 {2}UserNameIsRequired请输入用户名

  • 最后在HomeController中添加一个Register方法:
  1. [HttpPost]
  2. public IActionResult Register([FromBody] RegisterDto dto)
  3. {
  4.     if (!ModelState.IsValid)
  5.     {
  6. <ItemGroup>
  7.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  8.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  9. </ItemGroup>return Content($"当前区域文化:{CultureInfo.CurrentCulture.Name}\n" +
  10. <ItemGroup>
  11.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  12.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  13. </ItemGroup>    "模型状态无效:" + Environment.NewLine +
  14. <ItemGroup>
  15.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  16.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  17. </ItemGroup>    string.Join(Environment.NewLine, ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage))));
  18.     }
  19.     return Ok();
  20. }
复制代码
测试结果就不贴了,赶紧自己试一试吧!
另外,如果你觉得每一个模型类都要创建一个资源文件太麻烦了,可以通过DataAnnotationLocalizerProvider来手动指定IStringLocalizer实例,例如设置所有模型类仅从 SharedResource 中寻找本地化资源
  1. builder.Services
  2.     .AddControllersWithViews()
  3.     .AddDataAnnotationsLocalization(options =>
  4.     {
  5. <ItemGroup>
  6.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  7.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  8. </ItemGroup>options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource));
  9.     });
复制代码
IStringLocalizerFactory

有时,我们可能想要使用一些没有代理类或代理类无法使用的区域资源,无法直接通过IStringLocalizer进行注入,那IStringLocalizerFactory就可以帮助我们获取对应的IStringLocalizer,该接口结构如下:
  1. public interface IStringLocalizerFactory
  2. {
  3.     IStringLocalizer Create(Type resourceSource);
  4.     IStringLocalizer Create(string baseName, string location);
  5. }
复制代码
下面我们通过IStringLocalizerFactory来获取HomeController资源实例:
  1. public class HomeController : Controller
  2. {
  3.     private readonly IStringLocalizer _localizer;
  4.     private readonly IStringLocalizer _localizer2;
  5.     public HomeController(IStringLocalizerFactory localizerFactory)
  6.     {
  7. <ItemGroup>
  8.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  9.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  10. </ItemGroup>_localizer = localizerFactory.Create(typeof(HomeController));
  11. <ItemGroup>
  12.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  13.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  14. </ItemGroup>_localizer2 = localizerFactory.Create("Controllers.HomeController", Assembly.GetExecutingAssembly().FullName);
  15.     }
  16.     [HttpGet]
  17.     public IActionResult GetString()
  18.     {
  19. <ItemGroup>
  20.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  21.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  22. </ItemGroup>var content = $"当前区域文化:{CultureInfo.CurrentCulture.Name}\n" +
  23. <ItemGroup>
  24.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  25.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  26. </ItemGroup>    $"{_localizer["HelloWorld"]}\n" +
  27. <ItemGroup>
  28.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  29.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  30. </ItemGroup>    $"{_localizer2["HelloWorld"]}\n";
  31. <ItemGroup>
  32.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  33.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  34. </ItemGroup>return Content(content);
  35.     }
  36. }
复制代码
这里演示了两种创建方式:

  • 一个是通过类型来创建,一般我们不会手动通过该方式获取,而是直接注入对应的泛型版本
  • 另一个是通过指定资源基础名称和所属程序集来创建,所谓资源基础名称,就是资源文件相对于资源根目录的相对路径+文件基础名称,例如对于 HomeController.XXX.resx 来说,资源根目录就是前面注册服务时设置的 Resources,相对路径为 Controllers,文件基础名为 HomeController,所以资源基础名称为 Controllers.HomeController
资源文件命名规则

是时候明确一下资源文件的命名规则了,很简单:类的资源名称 = 类的完整类型名 - 程序集名称。
还是拿HomeController来举例,假设所属程序集名称为LocalizationWeb.dll,默认根命名空间与程序集同名,那么它的全名称为LocalizationWeb.Controllers.HomeController,资源文件就需要命名为Controllers.HomeController.XXX.resx,而我们在注册本地化服务时,通过ResourcesPath指定了资源的根目录为 Resources,所以资源文件相对项目根目录的相对路径为Resources/Controllers.HomeController.XXX.resx。由于这样做可能会导致资源文件名字较长,并且不便于归类,所以我们可以将 Controllers 提取为目录,变为Resources/Controllers/HomeController.XXX.resx。
强烈建议程序的程序集名称与根命名空间保持一致,这样可以省很多事。如果不一致,当然也有解决办法,例如有个DifferentController,它位于Different.Controllers命名空间下,那么资源文件需要放置于Resources/Different/Controllers目录下。
最后,如果你愿意,可以把SharedResource类放到 Resources 文件夹下,让它和它的资源文件在一起,不过要注意它的命名空间,确保该类够按照上述规则对应到资源文件上。你可能还需要在.csproj文件中进行如下配置(二选一,具体原因参考此文档):
  1. <PropertyGroup>
  2.     <EmbeddedResourceUseDependentUponConvention>false</EmbeddedResourceUseDependentUponConvention>
  3. </PropertyGroup>
复制代码
  1. <ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>
复制代码
IHtmlLocalizer

相对于IStringLocalizer, IHtmlLocalizer和IHtmlLocalizer中的资源可以包含 HTML 代码,并使其能够在前端页面中正常渲染出来。
通常情况下,我们仅仅需要本地化文本内容,而不会包含 HTML。不过这里还是简单介绍一下。

  • 首先调用AddViewLocalization注册服务
  1. builder.Services
  2.     .AddControllersWithViews()
  3.     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
复制代码
此处我们注册了IHtmlLocalizerFactory、IHtmlLocalizer,以及接下来要讲的IViewLocalizer共3个服务,并且通过LanguageViewLocationExpanderFormat.Suffix指定了视图(View)语言资源命名格式为后缀,即 ..resx。

  • 接着在 SharedResource 的资源文件中添加以下内容:


  • SharedResource.en-US.resx
名称值WelcomeWelcome {0}!

  • SharedResource.zh-CN.resx
名称值Welcome欢迎 {0}!

  • 最后自己可以在视图中看一下效果,文本确实被加粗了:
  1. @inject IHtmlLocalizer<SharedResource> HtmlSharedResource
  2.     @HtmlSharedResource["Welcome", "jjj"]
复制代码
IViewLocalizer

IViewLocalizer是专门服务于视图的,他没有泛型版本,也没有工厂类,所以它只能用来获取当前视图资源文件中的资源,如果想要使用其他资源,可以使用IStringLocalizer或IHtmlLocalizer。
它继承自IHtmlLocalizer,所以它也支持资源中包含 HTML 代码:
  1. public interface IViewLocalizer : IHtmlLocalizer { }
复制代码
下面我们在Views/Home/Index.cshtml中演示一下效果。
上面我们已经通过AddViewLocalization将IViewLocalizer服务注册到容器中了。

  • 首先在Resources/Views/Home目录下增加以下两个资源文件,并设置内容:


  • Index.en-US.resx
名称值WelcomeWelcome {0} !!!

  • Index.zh-CN
名称值Welcome欢迎 {0} !!!

  • 在视图中使用并查看效果
  1. @inject IViewLocalizer L
  2.     <h1>@L["Welcome", "jjj"]</h1>
复制代码
区域性回退

当请求的区域资源未找到时,会回退到该区域的父区域资源,例如档区域文化为 zh-CN 时,HomeController资源文件查找顺序如下:

  • HomeController.zh-CN.resx
  • HomeController.zh.resx
  • HomeController.resx
如果都没找到,则会返回资源 Key 本身。
配置 CultureProvider

上面,我们通过在 url 中添加参数 culture 来设置当前请求的区域信息,实际上,ASP.NET Core 是通过IRequestCultureProvider接口来为我们提供区域的设置方式。
内置的 RequestCultureProvider

可以通过以下代码查看已添加的 Provider:
  1. app.UseRequestLocalization(options =>
  2. {
  3.     var cultureProviders = options.RequestCultureProviders;
  4. }
复制代码
可以看到,ASP.NET Core 框架默认添加了3种 Provider,分别为:

  • QueryStringRequestCultureProvider:通过在 Query 中设置"culture"、"ui-culture"的值,例如 ?culture=zh-CN&ui-culture=zh-CN
  • CookieRequestCultureProvider:通过Cookie中设置名为 ".AspNetCore.Culture" Key 的值,值形如 c=zh-CN|uic=zh-CN
  • AcceptLanguageHeaderRequestCultureProvider:从请求头中设置 "Accept-Language" 的值
如果只传了 culture 或 ui-culture,则会将该值同时赋值给 culture 或 ui-culture。我们可以通过以下代码查看
我们也可以在这3个的基础上进行自定义配置,例如通过在 Query 中设置"lang"的值来设置区域:
  1. options.AddInitialRequestCultureProvider(new QueryStringRequestCultureProvider() { QueryStringKey = "lang" });
复制代码
AddInitialRequestCultureProvider默认将新添加的 Provider 放置在首位。
内置的还有一个RouteDataRequestCultureProvider,不过它并没有被默认添加到提供器列表中。它默认可以通过在路由中设置 culture 的值来设置区域,就像微软官方文档一样。需要注意的是,一定要在 app.UseRouting() 之后再调用 app.UseRequestLocalization()。
实现自定义 RequestCultureProvider

实现自定义RequestCultureProvider的方式有两种,分别是通过委托和继承抽象类RequestCultureProvider。
下面,我们实现一个从自定义 Header 中获取区域文化信息的自定义RequestCultureProvider。

  • 通过委托实现自定义RequestCultureProviders
  1. app.UseRequestLocalization(options =>{    var cultures = new[] { "zh-CN", "en-US", "zh-TW" };    options.AddSupportedCultures(cultures);    options.AddSupportedUICultures(cultures);    options.SetDefaultCulture(cultures[0]);<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>    {<ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>ArgumentException.ThrowIfNullOrEmpty(nameof(context));<ItemGroup>
  8.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  9.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  10. </ItemGroup>// 从请求头“X-Lang”中获取区域文化信息<ItemGroup>
  11.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  12.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  13. </ItemGroup>var acceptLanguageHeader = context.Request.GetTypedHeaders().GetList("X-Lang");<ItemGroup>
  14.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  15.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  16. </ItemGroup>if (acceptLanguageHeader == null || acceptLanguageHeader.Count == 0)<ItemGroup>
  17.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  18.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  19. </ItemGroup>{<ItemGroup>
  20.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  21.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  22. </ItemGroup>    return Task.FromResult(default(ProviderCultureResult));<ItemGroup>
  23.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  24.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  25. </ItemGroup>}<ItemGroup>
  26.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  27.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  28. </ItemGroup>var languages = acceptLanguageHeader.AsEnumerable();<ItemGroup>
  29.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  30.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  31. </ItemGroup>// 如果值包含多,我们只取前3个<ItemGroup>
  32.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  33.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  34. </ItemGroup>languages = languages.Take(3);<ItemGroup>
  35.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  36.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  37. </ItemGroup>var orderedLanguages = languages.OrderByDescending(h => h, StringWithQualityHeaderValueComparer.QualityComparer)<ItemGroup>
  38.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  39.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  40. </ItemGroup>    .Select(x => x.Value).ToList();<ItemGroup>
  41.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  42.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  43. </ItemGroup>if (orderedLanguages.Count > 0)<ItemGroup>
  44.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  45.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  46. </ItemGroup>{<ItemGroup>
  47.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  48.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  49. </ItemGroup>    return Task.FromResult(new ProviderCultureResult(orderedLanguages));<ItemGroup>
  50.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  51.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  52. </ItemGroup>}<ItemGroup>
  53.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  54.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  55. </ItemGroup>return Task.FromResult(default(ProviderCultureResult));    }));}
复制代码
需要注意的是,当未获取到区域文化信息时,若想要接着让后面的RequestCultureProvider继续解析获取,则记得一定要返回default(ProviderCultureResult),否则建议直接返回默认区域文化,即new ProviderCultureResult(options.DefaultRequestCulture.Culture.Name。

  • 通过继承抽象类RequestCultureProvider
  1. public interface IRequestCultureProvider{    // 确定当前请求的区域性,我们要实现这个接口    Task DetermineProviderCultureResult(HttpContext httpContext);}public abstract class RequestCultureProvider : IRequestCultureProvider{    // 指代空区域性结果    protected static readonly Task NullProviderCultureResult = Task.FromResult(default(ProviderCultureResult));    // 中间件 RequestLocalizationMiddleware 的选项    public RequestLocalizationOptions? Options { get; set; }    public abstract Task DetermineProviderCultureResult(HttpContext httpContext);}public class CustomHeaderRequestCultureProvider : RequestCultureProvider{    // Header 名称,默认为 Accept-Language    public string HeaderName { get; set; } = HeaderNames.AcceptLanguage;    // 当 Header 值有多个时,最多取前 n 个    public int MaximumHeaderValuesToTry { get; set; } = 3;    public override Task DetermineProviderCultureResult(HttpContext httpContext)    {<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>ArgumentException.ThrowIfNullOrEmpty(nameof(httpContext));<ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>ArgumentException.ThrowIfNullOrEmpty(nameof(HeaderName));<ItemGroup>
  8.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  9.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  10. </ItemGroup>var acceptLanguageHeader = httpContext.Request.GetTypedHeaders().GetList(HeaderName);<ItemGroup>
  11.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  12.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  13. </ItemGroup>if (acceptLanguageHeader == null || acceptLanguageHeader.Count == 0)<ItemGroup>
  14.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  15.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  16. </ItemGroup>{<ItemGroup>
  17.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  18.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  19. </ItemGroup>    return NullProviderCultureResult;<ItemGroup>
  20.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  21.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  22. </ItemGroup>}<ItemGroup>
  23.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  24.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  25. </ItemGroup>var languages = acceptLanguageHeader.AsEnumerable();<ItemGroup>
  26.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  27.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  28. </ItemGroup>if (MaximumHeaderValuesToTry > 0)<ItemGroup>
  29.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  30.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  31. </ItemGroup>{<ItemGroup>
  32.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  33.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  34. </ItemGroup>    languages = languages.Take(MaximumHeaderValuesToTry);<ItemGroup>
  35.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  36.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  37. </ItemGroup>}<ItemGroup>
  38.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  39.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  40. </ItemGroup>var orderedLanguages = languages.OrderByDescending(h => h, StringWithQualityHeaderValueComparer.QualityComparer)<ItemGroup>
  41.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  42.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  43. </ItemGroup>    .Select(x => x.Value).ToList();<ItemGroup>
  44.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  45.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  46. </ItemGroup>if (orderedLanguages.Count > 0)<ItemGroup>
  47.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  48.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  49. </ItemGroup>{<ItemGroup>
  50.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  51.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  52. </ItemGroup>    return Task.FromResult(new ProviderCultureResult(orderedLanguages));<ItemGroup>
  53.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  54.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  55. </ItemGroup>}<ItemGroup>
  56.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  57.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  58. </ItemGroup>return NullProviderCultureResult;    }}app.UseRequestLocalization(options =>{    var cultures = new[] { "zh-CN", "en-US", "zh-TW" };    options.AddSupportedCultures(cultures);    options.AddSupportedUICultures(cultures);    options.SetDefaultCulture(cultures[0]);<ItemGroup>
  59.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  60.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  61. </ItemGroup>options.RequestCultureProviders.Insert(0, new CustomHeaderRequestCultureProvider { HeaderName = "X-Lang" });}
复制代码
使用 Json 资源文件

你可能和我一样,不太喜欢 .resx 资源文件,想要将多语言配置到 json 文件中,虽然微软并没有提供完整地实现,但是社区已经有大佬根据接口规范为我们写好了,这里推荐使用My.Extensions.Localization.Json。
ASP.NET Core 也支持 PO 文件,如果有兴趣,请自行了解。
只需要将AddLocalization替换为AddJsonLocalization即可:
  1. builder.Services.AddJsonLocalization(options => options.ResourcesPath = "JsonResources");
复制代码
后面就是在 json 文件中配置多语言了,例如:

  • HomeController.en-US.json
  1. {
  2.   "HelloWorld": "Hello,World!"
  3. }
复制代码

  • HomeController.zh-CN.json
  1. {
  2.   "HelloWorld": "你好,世界!"
  3. }
复制代码
设计原理

现在,基础用法我们已经了解了,接下来就一起学习一下它背后的原理吧。
鉴于涉及到的源码较多,所以为了控制文章长度,下面只列举核心代码。
IStringLocalizerFactory & IStringLocalizer

先来看下AddLocalization中注册的默认实现:
  1. public static class LocalizationServiceCollectionExtensions{    internal static void AddLocalizationServices(IServiceCollection services)    {<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>services.TryAddSingleton();<ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>services.TryAddTransient(typeof(IStringLocalizer), typeof(StringLocalizer));    }}
复制代码
一共注册了两个实现,分别是ResourceManagerStringLocalizerFactory和StringLocalizer,先来看一下工厂:
  1. public interface IStringLocalizerFactory
  2. {
  3.     IStringLocalizer Create(Type resourceSource);
  4.     IStringLocalizer Create(string baseName, string location);
  5. }public class ResourceManagerStringLocalizerFactory : IStringLocalizerFactory{    private readonly IResourceNamesCache _resourceNamesCache = new ResourceNamesCache();    private readonly ConcurrentDictionary _localizerCache =<ItemGroup>
  6.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  7.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  8. </ItemGroup>new ConcurrentDictionary();    private readonly string _resourcesRelativePath;    public ResourceManagerStringLocalizerFactory(<ItemGroup>
  9.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  10.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  11. </ItemGroup>IOptions localizationOptions)    {<ItemGroup>
  12.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  13.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  14. </ItemGroup>_resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;<ItemGroup>
  15.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  16.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  17. </ItemGroup>if (!string.IsNullOrEmpty(_resourcesRelativePath))<ItemGroup>
  18.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  19.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  20. </ItemGroup>{<ItemGroup>
  21.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  22.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  23. </ItemGroup>    // 将目录分隔符“/”和“\”全部替换为“.”<ItemGroup>
  24.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  25.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  26. </ItemGroup>    _resourcesRelativePath = _resourcesRelativePath.Replace(Path.AltDirectorySeparatorChar, '.')<ItemGroup>
  27.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  28.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  29. </ItemGroup><ItemGroup>
  30.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  31.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  32. </ItemGroup>.Replace(Path.DirectorySeparatorChar, '.') + ".";<ItemGroup>
  33.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  34.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  35. </ItemGroup>}    }    protected virtual string GetResourcePrefix(TypeInfo typeInfo)    {<ItemGroup>
  36.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  37.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  38. </ItemGroup>// 代码不列了,直接说一下逻辑吧:<ItemGroup>
  39.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  40.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  41. </ItemGroup>// 1. 如果资源根路径(_resourcesRelativePath)为空,即项目的根目录,那么直接返回 typeInfo.FullName<ItemGroup>
  42.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  43.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  44. </ItemGroup>// 2. 如果资源根路径(_resourcesRelativePath)不为空,那么需要将资源根目录拼接在 typeInfo.FullName 中间, 按照如下格式拼接(注意里面的是减号):"{RootNamespace}.{ResourceLocation}.{FullTypeName - RootNamespace}"    }    protected virtual string GetResourcePrefix(string baseResourceName, string baseNamespace)    {<ItemGroup>
  45.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  46.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  47. </ItemGroup>// 逻辑同上    }    public IStringLocalizer Create(Type resourceSource)    {<ItemGroup>
  48.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  49.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  50. </ItemGroup>var typeInfo = resourceSource.GetTypeInfo();<ItemGroup>
  51.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  52.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  53. </ItemGroup>var baseName = GetResourcePrefix(typeInfo);<ItemGroup>
  54.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  55.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  56. </ItemGroup>var assembly = typeInfo.Assembly;<ItemGroup>
  57.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  58.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  59. </ItemGroup>return _localizerCache.GetOrAdd(baseName, _ => CreateResourceManagerStringLocalizer(assembly, baseName));    }    public IStringLocalizer Create(string baseName, string location)    {<ItemGroup>
  60.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  61.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  62. </ItemGroup>return _localizerCache.GetOrAdd($"B={baseName},L={location}", _ =><ItemGroup>
  63.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  64.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  65. </ItemGroup>{<ItemGroup>
  66.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  67.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  68. </ItemGroup>    var assemblyName = new AssemblyName(location);<ItemGroup>
  69.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  70.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  71. </ItemGroup>    var assembly = Assembly.Load(assemblyName);<ItemGroup>
  72.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  73.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  74. </ItemGroup>    baseName = GetResourcePrefix(baseName, location);<ItemGroup>
  75.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  76.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  77. </ItemGroup>    return CreateResourceManagerStringLocalizer(assembly, baseName);<ItemGroup>
  78.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  79.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  80. </ItemGroup>});    }<ItemGroup>
  81.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  82.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  83. </ItemGroup>protected virtual ResourceManagerStringLocalizer CreateResourceManagerStringLocalizer(<ItemGroup>
  84.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  85.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  86. </ItemGroup>Assembly assembly,<ItemGroup>
  87.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  88.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  89. </ItemGroup>string baseName)    {<ItemGroup>
  90.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  91.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  92. </ItemGroup>return new ResourceManagerStringLocalizer(<ItemGroup>
  93.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  94.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  95. </ItemGroup>    new ResourceManager(baseName, assembly),    // 指定了资源的基础名和所属程序集<ItemGroup>
  96.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  97.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  98. </ItemGroup>    assembly,<ItemGroup>
  99.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  100.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  101. </ItemGroup>    baseName,<ItemGroup>
  102.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  103.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  104. </ItemGroup>    _resourceNamesCache);    }}
复制代码
可以看到,Create(Type resourceSource)和Create(string baseName, string location)的实现都是通过CreateResourceManagerStringLocalizer来创建的,并且实例类型就是ResourceManagerStringLocalizer。另外,还通过_localizerCache将已创建的资源实例缓存了下来,避免了重复创建的开销,只不过由于缓存 Key 的构造规则不同,两者创建的实例并不能共享。
如果你现在就想要验证一下 HomeController 中的 Localizer 是否是相同的,你会发现通过构造函数直接注入的 IStringLocalizer._localizer 才是真正干活,你可以参考这段代码来获取它:typeof(Microsoft.Extensions.Localization.StringLocalizer).GetField("_localizer", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance).GetValue(mySharedLocalizer)
接着看ResourceManagerStringLocalizer的实现细节:
  1. public interface IStringLocalizer{    LocalizedString this[string name] { get; }    LocalizedString this[string name, params object[] arguments] { get; }    IEnumerable GetAllStrings(bool includeParentCultures);}public class ResourceManagerStringLocalizer : IStringLocalizer{    // 将不存在的资源 Key 进行缓存    private readonly ConcurrentDictionary _missingManifestCache = new ConcurrentDictionary();    // 用于操作 .resx 资源文件    private readonly ResourceManager _resourceManager;    private readonly IResourceStringProvider _resourceStringProvider;    private readonly string _resourceBaseName;    public ResourceManagerStringLocalizer(<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>ResourceManager resourceManager,<ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>Assembly resourceAssembly,<ItemGroup>
  8.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  9.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  10. </ItemGroup>string baseName,<ItemGroup>
  11.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  12.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  13. </ItemGroup>    // 资源的基础名称,类似于 xxx.xxx.xxx<ItemGroup>
  14.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  15.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  16. </ItemGroup>IResourceNamesCache resourceNamesCache)<ItemGroup>
  17.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  18.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  19. </ItemGroup>: this(<ItemGroup>
  20.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  21.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  22. </ItemGroup>    resourceManager,<ItemGroup>
  23.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  24.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  25. </ItemGroup>    new AssemblyWrapper(resourceAssembly),<ItemGroup>
  26.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  27.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  28. </ItemGroup>    baseName,<ItemGroup>
  29.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  30.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  31. </ItemGroup>    resourceNamesCache)    {    }    internal ResourceManagerStringLocalizer(<ItemGroup>
  32.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  33.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  34. </ItemGroup>ResourceManager resourceManager,<ItemGroup>
  35.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  36.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  37. </ItemGroup>AssemblyWrapper resourceAssemblyWrapper,<ItemGroup>
  38.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  39.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  40. </ItemGroup>string baseName,<ItemGroup>
  41.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  42.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  43. </ItemGroup>IResourceNamesCache resourceNamesCache<ItemGroup>
  44.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  45.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  46. </ItemGroup>: this(<ItemGroup>
  47.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  48.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  49. </ItemGroup>      resourceManager,<ItemGroup>
  50.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  51.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  52. </ItemGroup>      new ResourceManagerStringProvider(<ItemGroup>
  53.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  54.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  55. </ItemGroup><ItemGroup>
  56.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  57.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  58. </ItemGroup>  resourceNamesCache,<ItemGroup>
  59.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  60.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  61. </ItemGroup><ItemGroup>
  62.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  63.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  64. </ItemGroup>  resourceManager,<ItemGroup>
  65.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  66.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  67. </ItemGroup><ItemGroup>
  68.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  69.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  70. </ItemGroup>  resourceAssemblyWrapper.Assembly,<ItemGroup>
  71.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  72.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  73. </ItemGroup><ItemGroup>
  74.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  75.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  76. </ItemGroup>  baseName),<ItemGroup>
  77.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  78.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  79. </ItemGroup>      baseName,<ItemGroup>
  80.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  81.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  82. </ItemGroup>      resourceNamesCache)    {    }    internal ResourceManagerStringLocalizer(<ItemGroup>
  83.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  84.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  85. </ItemGroup>ResourceManager resourceManager,<ItemGroup>
  86.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  87.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  88. </ItemGroup>IResourceStringProvider resourceStringProvider,<ItemGroup>
  89.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  90.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  91. </ItemGroup>string baseName,<ItemGroup>
  92.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  93.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  94. </ItemGroup>IResourceNamesCache resourceNamesCache)    {<ItemGroup>
  95.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  96.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  97. </ItemGroup>_resourceStringProvider = resourceStringProvider;<ItemGroup>
  98.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  99.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  100. </ItemGroup>_resourceManager = resourceManager;<ItemGroup>
  101.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  102.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  103. </ItemGroup>_resourceBaseName = baseName;<ItemGroup>
  104.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  105.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  106. </ItemGroup>_resourceNamesCache = resourceNamesCache;    }    public virtual LocalizedString this[string name]    {<ItemGroup>
  107.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  108.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  109. </ItemGroup>get<ItemGroup>
  110.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  111.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  112. </ItemGroup>{<ItemGroup>
  113.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  114.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  115. </ItemGroup>    var value = GetStringSafely(name, culture: null);<ItemGroup>
  116.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  117.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  118. </ItemGroup>    // LocalizedString 包含了 资源名、资源值、资源是否不存在、资源搜索位 等信息<ItemGroup>
  119.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  120.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  121. </ItemGroup>    return new LocalizedString(name, value ?? name, resourceNotFound: value == null, searchedLocation: _resourceBaseName);<ItemGroup>
  122.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  123.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  124. </ItemGroup>}    }    public virtual LocalizedString this[string name, params object[] arguments]    {<ItemGroup>
  125.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  126.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  127. </ItemGroup>get<ItemGroup>
  128.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  129.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  130. </ItemGroup>{<ItemGroup>
  131.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  132.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  133. </ItemGroup>    var format = GetStringSafely(name, culture: null);<ItemGroup>
  134.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  135.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  136. </ItemGroup>    var value = string.Format(CultureInfo.CurrentCulture, format ?? name, arguments);<ItemGroup>
  137.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  138.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  139. </ItemGroup>    return new LocalizedString(name, value, resourceNotFound: format == null, searchedLocation: _resourceBaseName);<ItemGroup>
  140.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  141.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  142. </ItemGroup>}    }    public virtual IEnumerable GetAllStrings(bool includeParentCultures) =><ItemGroup>
  143.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  144.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  145. </ItemGroup>GetAllStrings(includeParentCultures, CultureInfo.CurrentUICulture);    protected IEnumerable GetAllStrings(bool includeParentCultures, CultureInfo culture)    {<ItemGroup>
  146.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  147.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  148. </ItemGroup>// 通过 culture 获取所有资源,原理与通过资源名获取类似<ItemGroup>
  149.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  150.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  151. </ItemGroup>// 需要注意的是,它是通过 yield return 返回的    }    // 所谓 Safely,就是当 资源名 不存在时,不会抛出异常,而是返回 null    protected string? GetStringSafely(string name, CultureInfo? culture)    {<ItemGroup>
  152.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  153.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  154. </ItemGroup>var keyCulture = culture ?? CultureInfo.CurrentUICulture;<ItemGroup>
  155.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  156.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  157. </ItemGroup>var cacheKey = $"name={name}&culture={keyCulture.Name}";<ItemGroup>
  158.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  159.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  160. </ItemGroup>// 资源已缓存为不存在,直接返回 null<ItemGroup>
  161.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  162.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  163. </ItemGroup>if (_missingManifestCache.ContainsKey(cacheKey))<ItemGroup>
  164.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  165.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  166. </ItemGroup>{<ItemGroup>
  167.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  168.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  169. </ItemGroup>    return null;<ItemGroup>
  170.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  171.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  172. </ItemGroup>}<ItemGroup>
  173.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  174.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  175. </ItemGroup>try<ItemGroup>
  176.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  177.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  178. </ItemGroup>{<ItemGroup>
  179.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  180.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  181. </ItemGroup>    // 通过 ResourceManager 获取资源<ItemGroup>
  182.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  183.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  184. </ItemGroup>    return _resourceManager.GetString(name, culture);<ItemGroup>
  185.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  186.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  187. </ItemGroup>}<ItemGroup>
  188.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  189.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  190. </ItemGroup>catch (MissingManifestResourceException)<ItemGroup>
  191.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  192.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  193. </ItemGroup>{<ItemGroup>
  194.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  195.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  196. </ItemGroup>    // 若资源不存在,则缓存<ItemGroup>
  197.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  198.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  199. </ItemGroup>    _missingManifestCache.TryAdd(cacheKey, null);<ItemGroup>
  200.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  201.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  202. </ItemGroup>    return null;<ItemGroup>
  203.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  204.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  205. </ItemGroup>}    }}
复制代码
好了,资源的加载流程我们已经清楚了,还有一个StringLocalizer需要看一下:
  1. public interface IStringLocalizer : IStringLocalizer{}public class StringLocalizer : IStringLocalizer{    private readonly IStringLocalizer _localizer;    public StringLocalizer(IStringLocalizerFactory factory)    {<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>_localizer = factory.Create(typeof(TResourceSource));    }    public virtual LocalizedString this[string name] => _localizer[name];    public virtual LocalizedString this[string name, params object[] arguments] => _localizer[name, arguments];    public IEnumerable GetAllStrings(bool includeParentCultures) =><ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>_localizer.GetAllStrings(includeParentCultures);}
复制代码
其实很简单,本质上还是通过工厂创建的本地化实例,真正干活的其实是它的私有变量_localizer,泛型只是一层包装。
DataAnnotationsLocalization

现在StringLocalizer的原理我们已经搞清楚了,但是数据注解本地化是如何实现的呢?它啊,其实也是通过StringLocalizer实现的,看:
  1. public static IMvcCoreBuilder AddDataAnnotationsLocalization(    this IMvcCoreBuilder builder,    Action? setupAction){    AddDataAnnotationsLocalizationServices(services, setupAction);    return builder;}public static void AddDataAnnotationsLocalizationServices(    IServiceCollection services,    Action? setupAction){    services.AddLocalization();    // 如果传入的 setup 委托不为空则使用该委托配置 MvcDataAnnotationsLocalizationOptions,    if (setupAction != null)    {<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>services.Configure(setupAction);    }    // 否则使用默认的 MvcDataAnnotationsLocalizationOptionsSetup 进行配置    else    {<ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>services.TryAddEnumerable(<ItemGroup>
  8.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  9.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  10. </ItemGroup>    ServiceDescriptor.Transient<ItemGroup>
  11.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  12.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  13. </ItemGroup>    ());    }}internal class MvcDataAnnotationsLocalizationOptionsSetup : IConfigureOptions{    public void Configure(MvcDataAnnotationsLocalizationOptions options)    {<ItemGroup>
  14.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  15.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  16. </ItemGroup>options.DataAnnotationLocalizerProvider = (modelType, stringLocalizerFactory) =><ItemGroup>
  17.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  18.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  19. </ItemGroup>    stringLocalizerFactory.Create(modelType);    }}
复制代码
可以看到,MvcDataAnnotationsLocalizationOptions提供了一个委托DataAnnotationLocalizerProvider,它接收两个参数,Type和IStringLocalizerFactory,返回一个IStringLocalizer。从这里我们就可以看出来,它的本地化就是通过IStringLocalizer来实现的。
默认情况下,它的本地化器指向当前模型类资源,我上面提到过,可以将其自定义为从共享资源中获取,这下你就理解为啥所有模类都会受影响了吧。
IViewLocalizer & IHtmlLocalizer

IViewLocalizer、IHtmlLocalizer和IHtmlLocalizer这里就不再深入了,毕竟现在前端更多的是用三大前端框架,等用到的时候再去了解吧。
RequestLocalizationMiddleware

RequestLocalizationMiddleware的作用主要是解析并设置当前请求的区域文化,以便于本地化器可以正常工作。
我们可以通过RequestLocalizationOptions对该中间件进行配置,可配置项如下:
  1. public class RequestLocalizationOptions{    private RequestCulture _defaultRequestCulture =<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>new RequestCulture(CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);    public RequestLocalizationOptions()    {<ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>RequestCultureProviders = new List<ItemGroup>
  8.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  9.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  10. </ItemGroup>{<ItemGroup>
  11.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  12.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  13. </ItemGroup>    new QueryStringRequestCultureProvider { Options = this },<ItemGroup>
  14.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  15.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  16. </ItemGroup>    new CookieRequestCultureProvider { Options = this },<ItemGroup>
  17.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  18.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  19. </ItemGroup>    new AcceptLanguageHeaderRequestCultureProvider { Options = this }<ItemGroup>
  20.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  21.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  22. </ItemGroup>};    }    // 默认请求区域文化,默认值:当前区域文化    public RequestCulture DefaultRequestCulture    {<ItemGroup>
  23.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  24.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  25. </ItemGroup>get => _defaultRequestCulture;<ItemGroup>
  26.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  27.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  28. </ItemGroup>set<ItemGroup>
  29.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  30.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  31. </ItemGroup>{<ItemGroup>
  32.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  33.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  34. </ItemGroup>    if (value == null)<ItemGroup>
  35.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  36.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  37. </ItemGroup>    {<ItemGroup>
  38.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  39.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  40. </ItemGroup><ItemGroup>
  41.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  42.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  43. </ItemGroup>throw new ArgumentNullException(nameof(value));<ItemGroup>
  44.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  45.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  46. </ItemGroup>    }<ItemGroup>
  47.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  48.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  49. </ItemGroup>    _defaultRequestCulture = value;<ItemGroup>
  50.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  51.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  52. </ItemGroup>}    }    // 是否允许回退到父区域文化,默认值:true    public bool FallBackToParentCultures { get; set; } = true;    // 是否允许回退到父UI区域文化,默认值:true    public bool FallBackToParentUICultures { get; set; } = true;    // 是否要将当前请求的区域文化设置到响应头 Content-Language 中,默认值:false    public bool ApplyCurrentCultureToResponseHeaders { get; set; }    // 受支持的区域文化列表,默认仅支持当前区域文化    public IList? SupportedCultures { get; set; } = new List { CultureInfo.CurrentCulture };    // 受支持的UI区域文化列表,默认仅支持当前UI区域文化    public IList? SupportedUICultures { get; set; } = new List { CultureInfo.CurrentUICulture };    // 请求区域文化提供器列表    public IList RequestCultureProviders { get; set; }    // 设置受支持的区域文化(注意,它的行为是 Set,而不是 Add)    public RequestLocalizationOptions AddSupportedCultures(params string[] cultures)    {<ItemGroup>
  53.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  54.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  55. </ItemGroup>var supportedCultures = new List(cultures.Length);<ItemGroup>
  56.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  57.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  58. </ItemGroup>foreach (var culture in cultures)<ItemGroup>
  59.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  60.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  61. </ItemGroup>{<ItemGroup>
  62.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  63.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  64. </ItemGroup>    supportedCultures.Add(new CultureInfo(culture));<ItemGroup>
  65.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  66.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  67. </ItemGroup>}<ItemGroup>
  68.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  69.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  70. </ItemGroup>SupportedCultures = supportedCultures;<ItemGroup>
  71.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  72.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  73. </ItemGroup>return this;    }    // 设置受支持的UI区域文化(注意,它的行为是 Set,而不是 Add)    public RequestLocalizationOptions AddSupportedUICultures(params string[] uiCultures)    {<ItemGroup>
  74.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  75.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  76. </ItemGroup>var supportedUICultures = new List(uiCultures.Length);<ItemGroup>
  77.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  78.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  79. </ItemGroup>foreach (var culture in uiCultures)<ItemGroup>
  80.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  81.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  82. </ItemGroup>{<ItemGroup>
  83.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  84.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  85. </ItemGroup>    supportedUICultures.Add(new CultureInfo(culture));<ItemGroup>
  86.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  87.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  88. </ItemGroup>}<ItemGroup>
  89.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  90.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  91. </ItemGroup>SupportedUICultures = supportedUICultures;<ItemGroup>
  92.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  93.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  94. </ItemGroup>return this;    }    // 设置默认区域文化    public RequestLocalizationOptions SetDefaultCulture(string defaultCulture)    {<ItemGroup>
  95.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  96.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  97. </ItemGroup>DefaultRequestCulture = new RequestCulture(defaultCulture);<ItemGroup>
  98.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  99.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  100. </ItemGroup>return this;    }}
复制代码
下面看一下RequestLocalizationMiddleware中间件的实现:
  1. public class RequestLocalizationMiddleware{    // 区域文化回退最大深度,5 层已经很足够了    private const int MaxCultureFallbackDepth = 5;    private readonly RequestDelegate _next;    private readonly RequestLocalizationOptions _options;    public RequestLocalizationMiddleware(RequestDelegate next, IOptions options)    {<ItemGroup>
  2.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  3.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  4. </ItemGroup>_next = next ?? throw new ArgumentNullException(nameof(next));<ItemGroup>
  5.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  6.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  7. </ItemGroup>_options = options.Value;    }    public async Task Invoke(HttpContext context)    {<ItemGroup>
  8.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  9.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  10. </ItemGroup>// 默认当前请求区域文化为 options 中配置的默认值<ItemGroup>
  11.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  12.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  13. </ItemGroup>var requestCulture = _options.DefaultRequestCulture;<ItemGroup>
  14.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  15.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  16. </ItemGroup>IRequestCultureProvider? winningProvider = null;<ItemGroup>
  17.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  18.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  19. </ItemGroup>// 如果存在 Provider,则通过 Provider 解析当前请求中设置的区域文化<ItemGroup>
  20.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  21.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  22. </ItemGroup>if (_options.RequestCultureProviders != null)<ItemGroup>
  23.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  24.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  25. </ItemGroup>{<ItemGroup>
  26.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  27.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  28. </ItemGroup>    foreach (var provider in _options.RequestCultureProviders)<ItemGroup>
  29.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  30.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  31. </ItemGroup>    {<ItemGroup>
  32.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  33.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  34. </ItemGroup><ItemGroup>
  35.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  36.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  37. </ItemGroup>var providerResultCulture = await provider.DetermineProviderCultureResult(context);<ItemGroup>
  38.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  39.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  40. </ItemGroup><ItemGroup>
  41.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  42.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  43. </ItemGroup>// 如果解析出来为 null,则继续让后续的 Provider 继续解析<ItemGroup>
  44.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  45.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  46. </ItemGroup><ItemGroup>
  47.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  48.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  49. </ItemGroup>if (providerResultCulture == null)<ItemGroup>
  50.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  51.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  52. </ItemGroup><ItemGroup>
  53.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  54.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  55. </ItemGroup>{<ItemGroup>
  56.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  57.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  58. </ItemGroup><ItemGroup>
  59.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  60.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  61. </ItemGroup>    continue;<ItemGroup>
  62.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  63.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  64. </ItemGroup><ItemGroup>
  65.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  66.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  67. </ItemGroup>}<ItemGroup>
  68.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  69.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  70. </ItemGroup><ItemGroup>
  71.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  72.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  73. </ItemGroup>var cultures = providerResultCulture.Cultures;<ItemGroup>
  74.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  75.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  76. </ItemGroup><ItemGroup>
  77.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  78.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  79. </ItemGroup>var uiCultures = providerResultCulture.UICultures;<ItemGroup>
  80.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  81.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  82. </ItemGroup><ItemGroup>
  83.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  84.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  85. </ItemGroup>CultureInfo? cultureInfo = null;<ItemGroup>
  86.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  87.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  88. </ItemGroup><ItemGroup>
  89.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  90.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  91. </ItemGroup>CultureInfo? uiCultureInfo = null;<ItemGroup>
  92.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  93.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  94. </ItemGroup><ItemGroup>
  95.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  96.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  97. </ItemGroup>if (_options.SupportedCultures != null)<ItemGroup>
  98.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  99.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  100. </ItemGroup><ItemGroup>
  101.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  102.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  103. </ItemGroup>{<ItemGroup>
  104.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  105.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  106. </ItemGroup><ItemGroup>
  107.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  108.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  109. </ItemGroup>    // 检查区域文化(可能有多个)是否支持,如果不支持则返回 null<ItemGroup>
  110.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  111.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  112. </ItemGroup><ItemGroup>
  113.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  114.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  115. </ItemGroup>    cultureInfo = GetCultureInfo(<ItemGroup>
  116.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  117.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  118. </ItemGroup><ItemGroup>
  119.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  120.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  121. </ItemGroup><ItemGroup>
  122.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  123.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  124. </ItemGroup>cultures,<ItemGroup>
  125.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  126.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  127. </ItemGroup><ItemGroup>
  128.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  129.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  130. </ItemGroup><ItemGroup>
  131.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  132.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  133. </ItemGroup>_options.SupportedCultures,<ItemGroup>
  134.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  135.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  136. </ItemGroup><ItemGroup>
  137.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  138.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  139. </ItemGroup><ItemGroup>
  140.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  141.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  142. </ItemGroup>_options.FallBackToParentCultures);<ItemGroup>
  143.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  144.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  145. </ItemGroup><ItemGroup>
  146.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  147.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  148. </ItemGroup>}<ItemGroup>
  149.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  150.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  151. </ItemGroup><ItemGroup>
  152.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  153.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  154. </ItemGroup>if (_options.SupportedUICultures != null)<ItemGroup>
  155.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  156.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  157. </ItemGroup><ItemGroup>
  158.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  159.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  160. </ItemGroup>{<ItemGroup>
  161.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  162.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  163. </ItemGroup><ItemGroup>
  164.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  165.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  166. </ItemGroup>    // 检查UI区域文化(可能有多个)是否支持,如果不支持则返回 null<ItemGroup>
  167.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  168.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  169. </ItemGroup><ItemGroup>
  170.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  171.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  172. </ItemGroup>    uiCultureInfo = GetCultureInfo(<ItemGroup>
  173.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  174.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  175. </ItemGroup><ItemGroup>
  176.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  177.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  178. </ItemGroup><ItemGroup>
  179.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  180.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  181. </ItemGroup>uiCultures,<ItemGroup>
  182.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  183.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  184. </ItemGroup><ItemGroup>
  185.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  186.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  187. </ItemGroup><ItemGroup>
  188.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  189.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  190. </ItemGroup>_options.SupportedUICultures,<ItemGroup>
  191.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  192.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  193. </ItemGroup><ItemGroup>
  194.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  195.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  196. </ItemGroup><ItemGroup>
  197.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  198.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  199. </ItemGroup>_options.FallBackToParentUICultures);<ItemGroup>
  200.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  201.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  202. </ItemGroup><ItemGroup>
  203.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  204.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  205. </ItemGroup>}<ItemGroup>
  206.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  207.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  208. </ItemGroup><ItemGroup>
  209.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  210.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  211. </ItemGroup>// 如果区域文化和UI区域文化均不受支持,则视为解析失败,继续让下一个 Provider 解析<ItemGroup>
  212.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  213.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  214. </ItemGroup><ItemGroup>
  215.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  216.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  217. </ItemGroup>if (cultureInfo == null && uiCultureInfo == null)<ItemGroup>
  218.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  219.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  220. </ItemGroup><ItemGroup>
  221.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  222.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  223. </ItemGroup>{<ItemGroup>
  224.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  225.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  226. </ItemGroup><ItemGroup>
  227.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  228.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  229. </ItemGroup>    continue;<ItemGroup>
  230.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  231.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  232. </ItemGroup><ItemGroup>
  233.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  234.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  235. </ItemGroup>}<ItemGroup>
  236.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  237.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  238. </ItemGroup><ItemGroup>
  239.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  240.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  241. </ItemGroup>// 两种区域文化若有为 null 的,则赋 options 中设置的默认值<ItemGroup>
  242.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  243.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  244. </ItemGroup><ItemGroup>
  245.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  246.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  247. </ItemGroup>// 注意:我们上面讲 Provider 时提到过,如果只传了 culture 和 ui-culture 其中的一个值,会将该值赋值到两者,这个行为是 Provider 中执行的,不要搞混咯<ItemGroup>
  248.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  249.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  250. </ItemGroup><ItemGroup>
  251.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  252.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  253. </ItemGroup>cultureInfo ??= _options.DefaultRequestCulture.Culture;<ItemGroup>
  254.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  255.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  256. </ItemGroup><ItemGroup>
  257.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  258.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  259. </ItemGroup>uiCultureInfo ??= _options.DefaultRequestCulture.UICulture;<ItemGroup>
  260.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  261.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  262. </ItemGroup><ItemGroup>
  263.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  264.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  265. </ItemGroup>var result = new RequestCulture(cultureInfo, uiCultureInfo);<ItemGroup>
  266.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  267.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  268. </ItemGroup><ItemGroup>
  269.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  270.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  271. </ItemGroup>requestCulture = result;<ItemGroup>
  272.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  273.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  274. </ItemGroup><ItemGroup>
  275.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  276.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  277. </ItemGroup>winningProvider = provider;<ItemGroup>
  278.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  279.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  280. </ItemGroup><ItemGroup>
  281.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  282.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  283. </ItemGroup><ItemGroup>
  284.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  285.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  286. </ItemGroup><ItemGroup>
  287.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  288.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  289. </ItemGroup>// 解析成功,直接跳出<ItemGroup>
  290.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  291.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  292. </ItemGroup><ItemGroup>
  293.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  294.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  295. </ItemGroup>break;<ItemGroup>
  296.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  297.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  298. </ItemGroup>    }<ItemGroup>
  299.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  300.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  301. </ItemGroup>}<ItemGroup>
  302.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  303.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  304. </ItemGroup>context.Features.Set(new RequestCultureFeature(requestCulture, winningProvider));<ItemGroup>
  305.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  306.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  307. </ItemGroup>// 将当前区域文化信息设置到当前请求的线程,便于后续本地化器读取<ItemGroup>
  308.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  309.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  310. </ItemGroup>SetCurrentThreadCulture(requestCulture);<ItemGroup>
  311.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  312.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  313. </ItemGroup>if (_options.ApplyCurrentCultureToResponseHeaders)<ItemGroup>
  314.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  315.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  316. </ItemGroup>{<ItemGroup>
  317.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  318.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  319. </ItemGroup>    var headers = context.Response.Headers;<ItemGroup>
  320.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  321.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  322. </ItemGroup>    headers.ContentLanguage = requestCulture.UICulture.Name;<ItemGroup>
  323.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  324.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  325. </ItemGroup>}<ItemGroup>
  326.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  327.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  328. </ItemGroup>await _next(context);    }    private static void SetCurrentThreadCulture(RequestCulture requestCulture)    {<ItemGroup>
  329.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  330.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  331. </ItemGroup>CultureInfo.CurrentCulture = requestCulture.Culture;<ItemGroup>
  332.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  333.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  334. </ItemGroup>CultureInfo.CurrentUICulture = requestCulture.UICulture;    }    private static CultureInfo? GetCultureInfo(<ItemGroup>
  335.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  336.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  337. </ItemGroup>IList cultureNames,<ItemGroup>
  338.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  339.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  340. </ItemGroup>IList supportedCultures,<ItemGroup>
  341.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  342.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  343. </ItemGroup>bool fallbackToParentCultures)    {<ItemGroup>
  344.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  345.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  346. </ItemGroup>foreach (var cultureName in cultureNames)<ItemGroup>
  347.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  348.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  349. </ItemGroup>{<ItemGroup>
  350.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  351.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  352. </ItemGroup>    if (cultureName != null)<ItemGroup>
  353.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  354.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  355. </ItemGroup>    {<ItemGroup>
  356.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  357.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  358. </ItemGroup><ItemGroup>
  359.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  360.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  361. </ItemGroup>// 里面通过递归查找支持的区域文化(包括回退的)<ItemGroup>
  362.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  363.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  364. </ItemGroup><ItemGroup>
  365.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  366.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  367. </ItemGroup>var cultureInfo = GetCultureInfo(cultureName, supportedCultures, fallbackToParentCultures, currentDepth: 0);<ItemGroup>
  368.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  369.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  370. </ItemGroup><ItemGroup>
  371.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  372.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  373. </ItemGroup>if (cultureInfo != null)<ItemGroup>
  374.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  375.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  376. </ItemGroup><ItemGroup>
  377.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  378.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  379. </ItemGroup>{<ItemGroup>
  380.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  381.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  382. </ItemGroup><ItemGroup>
  383.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  384.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  385. </ItemGroup>    return cultureInfo;<ItemGroup>
  386.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  387.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  388. </ItemGroup><ItemGroup>
  389.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  390.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  391. </ItemGroup>}<ItemGroup>
  392.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  393.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  394. </ItemGroup>    }<ItemGroup>
  395.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  396.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  397. </ItemGroup>}<ItemGroup>
  398.     <EmbeddedResource Include="Resources/SharedResource.en-US.resx" DependentUpon="SharedResources" />
  399.     <EmbeddedResource Include="Resources/SharedResource.zh-CN.resx" DependentUpon="SharedResources" />
  400. </ItemGroup>return null;    }}
复制代码
总结

通过以上内容,我们可以总结出以下核心知识点:

  • ASP.NET Core 提供了3种本地化器:

    • IStringLocalizer或IStringLocalizer:文本本地化器,是最常用的,可以通过依赖注入获取,也可以通过IStringLocalizerFactory来获取。IStringLocalizer是对IStringLocalizer的一层包装。
    • IHtmlLocalizer或IHtmlLocalizer:HTML本地化器,顾名思义,可以本地化HTML文本而不会对其编码。可以通过依赖注入获取,也可以通过IHtmlLocalizerFactory来获取。
    • IViewLocalizer:视图本地化器,用于前端视图的本地化。

  • 通过AddLocalization设置资源根目录,并注册本地化服务IStringLocalizer和IStringLocalizerFactory
  • 通过AddDataAnnotationsLocalization注册数据注解本地化服务,主要是设置DataAnnotationLocalizerProvider委托
  • 通过AddViewLocalization注册视图本地化服务IViewLocalizer、IHtmlLocalizer和IHtmlLocalizerFactory
  • 通过UseRequestLocalization启用请求本地化中间件RequestLocalizationMiddleware,它可以从请求中解析出当前请求的区域文化信息并设置到当前的处理线程中。

    • 通过AddSupportedCultures和AddSupportedUICultures配置受支持的 Cultures 和 UICultures
    • 通过SetDefaultCulture配置默认 Culture
    • 默认提供了三种RequestCultureProvider:

      • QueryStringRequestCultureProvider:通过在 Query 中设置"culture"、"ui-culture"的值,例如 ?culture=zh-CN&ui-culture=zh-CN
      • CookieRequestCultureProvider:通过Cookie中设置名为 ".AspNetCore.Culture" Key 的值,值形如 c=zh-CN|uic=zh-CN
      • AcceptLanguageHeaderRequestCultureProvider:从请求头中设置 "Accept-Language" 的值

    • 通过AddInitialRequestCultureProvider添加自定义RequestCultureProvider,可以通过委托传入解析逻辑,也可以继承RequestCultureProvider抽象类来编写更复杂的逻辑。

  • 可以通过 Nuget 包My.Extensions.Localization.Json将资源文件(.resx)更换为 Json 文件。

来源:https://www.cnblogs.com/xiaoxiaotank/archive/2023/06/12/17466952.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具