前言

最近的新型冠状病毒流行让很多人主动在家隔离,希望疫情能快点消退。武汉加油,中国必胜!

Asp.Net Core 提供了内置的网站国际化(全球化与本地化)支持,微软还内置了基于 resx 资源字符串的国际化服务组件。可以在入门教程中找到相关内容。

但是内置实现方式有一个明显缺陷,resx 资源是要静态编译到程序集中的,无法在网站运行中临时编辑,灵活性较差。幸好我找到了一个基于数据库资源存储的组件,这个组件完美解决了 resx 资源不灵活的缺陷,经过适当的设置,可以在第一次查找资源时顺便创建数据库记录,而我们要做的就是访问一次相应的网页,让组件创建好记录,然后我们去编辑相应的翻译字段并刷新缓存即可。

但是!又是但是,经过一段时间的使用,发现基于数据库的方式依然存在缺陷,开发中难免有需要删除并重建数据库,初始化环境。这时,之前辛辛苦苦编辑的翻译就会一起灰飞烟灭 (╯‵□′)╯︵┻━┻ 。而 resx 资源却完美避开了这个问题,这时我就在想,能不能让他们同时工作,兼顾灵活性与稳定性,鱼与熊掌兼得。

经过一番摸索,终于得以成功,在此开贴记录分享。

正文

设置并启用国际化服务组件

安装 Nuget 包 Localization.SqlLocalizer,这个包依赖 EF Core 进行数据库操作。然后在 Startup 的 ConfigureServices 方法中加入以下代码注册  EF Core 上下文:

 services.AddDbContext<LocalizationModelContext>(options =>
     {
         options.UseSqlServer(connectionString);
     },
     ServiceLifetime.Singleton,
     ServiceLifetime.Singleton);

注册自制的混合国际化服务:

services.AddMixedLocalization(opts => { opts.ResourcesPath = "Resources"; }, options => options.UseSettings(true, false, true, true));

注册请求本地化配置:

 services.Configure<RequestLocalizationOptions>(
     options =>
     {
         var cultures =  Configuration.GetSection("Internationalization").GetSection("Cultures")
         .Get<List<string>>()
         .Select(x => new CultureInfo(x)).ToList();
         var supportedCultures = cultures;

         var defaultRequestCulture = cultures.FirstOrDefault() ?? new CultureInfo("zh-CN");
         options.DefaultRequestCulture = new RequestCulture(culture: defaultRequestCulture, uiCulture: defaultRequestCulture);
         options.SupportedCultures = supportedCultures;
         options.SupportedUICultures = supportedCultures;
     });

注册 MVC 本地化服务:

 services.AddMvc()
     //注册视图本地化服务
     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, opts => { opts.ResourcesPath = "Resources"; })
     //注册数据注解本地化服务
     .AddDataAnnotationsLocalization();

在 appsettings.json 的根对象节点添加属性:

"Internationalization": {
  "Cultures": [
    "zh-CN",
    "en-US"
  ]
}

在某个控制器加入以下动作:

 public IActionResult SetLanguage(string lang)
 {
     var returnUrl = HttpContext.RequestReferer() ?? "/Home";

     Response.Cookies.Append(
         CookieRequestCultureProvider.DefaultCookieName,
         CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(lang)),
         ) }
     );

     return Redirect(returnUrl);
 }

准备一个页面调用这个动作切换语言。然后,大功告成!

这个自制服务遵循以下规则:优先查找基于 resx 资源的翻译数据,如果找到则直接使用,如果没有找到,再去基于数据库的资源中查找,如果找到则正常使用,如果没有找到则按照对服务的配置决定是否在数据库中生成记录并使用。

自制混合国际化服务组件的实现

本体:

 public interface IMiscibleStringLocalizerFactory : IStringLocalizerFactory
 {
 }

 public class MiscibleResourceManagerStringLocalizerFactory : ResourceManagerStringLocalizerFactory, IMiscibleStringLocalizerFactory
 {
     public MiscibleResourceManagerStringLocalizerFactory(IOptions<LocalizationOptions> localizationOptions, ILoggerFactory loggerFactory) : base(localizationOptions, loggerFactory)
     {
     }
 }

 public class MiscibleSqlStringLocalizerFactory : SqlStringLocalizerFactory, IStringExtendedLocalizerFactory, IMiscibleStringLocalizerFactory
 {
     public MiscibleSqlStringLocalizerFactory(LocalizationModelContext context, DevelopmentSetup developmentSetup, IOptions<SqlLocalizationOptions> localizationOptions) : base(context, developmentSetup, localizationOptions)
     {
     }
 }

 public class MixedStringLocalizerFactory : IStringLocalizerFactory
 {
     private readonly IEnumerable<IMiscibleStringLocalizerFactory> _localizerFactories;
     private readonly ILogger<MixedStringLocalizerFactory> _logger;

     public MixedStringLocalizerFactory(IEnumerable<IMiscibleStringLocalizerFactory> localizerFactories, ILogger<MixedStringLocalizerFactory> logger)
     {
         _localizerFactories = localizerFactories;
         _logger = logger;
     }

     public IStringLocalizer Create(string baseName, string location)
     {
         return new MixedStringLocalizer(_localizerFactories.Select(x =>
         {
             try
             {
                 return x.Create(baseName, location);
             }
             catch (Exception ex)
             {
                 _logger.LogError(ex, ex.Message);
                 return null;
             }
         }));
     }

     public IStringLocalizer Create(Type resourceSource)
     {
         return new MixedStringLocalizer(_localizerFactories.Select(x =>
         {
             try
             {
                 return x.Create(resourceSource);
             }
             catch (Exception ex)
             {
                 _logger.LogError(ex, ex.Message);
                 return null;
             }
         }));
     }
 }

 public class MixedStringLocalizer : IStringLocalizer
 {
     private readonly IEnumerable<IStringLocalizer> _stringLocalizers;

     public MixedStringLocalizer(IEnumerable<IStringLocalizer> stringLocalizers)
     {
         _stringLocalizers = stringLocalizers;
     }

     public virtual LocalizedString this[string name]
     {
         get
         {
             var localizer = _stringLocalizers.SingleOrDefault(x => x is ResourceManagerStringLocalizer);
             var result = localizer?[name];
             if (!(result?.ResourceNotFound ?? true)) return result;

             localizer = _stringLocalizers.SingleOrDefault(x => x is SqlStringLocalizer) ?? throw new InvalidOperationException($"没有找到可用的 {nameof(IStringLocalizer)}");
             result = localizer[name];
             return result;
         }
     }

     public virtual LocalizedString this[string name, params object[] arguments]
     {
         get
         {
             var localizer = _stringLocalizers.SingleOrDefault(x => x is ResourceManagerStringLocalizer);
             var result = localizer?[name, arguments];
             if (!(result?.ResourceNotFound ?? true)) return result;

             localizer = _stringLocalizers.SingleOrDefault(x => x is SqlStringLocalizer) ?? throw new InvalidOperationException($"没有找到可用的 {nameof(IStringLocalizer)}");
             result = localizer[name, arguments];
             return result;
         }
     }

     public virtual IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
     {
         var localizer = _stringLocalizers.SingleOrDefault(x => x is ResourceManagerStringLocalizer);
         var result = localizer?.GetAllStrings(includeParentCultures);
         if (!(result?.Any(x => x.ResourceNotFound) ?? true)) return result;

         localizer = _stringLocalizers.SingleOrDefault(x => x is SqlStringLocalizer) ?? throw new InvalidOperationException($"没有找到可用的 {nameof(IStringLocalizer)}");
         result = localizer?.GetAllStrings(includeParentCultures);
         return result;
     }

     [Obsolete]
     public virtual IStringLocalizer WithCulture(CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

 public class MixedStringLocalizer<T> : MixedStringLocalizer, IStringLocalizer<T>
 {
     public MixedStringLocalizer(IEnumerable<IStringLocalizer> stringLocalizers) : base(stringLocalizers)
     {
     }

     public override LocalizedString this[string name] => base[name];

     public override LocalizedString this[string name, params object[] arguments] => base[name, arguments];

     public override IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
     {
         return base.GetAllStrings(includeParentCultures);
     }

     [Obsolete]
     public override IStringLocalizer WithCulture(CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

注册辅助扩展:

 public static class MixedLocalizationServiceCollectionExtensions
 {
     public static IServiceCollection AddMixedLocalization(
         this IServiceCollection services,
         Action<LocalizationOptions> setupBuiltInAction = null,
         Action<SqlLocalizationOptions> setupSqlAction = null)
     {
         if (services == null) throw new ArgumentNullException(nameof(services));

         services.AddSingleton<IMiscibleStringLocalizerFactory, MiscibleResourceManagerStringLocalizerFactory>();

         services.AddSingleton<IMiscibleStringLocalizerFactory, MiscibleSqlStringLocalizerFactory>();
         services.TryAddSingleton<IStringExtendedLocalizerFactory, MiscibleSqlStringLocalizerFactory>();
         services.TryAddSingleton<DevelopmentSetup>();

         services.TryAddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));

         services.AddSingleton<IStringLocalizerFactory, MixedStringLocalizerFactory>();

         if (setupBuiltInAction != null) services.Configure(setupBuiltInAction);
         if (setupSqlAction != null) services.Configure(setupSqlAction);

         return services;
     }
 }

原理简介

服务组件利用了 DI 中可以为同一个服务类型注册多个实现类型,并在构造方法中注入服务集合,便可以将注册的所有实现注入组件同时使用。要注意主控服务和工作服务不能注册为同一个服务类型,不然会导致循环依赖。 内置的国际化框架已经指明了依赖 IStringLocalizerFatory ,必须将主控服务注册为 IStringLocalizerFatory,工作服只能注册为其他类型,不过依然要实现 IStringLocalizerFatory,所以最方便的办法就是定义一个新服务类型作为工作服务类型并继承 IStringLocalizerFatory。

想直接体验效果的可以到文章底部访问我的 Github 下载项目并运行。

结语

这个组件是在计划集成 IdentityServer4 管理面板时发现那个组件使用了 resx 的翻译,而我的现存项目已经使用了数据库翻译存储,两者又不相互兼容的情况下产生的想法。

当时 Localization.SqlLocalizer 旧版本(2.0.4)还存在无法在视图本地化时正常创建数据库记录的问题,也是我调试修复了 bug 并向原作者提交了拉取请求,原作者也在合并了我的修复后发布了新版本。

这次在集成 IdentityServer4 管理面板时又发现了 bug,正准备联系原作者看怎么处理。

转载请完整保留以下内容并在显眼位置标注,未经授权删除以下内容进行转载盗用的,保留追究法律责任的权利!

  本文地址:https://www.cnblogs.com/coredx/p/12271537.html

  完整源代码:Github

  里面有各种小东西,这只是其中之一,不嫌弃的话可以Star一下。

Asp.Net Core 混合全球化与本地化支持的更多相关文章

  1. 微软推出ASP.NET Core 2.0,并支持更新Visual Studio 2017

    微软推出ASP.NET Core 2.0的一般可用性,并发布.NET Core 2.0.该公司还推出了其旗舰集成开发环境(IDE)的更新:Visual Studio 2017版本15.3和Visual ...

  2. 体验 ASP.NET Core 中的多语言支持(Localization)

    首先在 Startup 的 ConfigureServices 中添加 AddLocalization 与 AddViewLocalization 以及配置 RequestLocalizationOp ...

  3. Asp.net Core 2.1 Kestrel 现在支持 多协议处理(Tcp)

    地址:https://github.com/davidfowl/MultiProtocolAspNetCore.git 在一个Kestrel服务上可以同时处理Tcp,Http,Https等多种协议. ...

  4. Asp.Net Core IdentityServer4 管理面板集成

    前言 IdentityServer4(以下简称 Id4) 是 Asp.Net Core 中一个非常流行的 OpenId Connect 和 OAuth 2.0 框架,可以轻松集成到 Asp.Net C ...

  5. ASP.NET Core 中文文档 第三章 原理(6)全球化与本地化

    原文:Globalization and localization 作者:Rick Anderson.Damien Bowden.Bart Calixto.Nadeem Afana 翻译:谢炀(Kil ...

  6. Jexus 5.8.2 正式发布为Asp.Net Core进入生产环境提供平台支持

    Jexus 是一款运行于 Linux 平台,以支持  ASP.NET.PHP 为特色的集高安全性和高性能为一体的 WEB 服务器和反向代理服务器.最新版 5.8.2 已经发布,有如下更新: 1,现在大 ...

  7. 一劳永逸:域名支持通配符,ASP.NET Core中配置CORS更轻松

    ASP.NET Core 内置了对 CORS 的支持,使用很简单,只需先在 Startup 的 ConfigureServices() 中添加 CORS 策略: public void Configu ...

  8. 在ASP.NET Core中如何支持每个租户数据存储策略的数据库

    在ASP.NET Core中如何支持每个租户数据存储策略的数据库 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: ht ...

  9. 一劳永逸:域名支持通配符,ASP.NET Core中配置CORS

    ASP.NET Core 内置了对 CORS 的支持,使用很简单,只需先在 Startup 的 ConfigureServices() 中添加 CORS 策略: public void Configu ...

随机推荐

  1. ELK学习实验005:beats的一些工具介绍

    一 背景需求 Nginx是一个非常优秀的web服务器,往往Nginx服务会作为项目的访问入口,那么,nginx的性能保障就会变得非常重要,如果nginx的运行出现了问题就会对项目有较大的影响,所以,我 ...

  2. springboot 文件上传及java使用post请求模拟文件上传

    参考自:https://blog.csdn.net/qq_25958999/article/details/83988974 接收端Controller类中方法: @RequestMapping(va ...

  3. 处理样本不平衡的LOSS—Focal Loss

    0 前言 Focal Loss是为了处理样本不平衡问题而提出的,经时间验证,在多种任务上,效果还是不错的.在理解Focal Loss前,需要先深刻理一下交叉熵损失,和带权重的交叉熵损失.然后我们从样本 ...

  4. Hello2020(前四题题解)

    Hello,2020!新的一年从快乐的掉分开始…… 我在m3.codeforces.com这个镜像网站中一开始还打不开D题,我…… 还有话说今天这场为什么那么多二分. 比赛传送门:https://co ...

  5. 如何在Pypi发布上传你自己的Python库

    Pypi上传包 准备 Windows环境 *以下教程只能在Windows上执行,MACOS系统不能使用 注册账号 你需要在Pypi上注册一个账号. 安装必要的库 setuptools 原则上安装了pi ...

  6. U盘中了蠕虫病毒,文件夹都变成exe了,怎么办?

    昨天做实验,用U盘拷了实验室的文件,然后就中了病毒了(无奈),U盘里的文件全都变成了exe.有点慌张,我的U盘里存了很多课程资料.然而,我懒得下载杀毒软件.参考这位博主的做法,我成功的找回了我隐藏的文 ...

  7. 推荐中的多任务学习-YouTube视频推荐

    本文将介绍Google发表在RecSys'19 的论文<Recommending What Video to Watch Next: A Multitask Ranking System> ...

  8. 机器学习实战笔记(一)- 使用SciKit-Learn做回归分析

    一.简介 这次学习的书籍主要是Hands-on Machine Learning with Scikit-Learn and TensorFlow(豆瓣:https://book.douban.com ...

  9. 解释一下什么是 aop?(未完成)

    解释一下什么是 aop?(未完成)

  10. Could not find a version that satisfies the requirement numpy>=1.7.0 (from pan das==0.17.0) (from versions: ) No matching distribution found for numpy>=1.7.0 (from pandas==0.17.0)

    今天晚上一直在安装pandas,天杀的,真的是太难了.后来发现提示: Could not find a version that satisfies the requirement numpy> ...