Search code examples
c#asp.net-coreasp.net-core-mvcasp.net-core-8

Localizing shared views in ASP.NET Core 8 MVC


I am trying to localize my ASP.NET Core 8 MVC application. I have followed all the steps but I am unable to get my shared views localized.

Here is my Program.cs:

using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Localization;
using System.Globalization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddLocalization(opt => opt.ResourcesPath = "Localization");
builder.Services.AddControllersWithViews()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .AddDataAnnotationsLocalization();

string[] supportedLanguages = ["en", "jp"];
IList<CultureInfo> cultures = new List<CultureInfo>();
foreach(string lang in supportedLanguages)
{
    cultures.Add(new CultureInfo(lang));
}

builder.Services.Configure<RequestLocalizationOptions>(opt =>{
    opt.DefaultRequestCulture = new RequestCulture(supportedLanguages[0]);
    opt.SupportedCultures = cultures;
});

var app = builder.Build();
app.UseRequestLocalization();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Views/ViewImports.cshtml:

@using newApp
@using newApp.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

I have my resource files in Localization folder the structure is as follows:

Localization > Views > Shared > _Layout.en.resx
Localization > Views > Shared > _Layout.jp.resx

I am using the following code for invoking the Localizing @Localizer["search"] - here is my action in HomeController:

public IActionResult Lang(string? culture)
{
    if (culture != null)
    {
        Response.Cookies.Append(CookieRequestCultureProvider.DefaultCookieName, 
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), 
            new CookieOptions{ Expires = DateTime.Now.AddYears(1) }
        );
    }

    string referer = Request.Headers["Referer"].ToString();

    if (string.IsNullOrEmpty(referer))
        referer = "/";

    return Redirect(referer);
}

I still can't see my application change languages. What is it that I am doing wrong?


Solution

  • First, culture jp cannot be recognized, ja is correct.

    Second, SupportedUICultures is needed.

    Program.cs

    using Microsoft.AspNetCore.Localization;
    using Microsoft.AspNetCore.Mvc.Razor;
    using System.Globalization;
    
    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddLocalization(opt => opt.ResourcesPath = "Localization");
    builder.Services.AddControllersWithViews()
        .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
        .AddDataAnnotationsLocalization();
    
    string[] supportedLanguages = ["en", "ja"];    //1.ja represents japanese
    IList<CultureInfo> cultures = new List<CultureInfo>();
    foreach (string lang in supportedLanguages)
    {
        cultures.Add(new CultureInfo(lang));
    }
    
    builder.Services.Configure<RequestLocalizationOptions>(opt => {
        opt.DefaultRequestCulture = new RequestCulture(supportedLanguages[0]);
        opt.SupportedCultures = cultures;
        opt.SupportedUICultures = cultures;    //2.supportedui needed
    });
    
    var app = builder.Build();
    app.UseRequestLocalization();
    
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    
    app.Run();
    

    enter image description here