Aspnetcore: Blazor Server Side Localization not working

Created on 11 Jul 2019  路  20Comments  路  Source: dotnet/aspnetcore

Trying to stand up a super simple example of Blazor Server Side with Localization/Translation support.

I posted the spike here: https://github.com/aherrick/BlazorServerSideLocalization

It appears in my Index.razor view @inject IViewLocalizer L is always null. I have a Resources folder with a Pages.Index.en.resx

area-blazor question

Most helpful comment

Localization is critical for us. I really hope MS comes up with official guidance on this soon. I also hope that the mechanism will be server/client side agnostic as well as not impact unit testing.

All 20 comments

Thanks for contacting us, @aherrick.
IViewLocalizer is an MVC feature and can't be used with Blazor. You can use the resource files directly yourself to read localized resources.

@mkArtakMSFT do you have an example of this within the context of Blazor or the example I posted?

@aherrick not really. I'll leave this as a question for community members to help you out with.

OK - it would be nice if there was some documentation or examples on how to do Localization with Blazor.

@aherrick

I whipped together something very crude last week as I ran into the same problem and I needed a quick fix for it. I ended up with something along the lines of the following. Maybe it can send you on the right track for your needs.

See this gist: https://gist.github.com/christiansparre/5576907ad391b972581714f7564674b7

  • There is a TextService class pulling strings from resource files
  • There is a LocalizedText blazor component displaying the text in the currently selected culture
  • There is a CultureChanger class that can be used to change the "current" culture, that also has an event the LocalizedText component listens for

@christiansparre Awesome! Are you initializing TextService as a singleton?

Yes @aherrick it's registered as singleton

What about IStringLocalizer<>? I already asked a similar question here: https://github.com/aspnet/AspNetCore/issues/12277.

@page "/"
@using  Microsoft.Extensions.Localization;
@inject IStringLocalizer<Index> L

<h1>Hello, world!</h1>

Welcome to your new app.

<h1>@L["Hello"]</h1>

@code {
}

Don't forget to add

services.AddLocalization(options => options.ResourcesPath = "Translations");

to your ConfigureServices method in the Startup file.

Translations here is a folder in your project on root level containing all the *.resx files used for your view localization.

I'd like to expand on @SeppPenner post, as I found the following to work based on the browser's preferred language.

Startup.cs
```
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();

// localization
services.AddLocalization(options => options.ResourcesPath = "Resources");
var supportedCultures = new List<CultureInfo>{
    new CultureInfo("en-US"),
    new CultureInfo("fr")
};
services.Configure<RequestLocalizationOptions>(options =>
{
    options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en");
    options.SupportedUICultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
});

}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

// use the browser's preferred language for localization
app.UseRequestLocalization();
app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.MapBlazorHub();
    endpoints.MapFallbackToPage("/_Host");
});

}

**Pages/Index.razor**

@page "/"
@inject IStringLocalizer L

@L["App Home"]

```

Resource files

/Resources
......./Pages
.............Index.fr.resx
.............Index.resx

Localization is critical for us. I really hope MS comes up with official guidance on this soon. I also hope that the mechanism will be server/client side agnostic as well as not impact unit testing.

@GiroSA I don't know where the problem is here exactly, but it works perfectly for server side in my projects...

@GiroSA did you try my example?

I cannot get the StringLocalizer to work on a Blazor Client Side Application. The Resource Files are published to the Output Folder but the Loalizer allways reports no Manifest found for the current Culture, which do exist.

@endeffects As far as I know, there is a seperate issue for that.

@aaronhudon-ts Can you share a repo that demonstrates your approach in https://github.com/aspnet/AspNetCore/issues/12088#issuecomment-528499976? I am still not able to make it work.

@Stamo-Gochev Are your resource files in the same project, under the folder "Resources"? This also assumes Server-Side Blazor.

@aaronhudon-ts They are, the problem seems to be that localization of blazor components is not currently supported https://github.com/aspnet/AspNetCore/issues/16687#issuecomment-548461295

@Stamo-Gochev I am only using IStringLocalizer as shown in my example above - and everything works fine.

I am finally back on this after a huge detour. I have implemented and am using a custom IStringLocalizer and it works as expected server side. I am not going to worry about client side for now. I am hoping that MS would have kept the infrastructure the same for both client and server side by the time they shipped the client side in May next year.

Thank you for contacting us. Due to no activity on this issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

Was this page helpful?
0 / 5 - 0 ratings