MudBlazor data grid localization using Resource files in Blazor server app
Asked Answered
P

2

6

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?

Postobit answered 5/11, 2023 at 6:36 Comment(2)
Are you using the translation keys mudblazor.com/features/localization#translation-keys ? Make sure it finds the resx file. For a reason unknown for me it wasn't working for me. After creating a .razor file ./Shared/MudDataGridLocalizer.razor and the .resx file ./Resources/Shared/MudDataGridLocalizer.de.resxPleasing
Yes, I'm using the translation keys. Instead of creating a .razor file, I have created a .cs file 'MudResources.cs' and I have created two .resx files named 'MudResources.resx' and 'MudResources.fa-IR.resx'. But it does not work... @PleasingPostobit
P
2

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

Postobit answered 23/3 at 7:56 Comment(0)
E
0

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>();
Elka answered 13/12, 2023 at 17:19 Comment(1)
the question is for using resx files not dictionaryCitronella

© 2022 - 2024 — McMap. All rights reserved.