I am working localization project and stuck in the middle when trying to read values from Resource file.
I have done all configurations as below.
Configure localization:
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
new CultureInfo("fr-FR")
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture(supportedCultures[0]);
options.SupportedCultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new CustomerCultureProvider());
})
Configure the ASP.NET Core middleware
app.UseRequestLocalization();
Custom request culture provider
public class CustomerCultureProvider : RequestCultureProvider
{
public override async Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
await Task.Yield();
return new ProviderCultureResult("de-DE");
}
}
Below is my project structure:
I am trying to read the Resource Key "Name" in API endpoint as below:
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
var name = Employees.Name;
return new string[] { name };
}
If I set the culture de-DF, I should get the German translated text but it is not giving as expected.
My question, How can we get keys based on the culture we set in the startup? and How can I set the Resource path as my resource files in another project?
You need using one resource string for multiple classes so you need to create empty SharedResource class then register in Startup.cs like this
Then you have to inject IViewLocalizer in your view like this
Please let me know if you still have problem