aksoftware98 / multilanguages

AKSoftware.Localization.MultiLanguages is a package for .NET developers allows them to easily build apps target multiple languages with just few lines of code
MIT License
85 stars 15 forks source link

SetLanguage during WASM startup #51

Open nazarenom opened 1 year ago

nazarenom commented 1 year ago

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:

builder.Services.AddLanguageContainer<EmbeddedResourceKeysProvider>(Assembly.GetExecutingAssembly(),"Resources");
builder.Services.AddBlazoredLocalStorage();
@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?

aksoftware98 commented 1 year ago

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

Stonne commented 1 year ago

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