How to Implement Localization and Globalization in ASP.NET Core 10 Using RESX Files and C# (2026)
An ASP.NET Core 10 application can select a supported culture with request localization middleware and look up translated strings in RESX files. This lab shows an API that accepts an explicit language choice, persists it in a cookie, and returns a localized quote and validation message. You can use the same request culture and resource keys in Razor.
How do localization and globalization fit together?
Localization supplies translated text. Globalization makes dates, numbers, and other values display according to the selected culture. ASP.NET Core maintains both CurrentCulture for formatting and CurrentUICulture for resource lookup. The sample sets both to the same supported locale. A system can intentionally choose separate values when users want one display language and another regional format.
| Input | Use | Tradeoff |
|---|---|---|
| Cookie | Persist an explicit visitor choice | Scope to the application and account for caching |
| Accept-Language | Select an initial preference | Browsers may send more than one language |
| Route or query | Support shareable localized URLs | Validate the supported set and design URL/canonical behavior |
Here the cookie has precedence over the browser header. The middleware runs before endpoint execution. If neither provider finds a supported culture, the application uses en-US. Authentication or storage decisions must not depend on a client-supplied locale.
Create the project and resources
Install the .NET 10 SDK. The sample uses the ASP.NET Core shared framework, so no additional package is required. Keep the marker type in the project root. If you add a namespace, match the resource path to the root namespace and type, or explicitly configure the root namespace as documented.
Add the following two resource files. Their keys are stable identifiers and their values are user-facing strings. The neutral file is the fallback. A typical project also runs a resource key check in CI, because fallback can hide an untranslated French string until users see English inside an otherwise localized page.
Configure the request and API behavior
The quote separates the currency code from the localized number. A French culture can format an amount in Euros or Dollars. It does not determine the invoice currency. For integration contracts, preserve the decimal amount and ISO currency code as machine-readable fields and offer formatted strings as a convenience for display.
The quantity endpoint returns a localized message for an out-of-range value. Framework-level failures such as malformed JSON happen before this handler and need a separate error localization policy if your contract promises translated errors. Clients should rely on field identifiers and status codes to decide what to do with a response. Display text can change without changing application logic.
Verify cookie precedence and localized validation
Start with dotnet run --urls http://localhost:5087. Run these commands in a second terminal.
Expect Content-Language: fr-FR and a French quote for both quote requests. The cookie-setting response is 204 and the invalid quantity response is 400 with a French error for quantity. Test an unknown language and verify the explicit switch returns 400 without replacing the existing cookie. French digit grouping can contain a Unicode space, so avoid assuming ordinary spaces in formatted output. Test en-US as well as the default path without either provider.
Use the same policy in Razor and production
In Razor, inject IStringLocalizer<Messages> with @inject and display the localized value through Razor’s normal encoding. Add a small language selector that makes a same-origin POST and refreshes the view after success. For applications that also set security-sensitive cookies or accept cross-site requests, apply the appropriate antiforgery protection and use HTTPS. With a reverse proxy, correctly configure trusted forwarded headers so the application can detect HTTPS.
The quote endpoint uses private, no-store caching. If your public localized pages should be cacheable, locale-specific paths often make cache keys and link sharing easier to reason about. If you use header negotiation instead, the caching policy must account for the header. A cookie-based variant must not accidentally share one visitor’s response with another. See the related ASP.NET Core Web API CRUD guide for an API where the same localized validation contract can be applied.
Why does the response contain a resource key?
Check that the RESX files are compiled as embedded resources and that their names match the localizer’s type. Missing keys can surface as key text. Check the neutral resource first, then the language-specific resource.
Why does my container show an invariant culture?
Check that globalization invariant mode is disabled and that your runtime image includes the required globalization support. Test the deployed image with both cultures.
Should culture selection accept any locale?
Use an explicit supported set. Fall back predictably and tell clients which language was selected. This keeps resource coverage and testing manageable.
Comments
Post a Comment