Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor Server Side Localization not working #12088

Closed
aherrick opened this issue Jul 11, 2019 · 20 comments
Closed

Blazor Server Side Localization not working #12088

aherrick opened this issue Jul 11, 2019 · 20 comments
Labels
area-blazor Includes: Blazor, Razor Components question
Milestone

Comments

@aherrick
Copy link

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

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jul 11, 2019
@mkArtakMSFT
Copy link
Member

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.

@aherrick
Copy link
Author

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

@mkArtakMSFT
Copy link
Member

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

@mkArtakMSFT mkArtakMSFT added this to the Discussions milestone Jul 15, 2019
@mkArtakMSFT mkArtakMSFT reopened this Jul 15, 2019
@aherrick
Copy link
Author

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

@christiansparre
Copy link

@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

@aherrick
Copy link
Author

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

@christiansparre
Copy link

Yes @aherrick it's registered as singleton

@SeppPenner
Copy link

SeppPenner commented Aug 2, 2019

What about IStringLocalizer<>? I already asked a similar question here: #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.

@ghost
Copy link

ghost commented Sep 5, 2019

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<Index> L

<h1>@L["App Home"]</h1>

Resource files

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

@GiroSA
Copy link

GiroSA commented Oct 6, 2019

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.

@SeppPenner
Copy link

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

@ghost
Copy link

ghost commented Oct 7, 2019

@GiroSA did you try my example?

@endeffects
Copy link

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.

@SeppPenner
Copy link

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

@Stamo-Gochev
Copy link

@aaronhudon-ts Can you share a repo that demonstrates your approach in #12088 (comment)? I am still not able to make it work.

@ghost
Copy link

ghost commented Oct 31, 2019

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

@Stamo-Gochev
Copy link

@aaronhudon-ts They are, the problem seems to be that localization of blazor components is not currently supported #16687 (comment)

@ghost
Copy link

ghost commented Nov 4, 2019

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

@GiroSA
Copy link

GiroSA commented Nov 11, 2019

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.

@ghost
Copy link

ghost commented Dec 7, 2019

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.

@ghost ghost closed this as completed Dec 7, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2019
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components question
Projects
None yet
Development

No branches or pull requests

7 participants