Configuration
Localization
Translate twblazor components and set date and time formats: built in languages, your own .resx files, and options.Localization in AddTwBlazor.
Last updated
Overview
Dates and times are localized the same way. Month and day names, the first day of the week, 12 or 24 hour time and the order of day, month and year all follow the culture, and each one can be set in AddTwBlazor.
Text you pass to a component yourself, such as a Label or a column title, is rendered as it is. Translating it stays in your application, with whatever you already use for that.
Supported languages
CultureInfo.CurrentUICulture. A regional culture uses its language file, so es-MX and es-AR both use Spanish, ar-EG uses Arabic, and zh-HK uses Traditional Chinese. Wu Chinese is written in Simplified Chinese, so it is covered by zh-Hans. A language that is not in this table falls back to English.
| Language | Culture | Resource file | Translation |
|---|---|---|---|
| English | en |
TwBlazorStrings.resx |
Written by the maintainers |
| Chinese (Simplified, Mandarin) | zh-Hans |
TwBlazorStrings.zh-Hans.resx |
AI translated |
| Spanish | es |
TwBlazorStrings.es.resx |
AI translated |
| Hindi | hi |
TwBlazorStrings.hi.resx |
AI translated |
| Portuguese | pt |
TwBlazorStrings.pt.resx |
AI translated |
| Bengali | bn |
TwBlazorStrings.bn.resx |
AI translated |
| Russian | ru |
TwBlazorStrings.ru.resx |
AI translated |
| Japanese | ja |
TwBlazorStrings.ja.resx |
AI translated |
| Western Punjabi (Shahmukhi) | pa-Arab |
TwBlazorStrings.pa-Arab.resx |
AI translated |
| Turkish | tr |
TwBlazorStrings.tr.resx |
AI translated |
| Vietnamese | vi |
TwBlazorStrings.vi.resx |
AI translated |
| Chinese (Traditional, Cantonese) | zh-Hant |
TwBlazorStrings.zh-Hant.resx |
AI translated |
| Arabic, including Egyptian Arabic | ar |
TwBlazorStrings.ar.resx |
AI translated |
| Marathi | mr |
TwBlazorStrings.mr.resx |
AI translated |
| Telugu | te |
TwBlazorStrings.te.resx |
AI translated |
| Korean | ko |
TwBlazorStrings.ko.resx |
AI translated |
| Tamil | ta |
TwBlazorStrings.ta.resx |
AI translated |
| Urdu | ur |
TwBlazorStrings.ur.resx |
AI translated |
| Indonesian | id |
TwBlazorStrings.id.resx |
AI translated |
| German | de |
TwBlazorStrings.de.resx |
AI translated |
| French | fr |
TwBlazorStrings.fr.resx |
AI translated |
| Javanese | jv |
TwBlazorStrings.jv.resx |
AI translated |
| Persian | fa |
TwBlazorStrings.fa.resx |
AI translated |
| Italian | it |
TwBlazorStrings.it.resx |
AI translated |
| Polish | pl |
TwBlazorStrings.pl.resx |
AI translated |
Choose the language
In Blazor WebAssembly set the culture before the application runs. In a server application use request localization, so each user gets the culture of their request.
// Blazor WebAssembly: Program.cs
var culture = new CultureInfo("es");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
// Blazor Server: Program.cs
app.UseRequestLocalization(new RequestLocalizationOptions()
.SetDefaultCulture("en-GB")
.AddSupportedCultures("en-GB", "es", "ja")
.AddSupportedUICultures("en-GB", "es", "ja"));
To pin every component to one culture whatever the application is running under, set Culture on the options. It applies to both the text and the formatting.
builder.Services.AddTwBlazor(options =>
{
options.Localization.Culture = new CultureInfo("ja");
}, Theme.CreateTheme);
Blazor WebAssembly only downloads the language files for the culture the application starts with. Changing the language while it runs needs a page reload, and BlazorWebAssemblyLoadAllGlobalizationData has to be on for cultures outside the default set. This is how Blazor localization works in general and is not specific to twblazor.
Right to left languages
dir attribute: wrap your layout in a TwDirectionProvider, or set dir on the html element yourself.
@* MainLayout.razor *@
<TwDirectionProvider>
@Body
<TwToastProvider />
<TwDialogProvider />
</TwDirectionProvider>
To set the direction whatever the culture, use options.Localization.Direction.
builder.Services.AddTwBlazor(options =>
{
options.Localization.Direction = TextDirection.RightToLeft;
}, Theme.CreateTheme);Change or add text with your own resource files
.resx files to your own project: one neutral file, and one more for each language. The name of each entry is a key from TwStrings, such as Pagination_NextPage.
MyApp/
Resources/
TwBlazorStrings.resx the neutral file, used when no language file matches
TwBlazorStrings.nl.resx Dutch
TwBlazorStrings.sv.resx SwedishThen register the file family. Only the keys you want to change need to be there. Anything missing falls back to twblazor's own file for that language, then to English.
using System.Resources;
builder.Services.AddTwBlazor(options =>
{
options.Localization.AddResource(
new ResourceManager("MyApp.Resources.TwBlazorStrings", typeof(Program).Assembly));
}, Theme.CreateTheme);
A value with placeholders keeps them, so Page {0} of {1} becomes Pagina {0} van {1}. Text that counts something has a singular and a plural key, such as Sidebar_ResultsOne and Sidebar_ResultsOther.
<!-- Resources/TwBlazorStrings.nl.resx -->
<data name="Common_Close" xml:space="preserve">
<value>Sluiten</value>
</data>
<data name="Pagination_PageStatus" xml:space="preserve">
<value>Pagina {0} van {1}</value>
</data>
Several file families can be registered. They are searched last added first. The full list of keys, with the English text of each, is the TwStrings class and TwBlazorStrings.resx.
Change text on one component
A component parameter always wins over the resource files, so a single component can say something different from the rest of the application.
<TwSelect T="string" Values="@countries" Placeholder="Choose a country" />
<TwDatePicker @bind-SelectedDate="date" Format="yyyy-MM-dd" />
<TwTimePicker @bind-SelectedTime="time" Is12HourFormat="true" />Date and time formats
options.Localization.Formats. Every value is optional and is a .NET date and time format string. Anything you leave out comes from the culture, so most applications set nothing here.
When the application has no culture of its own (the invariant culture), twblazor uses British formats: a 24 hour clock, day before month, and weeks that start on Monday.
builder.Services.AddTwBlazor(options =>
{
options.Localization.Formats.FirstDayOfWeek = DayOfWeek.Sunday;
options.Localization.Formats.Use12HourClock = true;
options.Localization.Formats.LongDate = "dddd, d MMMM yyyy";
options.Localization.Formats.DateInput = "dd.MM.yyyy";
}, Theme.CreateTheme);| Option | Used for | Default |
|---|---|---|
FirstDayOfWeek |
The first column of calendars and date pickers | The culture's first day of the week |
Use12HourClock |
Whether times show AM and PM | The culture for displayed times, 24 hour for picker inputs |
ShortTime |
Event times and time slots | The culture's short time pattern |
Hour |
Hour labels beside the calendar's time grid | h tt or HH:mm, from the clock style |
LongDate |
The full date screen readers announce for a day | The culture's long date pattern |
MediumDate |
Date ranges in the calendar header and event summaries | d MMM yyyy |
MonthYear |
Month headings | The culture's year month pattern |
MonthDay |
The days a multi day event covers | d MMM |
Month, ShortMonth, Year |
Month and year buttons in the date pickers | MMMM, MMM, yyyy |
WeekdayDay |
Column headings in the calendar's week view | ddd d |
DateInput |
The format date pickers type and parse | The theme's DefaultFormat |
TimeInput |
The format time pickers type and parse | HH:mm, or hh:mm tt on a 12 hour clock |
DateTimeInput |
The format date time pickers type and parse | The theme's DefaultDateTimeFormat |
The picker input formats are the ones users type, so they stay fixed unless you change them: the date pickers use the theme's dd/MM/yyyy and the time pickers use a 24 hour clock, whatever the culture. To make the pickers type and parse the way a culture writes dates, copy its patterns in one call.
// For en-US this sets M/d/yyyy, h:mm tt and a 12 hour clock.
options.Localization.Formats.UseCultureInputFormats(new CultureInfo("en-US"));
A format can also live in a resource file, so each language carries its own. Add an entry named Format_ShortTime, Format_LongDate and so on to the language's file. A value set on the options wins over the resource file, which wins over the culture. A component parameter such as Format or Is12HourFormat wins over all three.
Use your own localizer
ITwLocalizer service. If your strings live somewhere other than .resx files, such as a database or an IStringLocalizer, register your own implementation and AddTwBlazor will leave it in place.
public class MyLocalizer(TwBlazorOptions options, IStringLocalizer<SharedResource> strings)
: TwLocalizer(options)
{
protected override string? GetString(string key, CultureInfo culture)
{
var text = strings[key];
return text.ResourceNotFound ? base.GetString(key, culture) : text.Value;
}
}
builder.Services.AddScoped<ITwLocalizer, MyLocalizer>();
builder.Services.AddTwBlazor(_ => { }, Theme.CreateTheme);The same service is available to your own components, with the same keys and formats.
@inject ITwLocalizer Localizer
<button aria-label="@Localizer[TwStrings.Common_Close]">×</button>
<span>@Localizer.Formats.Format(DateTime.Today, Localizer.Formats.LongDate)</span>Improve a translation
value of the entry and open a pull request. To add a language, copy TwBlazorStrings.resx to TwBlazorStrings.<culture>.resx and translate the values.
Keep the entry names and the {0} placeholders unchanged. The test suite checks every language file for unknown keys and missing placeholders, so a mistake there is caught before it ships.