Internationalization, usually referred to by the word i18n, is the process of enabling your website to display in different languages and different formats, like date format and number format. L10n is the process of translating from one language to another with its national formats like date format and number format. In this tutorial, you will discover:
You can download the example code used in this topic on GitHub.
Blazor WebAssembly identifies a region by CultureInfo.DefaultThreadCurrentCulture
and CultureInfo.DefaultThreadCurrentUICulture
. When you set a CultureInfo
to both properties, the formats of dates, times, numbers and currencies will be changed accordingly.
Before you begin, you need to install Microsoft.Extensions.Localization
. You will need more packages based on your approach.
There are 2 approaches for internationalization of your website:
A user can provide their preferred language in many ways:
There maybe a confliction between those ways. For example, a user may have entered your website with the language "en-US" in their URL, but their browser is set to French. Another example would be a user visits your website and select "en-US", then you store the language in their local storage; Later on, they revisit your website with the "fr" language in their URL. In those cases, you need to select one language to display your website. It's your job to priority those language selection strategies.
A resource is a file with the content is a collection of keys and values for translation for a specific language. For example, your website has 3 languages and 2 components, then each component needs 4 resource files: A fallback resource and 3 resource files for 3 languages. In total, you will have 8 resource files (because you have 2 components and each component has 4 resource files). The resource files need to be loaded before you can display the translated text to the UI. There are 2 loading strategies for Blazor WebAssembly:
.resx
file extension. When building the website, .NET CLI will compile .resx
to .dll
file and embedded to the source code. For example, your website has 10 components in 3 languages and the user requested 1 component, you will have to load all 40 resource files. This strategy can be used with any translation approach..json
and .yml
. For example, your website has 10 components in 3 languages and the user requested 1 component, you will have to load 1 resource files. This strategy can only be used with the Instant Translation approach.Microsoft.Extensions.Localization
.csproj.cs
file. Add the following code to the csproj.cs
:<PropertyGroup> ... <BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData> </PropertyGroup>
Here is how to edit your csproj.cs
file: Right click your project and Edit Project File.
IStringLocalizer<T>
with T
is your component type. For example, in your ChangeLanguageDemonstrate.razor
:@inject IStringLocalizer<ChangeLanguageDemonstrate> Localizer
IStringLocalizer
to serve the text in multiple language. For example:@Localizer["String1"]
The String1
in the above example is your resource key.
You can pass the parameter to the localizer as well. To use the parameter, you need to put a placeholder in the translated text. For example: "Hello {0}, {1}"
. Then you can pass parameters to replace the placeholder like this @Localizer["String1", "Blazor School", "Blazor School Books"]
.