Localization of data annotations in separate class library

吃可爱长大的小学妹 提交于 2020-01-04 03:39:58

问题


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.

Project structure

  • Solution
    • Web project
      • Resource folder (Contains .resx files. Ex. App.en.resx) Works fine
    • Class library
      • Domain models
      • Resource folder (Contains .resx files. Ex. App.en.resx) Doesn't work

Startup.cs

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

Note

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.

// Regards


回答1:


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

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

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



来源:https://stackoverflow.com/questions/38630777/localization-of-data-annotations-in-separate-class-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!