🟨 Slightly different to Blazor WebAssembly
If you're looking to expand your website's reach and cater to an international audience, creating a multilingual website is an excellent strategy. In this tutorial, we'll guide you through the process of building a Blazor application that can support multiple languages.
.resx
files for website localizationYou can download the example code used in this topic on GitHub.
Multilingual works can be achieved in four steps:
CultureInfo.CurrentCulture
and CultureInfo.CurrentUICulture
to determine the current region. Here is an example language tag, en-US: The en indicates that the language is English, and the US indicates that the language is used in America. You can find a list of language tags on Microsoft Learn. Although it is a rare case, you may need to create your own language and location, but that part is not included in this tutorial.Index.HelloWorld
or a flat format like HelloWorld
or even spread across multiple files. For the purpose of this tutorial, we will be using .resx resource files.The most important thing to keep in mind when creating a multilingual website are:
When creating a multilingual website, developers can use different methods to determine the user's preferred language, such as cookies, browser language, local storage, or the language in the URL. For example, if a user in France visits a website with their browser language set to French, the website can detect this and display the website content in French by default.
However, conflicts may arise when these methods produce different results for the user's preferred language. For instance, if a user's browser language is set to French, but they have previously selected English as their preferred language in the website's settings, there may be a conflict between the two methods.
To resolve such conflicts, developers can prioritize one method over others based on factors such as the reliability of the data source and the user's past language selection history. For instance, if the user has consistently chosen English as their preferred language in the website's settings, the website can prioritize that selection over their browser language.
There are 2 approaches to translation in Blazor:
.resx
files for website localizationA .resx file represents a Blazor component in a single language. For instance, if your MyBlazorComponent
supports both English and French, then you will have MyBlazorComponent.en.resx and MyBlazorComponent.fr.resx as resource files. There are two ways to organize resource files: centralized and distributed.
For centralized resource organization, all resource files are placed in the same folder as the component file, as illustrated in the following image:
To register resources for centralization, you can use the following code:
builder.Services.AddLocalization(); ... app.UseRequestLocalization();
Centralizing resources can pose maintenance challenges. To address this, resource files can be distributed into a separate folder from the component and the component folder tree can be reconstructed inside that folder, as shown in the following image:
To register resources for distributed organization, you can use the following code:
builder.Services.AddLocalization(options => options.ResourcesPath = "BlazorSchoolResources"); ... app.UseRequestLocalization();
In this case, the resource files are located in a folder called BlazorSchoolResources
which is separate from the component folder.
.resx
file structureTo create a resource file, you must adhere to the naming convention: <ComponentName>.<Language>.resx. For instance, if your component is named MyComponent
and you support French (fr) and English (en-US), then create two resource files named MyComponent.fr.resx and MyComponent.en-US.resx.
A resource is a collection of key-value pairs where the keys are consistent across all resource files and are used to retrieve the corresponding values that will be displayed to the user.
When working with Blazor Server, you'll need to use CultureInfo.CurrentCulture
and CultureInfo.CurrentUICulture
, rather than CultureInfo.DefaultThreadCurrentCulture
and CultureInfo.DefaultThreadCurrentUICulture
as you would with Blazor WebAssembly. Additionally, the project preparation steps are completely different.