Localization of data annotations in a separate class library

We are trying to implement localization for our domain models that exist in a separate class library project in our solution. However, we cannot make it work, since the annotation attributes of these models are not translated at all.

Project structure

  • Decision
    • Web project
      • Resource folder (contains .resx. Ex. App.en.resx files) Works fine
    • Class library
      • Domain Models
      • Resource folder (contains .resx. Ex. App.en.resx files) Doesn't work

Startup.cs

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

Note

Localization works as part of a web project, for example, translates views, controllers. However, this does not work when we try to translate models that exist in a separate project.

//Yours faithfully

+3
source share
1 answer

There is no support for translating annotations, views, controller, etc. that exist in a separate project, but without its implementation.

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

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

+1
source

Source: https://habr.com/ru/post/1240629/


All Articles