Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To utilize the URL path for localization in Razor and Blazor components, you can use the RouteData object provided by ASP.NET Core.

In Razor components, you can access the RouteData object using the @page directive. For example, if your URL is "https://example.com/en-US/home", you can use the following code to get the language code ("en-US"):

@page "/{lang}/home"
@inject NavigationManager NavigationManager

@{
    var languageCode = NavigationManager.ToBaseRelativePath(NavigationManager.Uri)?.Split('/')[1];
}

In Blazor components, you can access the RouteData object using the [Parameter] attribute in the component's class. For example:

[Parameter]
public string Lang { get; set; }

@code {
    protected override void OnInitialized()
    {
        var languageCode = Lang;
    }
}

You can then use the language code to retrieve the appropriate localization strings for your component. For example:

@inject IStringLocalizer<SharedResources> SharedLocalizer

<p>@SharedLocalizer["WelcomeMessage"]</p>