Localization

Localization allows to provide custom translations for localizable text in the components.

Explanation

MudBlazor itself provides English language strings for texts found in e.g. the MudDataGrid filter options. By registering a custom MudLocalizer implementation as a Service, you can provide custom translations.

Register Service

Add the following in Program.cs to register your custom localization service. AddTransient can be replaced with TryAddTransient and the scope can be changed to Scoped or Singleton depending on your exact implementation.

using Microsoft.Extensions.DependencyInjection;
using MudBlazor;

builder.Services.AddTransient<MudLocalizer, CustomMudLocalizerImpl>();
ResX Example

An example MudLocalizer implementation using Microsofts default IStringLocalizer. Using ResX, you'll have to leave the default culture translations file empty if you want to use English as the fallback language for missing translations. Otherwise the default culture values will be used as a fallback for all non-English languages.

using Microsoft.Extensions.Localization;
using MudBlazor;

internal class ResXMudLocalizer : MudLocalizer
{
    private IStringLocalizer _localization;

    public ResXMudLocalizer(IStringLocalizer<ResXLanguageResource> localizer)
    {
        _localization = localizer;
    }

    public override LocalizedString this[string key] => _localization[key];
}
Dictionary based Example

An example implementing MudLocalizer using a hardcoded dictionary for the translations. Note that LocalizedString.ResourceNotFound should be true, if there is no custom translation for a given key. If LocalizedString.ResourceNotFound is true, the included English localization will be used.

using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Localization;
using MudBlazor;

internal class DictionaryMudLocalizer : MudLocalizer
{
    private Dictionary<string, string> _localization;

    public DictionaryMudLocalizer()
    {
        _localization = new()
        {
            { "MudDataGrid.is empty", "ist leer" },
            { "MudDataGrid.is not empty", "ist nicht leer" },
            { "MudDataGrid.contains", "enthält" },
            { "MudDataGrid.not contains", "enthält nicht" },
        };
    }
    
    public override LocalizedString this[string key]
    {
        get
        {
            var currentCulture = Thread.CurrentThread.CurrentUICulture.Parent.TwoLetterISOLanguageName;
            if (currentCulture.Equals("de", StringComparison.InvariantCultureIgnoreCase)
                && _localization.TryGetValue(key, out var res))
            {
                return new(key, res);
            }
            else
            {
                return new(key, key, true);
            }
        }
    }
}
Translation Keys

The current default English language localization strings:

Translation Key
English Translation
MudDataGrid.!= !=
MudDataGrid.< <
MudDataGrid.<= <=
MudDataGrid.= =
MudDataGrid.> >
MudDataGrid.>= >=
MudDataGrid.AddFilter Add Filter
MudDataGrid.Apply Apply
MudDataGrid.Cancel Cancel
MudDataGrid.Clear Clear
MudDataGrid.CollapseAllGroups Collapse All Groups
MudDataGrid.Column Column
MudDataGrid.Columns Columns
MudDataGrid.contains contains
MudDataGrid.ends with ends with
MudDataGrid.equals equals
MudDataGrid.ExpandAllGroups Expand All Groups
MudDataGrid.False false
MudDataGrid.Filter Filter
MudDataGrid.FilterValue Filter value
MudDataGrid.Group Group
MudDataGrid.Hide Hide
MudDataGrid.HideAll Hide All
MudDataGrid.is is
MudDataGrid.is after is after
MudDataGrid.is before is before
MudDataGrid.is empty is empty
MudDataGrid.is not is not
MudDataGrid.is not empty is not empty
MudDataGrid.is on or after is on or after
MudDataGrid.is on or before is on or before
MudDataGrid.MoveDown Move Down
MudDataGrid.MoveUp Move Up
MudDataGrid.not contains not contains
MudDataGrid.not equals not equals
MudDataGrid.Operator Operator
MudDataGrid.RefreshData Refresh Data
MudDataGrid.Save Save
MudDataGrid.ShowAll Show All
MudDataGrid.Sort Sort
MudDataGrid.starts with starts with
MudDataGrid.True true
MudDataGrid.Ungroup Ungroup
MudDataGrid.Unsort Unsort
MudDataGrid.Value Value

Copyright © 2020-2024 MudBlazor.

Powered by .NET 8.0.1

An unhandled error has occurred. Reload 🗙