I try to use ViewLocalizer in my ASP.NET Core 6 MVC project. But when I run the project I couldn't see any translation from my resources folder.
Here is my program.cs code:
builder.Services.AddControllersWithViews().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
builder.Services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("tr-TR"),
new CultureInfo("fr")
};
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedUICultures = supportedCultures;
options.SupportedCultures = supportedCultures;
});
...
app.UseRequestLocalization();
I installed Microsoft.AspNetCore.Localization package, created resources files under Resources/Views/Category/CurrentCategory.en-US.resx, Resources/Views/Category/CurrentCategory.tr-TR.resx and injected them in the related view with this code:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
And I called for example:
<h6 class="sub-title">@Localizer["Information"]</h6>
When I run the application, it appears in Turkish by default, although I set it to English and when I search as https://localhost:44338/Kategori/CurrentKategori/?culture=en-US, the result does not change.
How can I solve it? I want to use ViewLocalizer.
I made an example, you can refer to it.
Program.cs:
Controller:
LocalizedView.cshtml:
Finally add
Resources, please pay attention to its structure, the name of the controller and view should be corresponding under theResources/Viewsfolder:Test Result: