Translate blazor server site with .NET 7

175 Views Asked by At

I try to find a way to translate a site that is writen with C# Blazor Server .NET 7.

It seems that there is no clear solution.

In the official page there is a solution with MVC although it refers to Blazor Server.

enter link description here

I would like to use a dropdown to select the language for example

something like this

<select class="form-control"  style="width:300px; margin-left:10px;">
@foreach (var culture in cultures)
{
    <option value="@culture"onclick="@(()=>OnLangSelected(culture))">@culture.DisplayName</option>
}
</select>
@code{
string[] cultures = new[]
{
    "en",
    "el"
};
protected async Task OnLangSelected(string selLang)
  {
    var cult = CultureInfo.GetCultures(CultureTypes.AllCultures).First(c => c.Name == selLang);
    CultureInfo.CurrentCulture=cult;
  }
}

In program.cs I use

builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services.AddScoped<IStringLocalizer<Index>, StringLocalizer<Index>>();

And I created a Resources folder where I have 2 files Index.el.resx and Index.en.resx

In Index.razor I use

@inject IStringLocalizer<Index> L
....
@L["varNameOfResx"]

But I get as a result varNameOfResx and not the translation in either language english or greek.

What am I doing wrong? Could you provide any help?

0

There are 0 best solutions below