Unable to switch app language/locale at runtime in a .NET MAUI Blazor Hybrid Application

154 Views Asked by At

I'm currently developing a .NET MAUI Blazor hybrid application (GitHub repo here) and I've encountered an issue with changing the locale at runtime.

Component Version
.NET SDK 8.0.200-preview.23624.5
IDE Microsoft Visual Studio Community 2022 (64-bit) - Preview
Version 17.9.0 Preview 4.0

My application aims (but fails) to support multiple languages, and I want users to be able to switch between these languages at runtime in the Settings page. However, no matter what approach I try, the result is always the same: The application receives the language request, but the app culture never changes. My debug output always shows:

OnSubmitLanguage: fr  # This is the language I chose
Current Culture: en-CA  # This is my computer's locale
Current UI Culture: en-US  # This is the default app locale
Thread Current Culture: en-CA
Thread Current UI Culture: en-US

To achieve this, I've tried several approaches:

  1. Traditional AppRes Approach: Initially, I tried using the traditional AppRes approach for localization. However, this did not allow me to change the locale at runtime. Although I choose to switch languages, the app culture (and hence, language) never changes:
Settings page Language Selector Settings page Save Button Result in the debug window
Language Selector Save button after language selected language choice has no effect
  1. Localizer Approach: Next, I tried using the newer IStringLocalizer<T> service, which is recommended for localization in .NET 6 and onwards. Despite setting the culture as shown below, the UI did not update to reflect the new culture.

    public void OnSubmitLanguage(EventArgs args)
    {
        Debug.Print($"OnSubmitLanguage: {SelectedLanguage}");
        Debug.Print($"Current Culture: {CultureInfo.CurrentCulture}");
        Debug.Print($"Current UI Culture: {CultureInfo.CurrentUICulture}");
        Debug.Print($"Thread Current Culture: {Thread.CurrentThread.CurrentCulture}");
        Debug.Print($"Thread Current UI Culture: {Thread.CurrentThread.CurrentUICulture}");
        var newCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage);
        CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = Thread.CurrentThread.CurrentUICulture = newCulture;
    }
    
  2. Multilingual App Toolkit Approach: I also tried using the Multilingual App Toolkit, which is a contemporary recommendation for localization in .NET applications. However, this approach also did not result in the desired behavior.

  3. Cookie Setting Approach: Finally, I tried storing the selected culture in a cookie. For this, I created a CookieService class and used it to set the culture cookie. However, this also did not result in the UI updating to the new culture.

    public void OnSubmitLanguage(EventArgs args)
    {
        Debug.Print($"OnSubmitLanguage: {SelectedLanguage}");
        Debug.Print($"Current Culture: {CultureInfo.CurrentCulture}");
        Debug.Print($"Current UI Culture: {CultureInfo.CurrentUICulture}");
        Debug.Print($"Thread Current Culture: {Thread.CurrentThread.CurrentCulture}");
        Debug.Print($"Thread Current UI Culture: {Thread.CurrentThread.CurrentUICulture}");
        var newCulture = CultureInfo.CreateSpecificCulture(SelectedLanguage);
        CultureInfo.CurrentUICulture = CultureInfo.CurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = Thread.CurrentThread.CurrentUICulture = newCulture;
        // Store the selected culture in a cookie
        var cookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(newCulture));
        cookieService.SetCookie(".AspNetCore.Culture", cookieValue, 365);
    }
    

Despite trying these different approaches, I'm still unable to change the locale at runtime. The CurrentCulture and CurrentUICulture remain unchanged after the method is called, as indicated by the debug prints.

Has anyone encountered a similar issue or does anyone have any suggestions on what I might be doing wrong? Any help would be greatly appreciated!

0

There are 0 best solutions below