datagridlocalizationresxmudblazor

MudBlazor data grid localization using Resource files in Blazor server app


I am trying to localize MudBlazor data grid using resource files. But data grid filter items are still shown in English.

According to this I have added the following class:

using Microsoft.Extensions.Localization;
using MudBlazor;

internal class ResXMudLocalizer : MudLocalizer
{
    private IStringLocalizer _localization;

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

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

And added the following to Program.cs

builder.Services.AddTransient<MudLocalizer, ResXMudLocalizer>();

I have made sure that the CurrentUICulture is "fa-IR". I have made two Resx files named 'MudResources.resx' and 'MudResources.fa-IR.resx' and put localized strings in them.

But the data grid is still showing English words.

Am I doing something wrong or missing something?


Solution

  • I found the root of the problem.

    I was using the app.UseRequestLocaliztion which somehow overrode culture to "en".

    I removed the requestlocalization from program.cs and everything is working fine now.

    Here are the steps to localize mudblazor in my project:

    1. Create an empty MudResources.cs class
    
        public class MudResources
        {
        }
    
    
    1. Create ResxMudLocalizer class:
    
        using Microsoft.Extensions.Localization;
        using MudBlazor;
        internal class ResXMudLocalizer(IStringLocalizer<MudResources> localizer) : MudLocalizer
        {
            private readonly IStringLocalizer _localization = localizer;
            
            public override LocalizedString this[string key] => _localization[key];
        }
    
    
    1. Create resource files MudResources.resx, MudResources.fa-IR.resx, ...
    2. Add these to Program.cs
    
        builder.Services.AddTransient<MudLocalizer, ResXMudLocalizer>();