Localization doesn't work in .NET Core 6.0

649 views Asked by At

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.

1

There are 1 answers

1
Chen On

I made an example, you can refer to it.

Program.cs:

builder.Services
    .AddLocalization(options => options.ResourcesPath = "Resources");

builder.Services
    .AddControllersWithViews()
    .AddViewLocalization();

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.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});
//...
//brfore app.UseStaticFiles()
app.UseRequestLocalization(app.Services.GetRequiredService<IOptions<RequestLocalizationOptions>>().Value);
app.UseHttpsRedirection();
app.UseStaticFiles();
//...

Controller:

public class LocalizationController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult LocalizedView()
    {
        return View();
    }
}

LocalizedView.cshtml:

@using Microsoft.AspNetCore.Mvc.Localization

@{
    ViewData["Title"] = "IViewLocalizer";
}

@inject IViewLocalizer ViewLocalizer

<div class="text-center">
    <p>@ViewLocalizer["Information"]</p>
</div>

Finally add Resources, please pay attention to its structure, the name of the controller and view should be corresponding under the Resources/Views folder:

enter image description here

Test Result:

enter image description here

enter image description here