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

Question: Can I add localization of Razor components to a Blazor project without using MVC? #12277

Closed
SeppPenner opened this issue Jul 17, 2019 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components question

Comments

@SeppPenner
Copy link

Question

Not actually a bug, but a question. The new Blazor project template uses EndpointRouting by default. Can I add localization of Razor components to this without using MVC? If not, how can I configure the project to work with MVC correctly? Or do I just have a problem with understanding something here?

I'm trying to follow https://docs.microsoft.com/en-US/aspnet/core/fundamentals/localization?view=aspnetcore-3.0 to add localization support to some razor component.

My Startup.cs class (and the component code and _Host.cshtml) can be found here: https://gist.github.com/SeppPenner/4e3d909a16a53cfab8e701d293344356

If I add the commented code:

services.AddMvc(options => options.EnableEndpointRouting = false).AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix).AddDataAnnotationsLocalization();

and

app.UseMvc();

I get the a null reference exception in the _Host.cshtml in the line

<app>@(await Html.RenderComponentAsync<App>())</app>

If I comment the both lines, it cannot work, of course and it throws the exception:

InvalidOperationException: Cannot provide a value for property 'Localizer' on type 'TestComponent'. There is no registered service of type 'Microsoft.AspNetCore.Mvc.Localization.IViewLocalizer'.

To Reproduce

Steps to reproduce the behavior:

  1. Using NET Core 3.0.100.12264 (preview-6).
  2. Visual Studio 2019 Preview 16.2.29021.104.
  3. Create a default app.
  4. Scaffold the identity pages (Don't know if this is relevant, but I did it.
  5. Adjust the Startup.cs and _Host.cshtml file as described and create the component.

Resource files

The resource files are named TestComponent.en.resx and TestComponent.de.resx and located under the Translations folder in the main

@rynowak
Copy link
Member

rynowak commented Jul 17, 2019

Have you tried using IStringLocalizer<>? IViewLocalizer is an MVC feature, and I expect that it won't work inside Blazor.

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jul 17, 2019
@SeppPenner
Copy link
Author

@rynowak Thanks for the information. That's what I was searching for. Works as expected.

@SeppPenner
Copy link
Author

Just another thing. Do you have an example on how I can localize e.g. a DateTime format in the same setup?

@rynowak
Copy link
Member

rynowak commented Jul 18, 2019

That will just generally work by default since .NET's formatting uses CultureInfo.CurrentCulture by default.

We're making some changes in preview 8 that will make this work correctly with @bind. If you have a specific scenario in mind, I'd suggest trying it out in preview 8 and logging an issue if its not to your satisfaction.

@Stamo-Gochev
Copy link

Stamo-Gochev commented Oct 30, 2019

@rynowak I am testing a similar approach for localizing a blazor component. Here is my repo:
https://github.com/Stamo-Gochev/blazor-test-localization

The LocalizedComponent uses IStringLocalizer. A select element is added (similar to the docs) in order to change the culture.

Changing the culture to fr using the dropdown changes the format of the DateTime, but the localized value remains the same. Actually, it displays the key - key1, so I suppose that the localization is not triggered at all. Resources are registered in Startup.cs. Can you shed some light why this isn't working?

@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 3, 2019
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

4 participants