数据注释在单独的类库中的本地化 [英] Localization of data annotations in separate class library

查看:97
本文介绍了数据注释在单独的类库中的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试为我们的解决方案中单独的类库项目中存在的域模型实现本地化。
但是,由于模型数据注释属性根本无法翻译,因此我们无法使其正常工作。

We are trying to implement localization for our domain models which are existing in a separate class library project within our solution. However, we are not able to get it working as our models data annotation attributes doesn't get translated at all.

项目结构


  • 解决方案


    • Web项目


      • 资源文件夹(包含.resx文件。 Ex。App.en.resx )工作正常

      • Solution
        • Web project
          • Resource folder (Contains .resx files. Ex. App.en.resx) Works fine

          • 域模型

          • 资源文件夹(包含.resx文件。 Ex。App.en.resx )不起作用

          • Domain models
          • Resource folder (Contains .resx files. Ex. App.en.resx) Doesn't work

          Startup.cs

          services.AddMvc()
               .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
               .AddDataAnnotationsLocalization();
          

          注意

          本地化在Web项目中起作用,例如,它翻译视图,控制器。
          但是,当我们尝试转换存在于一个单独项目中的模型时,它不起作用。

          Localization works within the Web project, e.g it translates views, controllers. However, it doesn't work when we try to translate models which exists in a separate project.

          //问候

          推荐答案

          目前尚不支持在单独的项目中转换数据批注,视图,控制器等,而无需您自己实现。

          There is no support to translate data annotations, views, controller etc that exists in a separate project for now without implementing it by yourself.

          解决方案是使用IStringLocalizer,IStringLocalizerFactory编写自己的自定义实现,并将其注册到Startup.cs中。 查看StringLocalizer / Factory的工作原理。

          The solution is to write your own custom implementation using IStringLocalizer, IStringLocalizerFactory and register it in Startup.cs. See how StringLocalizer/Factory works.

          仅供参考:正在使用Localizer的aspnet团队已经意识到了这一点,并正在研究一种解决方案,以在将来支持此功能。参见 aspnet /本地化

          FYI: The aspnet team that is working on Localizer is aware of this and is working on a solution to support this in the future. See aspnet/Localization

          这篇关于数据注释在单独的类库中的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆