Globalization And Localization In ASP.NET 6

Last modified: April 08, 2022

Want to build a multiple languages website in ASP.NET 6?

In this tutorial, we are going to learn how to provide multiple languages support for an ASP.NET Application.

1. Create a MVC .NET 6 application

project project project project

2. Program.cs

  • en-US is American English
  • en-GB is British English
builder.Services.AddLocalization(opt => opt.ResourcesPath = "Resources"); //Resources folder builder.Services.AddMvc() .AddMvcLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization(); builder.Services.Configure<RequestLocalizationOptions>(opt => { var supportedCultures = new[] { "en-US", "en-GB" }; opt.SetDefaultCulture(supportedCultures[0]) .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); });


var supportedCultures = new[] { "en-US", "en-GB" }; var localizationOptions = new RequestLocalizationOptions().SetDefaultCulture(supportedCultures[0]) .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); app.UseRequestLocalization(localizationOptions);


3. Partial View (Language Switcher)

Create a partial view _LanguageSwitcher in Shared folder. The aim of this Shared view is to display the list of the Languages in the system as a dropdown list, which can switch.

@using Microsoft.AspNetCore.Builder @using Microsoft.AspNetCore.Localization @using Microsoft.AspNetCore.Mvc.Localization @using Microsoft.Extensions.Options @inject IOptions<RequestLocalizationOptions> LocOptions @{ var requestCulture = Context.Features.Get<IRequestCultureFeature>(); var cultureItems = LocOptions.Value.SupportedUICultures .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName }) .ToList(); var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}"; } <form asp-action="SetLanguage" asp-controller="Language" asp-route-returnUrl="@returnUrl" method="post"> <label>Language:</label> <select name="culture" onchange="this.form.submit()" asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"> </select> </form>


4. Langauge Switcher Controller

Create a new LanguageController to store the selected Language in the cookie.

public class LanguageController : Controller { [HttpPost] public IActionResult SetLanguage(string culture, string returnUrl) { Response.Cookies.Append( CookieRequestCultureProvider.DefaultCookieName, CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) } ); return LocalRedirect(returnUrl); } }

5. Resources

Create Resources folder add the resource files to it. The folder structure of resources files in Resources folder should match folder structure in View Page. A resource file key pair values project project project

6. How to use?

You need add Partial View (Language Switcher) into _layout page

@await Html.PartialAsync("_LanguageSwitcher")
@using Microsoft.AspNetCore.Mvc.Localization @inject IViewLocalizer Localizer @Localizer["Hello"] //Hello is key


7. Test