There are many ways to make your website more appealing to the users, internationalization (i18n) and localization (L10n) is one of the methods. 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.
A CultureInfo
identifies a region (such as a country) in which people speak a particular language or language variant. The CultureInfo
determines the formatting and parsing of dates, times, numbers and currencies as well as measurement units and the translated names for time zones, language and countries.
In Blazor Server, there are 2 types of i18n:
For each type, we have 3 methodologies for detecting users' preferred languages.
Accept-Language
of the request header.This guide provides information to help you decide which type of i18n works best for your situation and walks you through how to L10n your website.
Passive and reactive i18n manage the translation process differently. Each approach offers different advantages.
Before diving in on how to i18n your website, you first need to know how to L10n first. In other words, you first need to understand how Blazor Server is looking for a translation of a text.
For each available language on your website, there will be a language file. The language files have .resx
as the file extension, those files are called Resources File. The folder structure must be the same as the component/page folder. The following image will give you an idea about resources folder structure.
I18N.razor
and NavMenu.razor
so you will need to create a Pages
folder inside the Resources folder for the I18N.razor
component and a Shared
folder inside the Resources folder for the NavMenu.razor
component.I18N.razor
component you will name your default language file as I18N.resx
. Right click on the Pages
folder inside the Resources folder, select Add => New Item.... Look for the Resources File as the following picture.<ComponentName>.<LanguageCode>.resx
. You can check http://www.lingoes.net/en/translator/langcode.htm for language codes. In this example, the component will have 2 languages: English and French, so you will need to create a I18N.fr.resx
file.I18N.resx
you have a key with the name "Name" and it's value is "First name" so later on, when you use the key "Name" it will be translated to "First name" when the user selects English. You also want to use "No code generation" as the Access Modifier for all language files.Blazor Server support IStringLocalizer
for localize your website.
IStringLocalizer
with the current component as a generic parameter. You are currently in I18N.razor
so the generic would be I18N
.@using Microsoft.Extensions.Localization @inject IStringLocalizer<I18N> Localizer
Localizer
with the key you have created in the resource file. For example, @Localizer["Name"]
. The following picture will elaborate on how to use the Localizer
.