Skip to content

Commit dfa50e0

Browse files
authored
Localization for Blazor WASM (#17569)
1 parent cb945fe commit dfa50e0

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

aspnetcore/blazor/globalization-localization.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to make Razor components accessible to users in multiple
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 02/12/2020
8+
ms.date: 04/02/2020
99
no-loc: [Blazor, SignalR]
1010
uid: blazor/globalization-localization
1111
---
@@ -52,6 +52,18 @@ The following field types have specific formatting requirements and aren't curre
5252

5353
## Localization
5454

55+
### Blazor WebAssembly
56+
57+
Blazor WebAssembly apps set the culture using the user's [language preference](https://developer.mozilla.org/docs/Web/API/NavigatorLanguage/languages).
58+
59+
To explicitly configure the culture, set `CultureInfo.DefaultThreadCurrentCulture` and `CultureInfo.DefaultThreadCurrentUICulture` in `Program.Main`.
60+
61+
By default, Blazor's linker configuration for Blazor WebAssembly apps strips out internationalization information except for locales explicitly requested. For more information and guidance on controlling the linker's behavior, see <xref:host-and-deploy/blazor/configure-linker#configure-the-linker-for-internationalization>.
62+
63+
While the culture that Blazor selects by default might be sufficient for most users, consider offering a way for users to specify their preferred locale. For a Blazor WebAssembly sample app with a culture picker, see the [LocSample](https://github.com/pranavkm/LocSample) localization sample app.
64+
65+
### Blazor Server
66+
5567
Blazor Server apps are localized using [Localization Middleware](xref:fundamentals/localization#localization-middleware). The middleware selects the appropriate culture for users requesting resources from the app.
5668

5769
The culture can be set using one of the following approaches:
@@ -61,19 +73,15 @@ The culture can be set using one of the following approaches:
6173

6274
For more information and examples, see <xref:fundamentals/localization>.
6375

64-
### Configure the linker for internationalization (Blazor WebAssembly)
65-
66-
By default, Blazor's linker configuration for Blazor WebAssembly apps strips out internationalization information except for locales explicitly requested. For more information and guidance on controlling the linker's behavior, see <xref:host-and-deploy/blazor/configure-linker#configure-the-linker-for-internationalization>.
67-
68-
### Cookies
76+
#### Cookies
6977

7078
A localization culture cookie can persist the user's culture. The cookie is created by the `OnGet` method of the app's host page (*Pages/Host.cshtml.cs*). The Localization Middleware reads the cookie on subsequent requests to set the user's culture.
7179

7280
Use of a cookie ensures that the WebSocket connection can correctly propagate the culture. If localization schemes are based on the URL path or query string, the scheme might not be able to work with WebSockets, thus fail to persist the culture. Therefore, use of a localization culture cookie is the recommended approach.
7381

7482
Any technique can be used to assign a culture if the culture is persisted in a localization cookie. If the app already has an established localization scheme for server-side ASP.NET Core, continue to use the app's existing localization infrastructure and set the localization culture cookie within the app's scheme.
7583

76-
The following example shows how to set the current culture in a cookie that can be read by the Localization Middleware. Create a *Pages/Host.cshtml.cs* file with the following contents in the Blazor Server app:
84+
The following example shows how to set the current culture in a cookie that can be read by the Localization Middleware. Create a *Pages/_Host.cshtml.cs* file with the following contents in the Blazor Server app:
7785

7886
```csharp
7987
public class HostModel : PageModel
@@ -99,9 +107,9 @@ Localization is handled by the app in the following sequence of events:
99107
1. The Localization Middleware reads the cookie and assigns the culture.
100108
1. The Blazor Server session begins with the correct culture.
101109

102-
### Provide UI to choose the culture
110+
#### Provide UI to choose the culture
103111

104-
To provide UI to allow a user to select a culture, a *redirect-based approach* is recommended. The process is similar to what happens in a web app when a user attempts to access a secure resource&mdash;the user is redirected to a sign-in page and then redirected back to the original resource.
112+
To provide UI to allow a user to select a culture, a *redirect-based approach* is recommended. The process is similar to what happens in a web app when a user attempts to access a secure resource. The user is redirected to a sign-in page and then redirected back to the original resource.
105113

106114
The app persists the user's selected culture via a redirect to a controller. The controller sets the user's selected culture into a cookie and redirects the user back to the original URI.
107115

0 commit comments

Comments
 (0)