gunaaa Ответов: 0

Локализация Blazor на главной странице макета


I implemented localization in blazor component page and it's working fine. But I need to move this functionality to main layout page, so that once I changed the language, it will reflect in all pages. Same code used in main layout page whatever implemented in component page, but it's not working in main layout page.


Что я уже пробовал:

MainLayout.razor

@inherits LayoutComponentBase
@inject NavigationManager NavigationManager

<div class="sidebar">
   <NavMenu />
</div>

<div class="main">
    <div class="top-row px-4">
        <select @onchange="OnSelected">
           <option value="en">English</option>
           <option value="de">German</option>
        </select>
    </div>
    <div class="content px-4">
       @Body
    </div>
</div>

@code {
   private void OnSelected(ChangeEventArgs e)
   {
       var culture = (string)e.Value;
       var uri = new Uri(NavigationManager.Uri)
         .GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
       var query = $"?culture={Uri.EscapeDataString(culture)}&" +
          $"redirectUri={Uri.EscapeDataString(uri)}";

       CultureInfo.CurrentCulture = new CultureInfo(culture);
       CultureInfo.CurrentUICulture = new CultureInfo(culture);
       System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
       System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

       System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(culture);
       System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(culture);

       StateHasChanged();
   }
}

Same code is working in razor component page. If use the code in main layout page, text is not changing based on language selection.

Please do the needful.

Thanks in advance.

0 Ответов