Lesson 30 of 40
Web Development
Intermediate
35 min
Globalization & Localization
Build world-ready applications with proper localization, resource files, culture-aware formatting, and RTL language support.
Part 1: Resource File Localization
// Resources/Messages.resx (default)
// Resources/Messages.ar.resx (Arabic)
// Resources/Messages.fr.resx (French)
builder.Services.AddLocalization(o => o.ResourcesPath = "Resources");
// Inject IStringLocalizer<T>
_localizer["WelcomeMessage"]
// Resources/Messages.ar.resx (Arabic)
// Resources/Messages.fr.resx (French)
builder.Services.AddLocalization(o => o.ResourcesPath = "Resources");
// Inject IStringLocalizer<T>
_localizer["WelcomeMessage"]
Part 2: Request Localization Middleware
app.UseRequestLocalization(new RequestLocalizationOptions
{
SupportedCultures = ["en", "fr", "ar", "zh"],
SupportedUICultures = ["en", "fr", "ar", "zh"],
DefaultRequestCulture = new("en")
});
{
SupportedCultures = ["en", "fr", "ar", "zh"],
SupportedUICultures = ["en", "fr", "ar", "zh"],
DefaultRequestCulture = new("en")
});
Part 3: Culture-Aware Formatting
// Always pass culture explicitly in libraries
var price = (1234.56m).ToString("C", new CultureInfo("de-DE"));
// Result: 1.234,56 โฌ
var date = DateTime.Now.ToString("D", new CultureInfo("ja-JP"));
// Result: 2026ๅนด3ๆ11ๆฅ
var price = (1234.56m).ToString("C", new CultureInfo("de-DE"));
// Result: 1.234,56 โฌ
var date = DateTime.Now.ToString("D", new CultureInfo("ja-JP"));
// Result: 2026ๅนด3ๆ11ๆฅ
Part 4: RTL Support in Blazor/Razor
<!-- Set direction based on culture -->
<html lang="@CultureInfo.CurrentUICulture.Name"
dir="@(IsRtl ? "rtl" : "ltr")">
@code {
bool IsRtl => CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;
}
<html lang="@CultureInfo.CurrentUICulture.Name"
dir="@(IsRtl ? "rtl" : "ltr")">
@code {
bool IsRtl => CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;
}