Nb777
Nb777

Reputation: 2032

localization with @page directive

Can I use @page directive of blazor with IStringLocalizer?, something like this:

@page @Localizer["myComponent"]

I know that we can't use two of @ after each other but can someone give me an idea how I can do that?

Thanks

Upvotes: 0

Views: 307

Answers (1)

Quango
Quango

Reputation: 13458

Simple answer is you can't do this.

The @page directive generates a RouteAttribute on the class generated in C# for the Blazor page. You can see this in the generated code in /obj:

    [Microsoft.AspNetCore.Components.RouteAttribute("/")]
    public partial class Index : Microsoft.AspNetCore.Components.ComponentBase
    {

This means the route is defined at compile time.

The Localize function uses the browser's language settings at run time to determine a language and translate a resource.

So how could it be done? The routes for Blazor pages would need to be fixed, but you'd add a set of localized routes for each language: this would mean having a "translation map" of localized route to 'actual route'.

e.g. "/fr/indice" => "/index"

This would be more an ASP.NET Core Endpoint routing issue than a Blazor one. You should find several articles on routing in this regard.

Upvotes: 1

Related Questions