Globalization and Localization
Introduction
Globalization and Localization are crucial for developing applications that can be used in multiple cultures and languages. Globalization is the process of designing applications that support different cultures, while Localization is the process of translating the application to a specific culture.
Setting Up Globalization and Localization
To set up Globalization and Localization in a .NET application, follow these steps:
Step 1: Add Localization Resources
Add resource files to your project for each language you want to support. These files typically have a .resx extension and are named according to the culture they support (e.g., Resources.en-US.resx
, Resources.fr-FR.resx
).
Welcome
Hello, how are you?
Bienvenue
Bonjour, comment ça va?
Step 2: Configure Localization in Startup
Configure the localization services in the Startup.cs
file:
public void ConfigureServices(IServiceCollection services) {
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.Configure(options => {
var supportedCultures = new[] { "en-US", "fr-FR" };
options.DefaultRequestCulture = new RequestCulture("en-US");
options.SupportedCultures = supportedCultures.Select(c => new CultureInfo(c)).ToList();
options.SupportedUICultures = supportedCultures.Select(c => new CultureInfo(c)).ToList();
});
services.AddControllersWithViews()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
var supportedCultures = new[] { "en-US", "fr-FR" };
var localizationOptions = new RequestLocalizationOptions {
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures.Select(c => new CultureInfo(c)).ToList(),
SupportedUICultures = supportedCultures.Select(c => new CultureInfo(c)).ToList()
};
app.UseRequestLocalization(localizationOptions);
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
Step 3: Use Localized Resources in Views
To use localized resources in your views, inject the IStringLocalizer
service and retrieve the localized values:
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<Resources> Localizer
<div>
<h1>@Localizer["Welcome"]</h1>
<p>@Localizer["Greeting"]</p>
</div>
Using Localized Resources in Controllers
You can also use localized resources in your controllers by injecting the IStringLocalizer
service:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
public class HomeController : Controller {
private readonly IStringLocalizer<Resources> _localizer;
public HomeController(IStringLocalizer<Resources> localizer) {
_localizer = localizer;
}
public IActionResult Index() {
ViewData["Message"] = _localizer["Welcome"];
return View();
}
}
Changing the Current Culture
To allow users to change the current culture, you can create an action method that sets the culture and redirects to the previous page:
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
public class CultureController : Controller {
public IActionResult SetCulture(string culture, string returnUrl) {
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
}
Conclusion
Globalization and Localization are essential for building applications that can be used by a global audience. By following the steps outlined in this tutorial, you can easily configure and use globalization and localization features in your .NET applications.