I am working on an ASP.NET project and I need to implement localization of messages on the backend. I want to use two resource files (one for English and one for Russian) that will be common for all classes in the solution, and also use the localizer in my controllers as follows: localizer["message"]
. I have configured Startup.cs:
public void ConfigureServices(IServiceCollection services)
...
// Localization
services.AddControllers().AddDataAnnotationsLocalization();
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { "ru", "en" };
options.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures);
});
...
Primitive structure:
Solution
│
├── Web
│ ├── Startup.cs
│ ├── Program.cs
│ ├── SharedResource.cs
│ │ Controllers
│ │ └── ...
│ ├── Resources
│ │ ├── SharedResource.en.resx
│ │ ├── SharedResource.ru.resx
│ └── ...
└── Other projects
I also created a separate class SharedResource
accordingly in the controller it looks like this IStringLocalizer<SharedResource> localizer
. I have two files SharedResources.ru.resx and SharedResource.en.resx which are located in the Resources folder.
And the whole problem is that when I send a request to the API, it returns not the message, but the key specified in localizer["message"]
, i.e., message. As I understand it, this happens when the resource file is not found.
And I don’t understand why this is happening. Thanks in advance!
Create localization in your project so that localized messages come from the backend, and you only receive the key.
Miclell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.