MudBlazor data grid localization using Resource files in Blazor server app

249 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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>();

1
On

This works for me:

Step 1: Create a class:

using Microsoft.Extensions.Localization;
using MudBlazor;

namespace PortalApp
{
    public class MudBlazorLocalization : MudLocalizer
    {
        private Dictionary<string, string> _localization;

        public MudBlazorLocalization()
        {
            _localization = new()
            {
                { "MudDataGrid.is empty", "está vacío" },
                { "MudDataGrid.is not empty", "no esta vacío" },
                { "MudDataGrid.contains", "contiene" },
                { "MudDataGrid.not contains", "no contiene" },
                { "MudDataGrid.Unsort","Sin ordenar" }
            };
        }

        public override LocalizedString this[string key]
        {
            get
            {
                if (_localization.TryGetValue(key, out var res))
                {
                    return new(key, res);
                }
                else
                {
                    return new(key, key, true);
                }
            }
        }
    }
}

Step 2: Add the following to Program.cs

builder.Services.AddMudServices();
builder.Services.AddTransient<MudLocalizer, MudBlazorLocalization>();