🟨 Slightly different to Blazor Server
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.
You can download the example code used in this topic on GitHub.
Multilingual works can be achieved in four steps:
CultureInfo.DefaultThreadCurrentCulture
and CultureInfo.DefaultThreadCurrentUICulture
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.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:
Resource loading strategies are an essential aspect of translation in Blazor. Resources typically contain collections of keys and values for a specific language, and a website with multiple languages and components will require multiple resource files to be loaded. There are 2 resource loading approaches for Blazor WebAssembly:
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.