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 Microsoft 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];
}

Copyright © 2020-2024 MudBlazor.

Powered by .NET 8.0.8

An error has occurred. This application may no longer respond until reloaded. Reload 🗙