asp.net core - Problem in accessing right value in resources during changing culture - blazor webassembly - c# - Stack Overflow

admin2025-05-01  0

I have these files in Resources project

  • Titles.resx
  • Titles.en.resx
  • Titles.fa.resx
  • Messages.resx
  • Messages.en.resx
  • Messages.fa.resx

now the switching works, but if the language in localstorage initially is set as "fa-IR" from last session, the switching works fine between english and persian, but if the language in localstorage is set as "en-US" from last session the switching doesnt work and what ever i do, all the captions are in english and i had checked and i am sure the culture is set to right culture, please help.

i did add this in program.cs :

    var host = builder.Build();
    var jsRuntime = host.Services.GetRequiredService\<IJSRuntime\>();
    var lang = await jsRuntime.InvokeAsync\<string\>("localStorage.getItem", "lang");
    if (!string.IsNullOrEmpty(lang))
    {
      var culture = new CultureInfo(lang);
      CultureInfo.DefaultThreadCurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentUICulture = culture;
    
    }
    await host.RunAsync();

and this to languageService :

    public async Task<bool> SetLanguageAsync(string lang)
    {
      SetCulture(lang);
      await _jSRuntime.InvokeVoidAsync("localStorage.setItem", "lang", lang);

      return true;
    }

    private void SetCulture(string cultureName)
    {
      var culture = new CultureInfo(cultureName);
      CultureInfo.DefaultThreadCurrentCulture = culture;
      CultureInfo.DefaultThreadCurrentUICulture = culture;
    }
转载请注明原文地址:http://anycun.com/QandA/1746098259a91641.html