multilanguages icon indicating copy to clipboard operation
multilanguages copied to clipboard

SetLanguage during WASM startup

Open nazarenom opened this issue 10 months ago • 2 comments

I'm using the last version pf this package and I'd lime to set the language used at startup getting the last used language from the local storage.

My actual solution is:

  • in the program.cs I do the setup
builder.Services.AddLanguageContainer<EmbeddedResourceKeysProvider>(Assembly.GetExecutingAssembly(),"Resources");
builder.Services.AddBlazoredLocalStorage();
  • in the MainLayout.razor i try to set the last used language:
@code {

    protected override async Task OnInitializedAsync()
    {
        string languageCode = Thread.CurrentThread.CurrentUICulture.Name;
        if (await storage.ContainKeyAsync("language-code"))
        {
            languageCode = await storage.GetItemAsStringAsync("language-code");
        }
        language.SetLanguage(CultureInfo.GetCultureInfo(languageCode));
    }
}

if for example the code is not the default one (for example 'it-IT') I get an error in the "language.SetLanguage"

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Culture is not supported. (Parameter 'name') "it-it" is an invalid culture identifier. System.Globalization.CultureNotFoundException: Culture is not supported. (Parameter 'name') "it-it" is an invalid culture identifier. at System.Globalization.CultureInfo.GetCultureInfo(String name) at Carmet.Drevojas.Mes.Client.Shared.MainLayout.OnInitializedAsync() in C:\src\Git-LC\Carmet\Carmet.Drevojas.Mes\src\Carmet.Drevojas.Mes\Client\Shared\MainLayout.razor:line 33 at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

And the program starts in English.

On the other side if I do the same on the parameter page where the user change the language everything works.

    async void SaveLanguage()
    {
        languageContainer.SetLanguage(System.Globalization.CultureInfo.GetCultureInfo(selectedLanguage, false));
        await storage.SetItemAsync("language-code", selectedLanguage);
        localizationClientService.NotifyLanguageChanged();
        Snackbar.Add(languageContainer.Keys["Parameters:TheLanguageWasSuccessfullyChanged"], Severity.Success);
    }

What I'm missing or doing wrong?

nazarenom avatar Oct 06 '23 09:10 nazarenom

Thank you so much for this report

I'm reproducing the issue on my repo to check what's going wrong I will keep you updated on this side

aksoftware98 avatar Oct 07 '23 03:10 aksoftware98

I know this is late for an answer but I noticed = if for example the code is not the default one (for example 'it-IT') I get an error in the "language.SetLanguage" But the error message says "it-it" is not a valid cultutre identifier... it should bet it-IT are you passing the correct identifier ? or it-it

Stonne avatar Nov 06 '23 15:11 Stonne