ASP.NET Core MVC Advanced

Globalization and localization

If your users speak different languages or format dates and money differently, localization lets one app serve them all. This lesson covers cultures, resource files, IStringLocalizer and IViewLocalizer, and how the request culture is chosen.

How a page is shown in the user's language
  1. StartRequest arrivesWith ?culture=fr, a culture cookie or an Accept-Language header
  2. Step 2Culture is chosenRequest localization middleware picks a supported culture
  3. Step 3Culture is appliedCurrentCulture formats numbers and dates; CurrentUICulture picks text
  4. Step 4Text is looked upIStringLocalizer finds the key in the matching .resx file
  5. Step 5FallbackIf no translation exists, the key itself is shown
  6. EndPage is renderedText, dates and currency match the user's culture

The idea in plain words

Globalization means making the app able to support many cultures; localization means adding the actual translations. In .NET each request has a culture (for formatting 1,234.50 or 1.234,50 and dates) and a UI culture (for the language of text). The request localization middleware sets both from the query string, a cookie or the browser's Accept-Language header.

Translations live in .resx resource files named after the class or view and the culture, such as Resources/Views/Home/Index.fr.resx.

Example: setup

C#
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddControllersWithViews()
    .AddViewLocalization()
    .AddDataAnnotationsLocalization();

var app = builder.Build();

var cultures = new[] { "en-IN", "hi-IN", "fr-FR" };
app.UseRequestLocalization(new RequestLocalizationOptions()
    .SetDefaultCulture(cultures[0])
    .AddSupportedCultures(cultures)
    .AddSupportedUICultures(cultures));

Example: using localizers

Razor
// Controller
public class HomeController : Controller
{
    private readonly IStringLocalizer<HomeController> _t;
    public HomeController(IStringLocalizer<HomeController> t) => _t = t;

    public IActionResult Index()
    {
        ViewData["Message"] = _t["Welcome to our shop"];
        return View();
    }
}

@* View *@
@inject Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer Localizer
<h1>@Localizer["Today's offers"]</h1>
<p>@DateTime.Now.ToString("D")</p>

Common mistakes

  • Concatenating translated fragments; word order differs between languages. Translate whole sentences with placeholders.
  • Calling UseRequestLocalization after middleware that already formats output.
  • Forgetting to list a culture in the supported cultures, so it is silently ignored.

Interview question

What is the difference between CurrentCulture and CurrentUICulture in .NET?

CurrentCulture controls culture-sensitive formatting and parsing, such as dates, numbers and currency. CurrentUICulture controls which localized resources are loaded, meaning the language of the text. They are usually the same, but can differ, for example English text with Indian number formatting.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.