The "Localization / Cookies" section is misleading – out of nowhere, it mentions HostModel and its OnGet method in Pages/Host.cshtml.cs file, which does not exist (at least not in a project created from the blazorserver template). I believe that HostModel is a Razor pages project feature; blazorserver projects have Pages/_Host.cshtml hosting page. Therefore, the correct instruction to enable custom localization scheme using the CookieRequestCultureProvider is to add the cookie-setting code to the Pages/_Host.cshtml before the call to Html.RenderComponentAsync like this:
<body>
@{
this.HttpContext.Response.Cookies.Append(
Microsoft.AspNetCore.Localization.CookieRequestCultureProvider.DefaultCookieName,
Microsoft.AspNetCore.Localization.CookieRequestCultureProvider.MakeCookieValue(
new Microsoft.AspNetCore.Localization.RequestCulture(
System.Globalization.CultureInfo.CurrentCulture,
System.Globalization.CultureInfo.CurrentUICulture)));
}
<app>
@(await Html.RenderComponentAsync<App>(RenderMode.ServerPrerendered))
</app>
<script src="_framework/blazor.server.js"></script>
</body>
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
I'll cross-link the work first ...
PR was https://github.com/aspnet/AspNetCore.Docs/pull/13724
Issue (includes the source content): https://github.com/aspnet/AspNetCore.Docs/issues/13436
... and ask @rynowak for feedback on your remarks.
@guardrex @rynowak
Can you also add some info how the section "Providing UI to choose the culture" from https://github.com/aspnet/AspNetCore.Docs/issues/13436 should be translated to the template blazor project? There are no controllers there. Is the same approach applicable to wasm apps?
@Stamo-Gochev There is a controller: CultureController with an action SetCulture receiving the request from the component's OnSelected: UriHelper.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);.
@guardrex Controllers in general are not part of the template blazor project. The examples I found are all about how to make localization work in ASP.NET Core MVC project - and they work fine. But what about blazor apps? Can you add an example that makes this work in a server-side blazor app? What about wasm and the problem with .resx files there?
It's covered in the MVC area of the docs.
Add to the service collection (Startup.ConfigureServices) ...
services.AddControllers();
Add the controller for endpoint routing in Startup.Configure ...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
With the bits in the topic for Blazor Server, binding for loc should work after that according to https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#use-net-localization-scenarios-in-blazor-apps.
I can't remark on the Blazor WebAssembly approach. I was merely a conduit for moving loc content into the doc. I'll :ear: for engineering to provide further guidance on the Blazor WebAssembly scenario.
btw -- now that we've noted here that those MVC bits are missing from coverage, I'll see about cross-linking to surface that. :point_up:
@guardrex My concrete case is that I want to localize a blazor component from a custom class library project. I suppose that this should be done by changing the culture and using IStringLocalizer, but this does not seem to be working. On the other hand, the examples from the docs show how to localize from a controller. It will be helpful if there is a runable project that demonstrates a similar thing.
TRUE!!, that docs mislead me for days, thanks for this correction. i have a question tho, so in my case, the user language is stored in the server, please i want to know where is the best place to make the api call to get the user languages and set the cultureinfo using the language. @PaloMraz @guardrex
Documentation and example for blazor webassembly localization are needed.
At the moment using IStringLocalizer, I have some issue with UI refresh.
Most helpful comment
It's covered in the MVC area of the docs.
Add to the service collection (
Startup.ConfigureServices) ...Add the controller for endpoint routing in
Startup.Configure...With the bits in the topic for Blazor Server, binding for loc should work after that according to https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#use-net-localization-scenarios-in-blazor-apps.
I can't remark on the Blazor WebAssembly approach. I was merely a conduit for moving loc content into the doc. I'll :ear: for engineering to provide further guidance on the Blazor WebAssembly scenario.