Skip to content

SetLanguage during WASM startup #51

Description

@nazarenom

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions