Search code examples
c#razorasp.net-coreinternationalizationasp.net-core-2.2

Translations not working remotely only in a local environment


On my browser that I run from my local environemnt, the strings are translated as supposed to. When I upload to Azure, it still works. However, when I switch to Edge (that I never use for anything other than downloading FireFox), the strings are not translated anymore. I verified with external users on a wide range of browsers and it seems be platform independent issue.

I have all my translations in a global file placed in the root directory and I have the dummy file so I can inject it into views and controllers, as proposed by the docs. Somehow, the RESX file seems not to be found so I put it to Always upload. No change in misbehavior, though.

I'm not sure how to diagnose it further or if the RESX file is compiled into the DLL or uploaded straight off to the server and read from on the fly. Is it possible to verify that the file is "up there" somehow?

My config is like this.

public void ConfigureServices(IServiceCollection services)
{
  ...
  services.AddLocalization(a => a.ResourcesPath = "");

  services.Configure<RequestLocalizationOptions>(a =>
    {
      CultureInfo[] supportedCultures = {
        new CultureInfo("sv-SE"),
        new CultureInfo("se")
      };
      a.DefaultRequestCulture = new RequestCulture("se");
      a.SupportedCultures = supportedCultures;
      a.SupportedUICultures = supportedCultures;
    });
  ...
  services.AddMvc()
    .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
  ...
  app.UseRequestLocalization();
  ...
  app.UseMvcWithDefaultRoute();
}

edit

I've noticed that it works in Chrome when I have those lines.

RequestLocalizationOptions options = app.ApplicationServices
  .GetService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(options);

It stops working when I have those instead.

  //RequestLocalizationOptions options = app.ApplicationServices
  //  .GetService<IOptions<RequestLocalizationOptions>>().Value;
  app.UseRequestLocalization();

In IE it doesn't work in either case.


Solution

  • Couple words about culture configuration. I guess you wanted to specify only Swedish locale so you have these line of code

    CultureInfo[] supportedCultures = {
        new CultureInfo("sv-SE"),
        new CultureInfo("se")
    };
    

    It turns out that sv-SE is definitely Swedish culture, but se is Northern Sami culture. If your intention is only Swedish culture you need to set sv instead of se

    CultureInfo[] supportedCultures = {
        new CultureInfo("sv-SE"),
        new CultureInfo("sv")
    };
    a.DefaultRequestCulture = new RequestCulture("sv");
    

    Back to the main problem. By default there are 3 ways to set request culture, via query string, cookies or Accept-Language header. It looks like you don't specify a culture in request cookies or query string, but your browser sends Accept-Language header from which ASP.NET Core reads request culture. If a browser sends en-US, en, and sv cultures, none of them matches to supportedCultures (which are sv-SE and se) the framework falls back to DefaultRequestCulture (which is se) and reads resources from Lingo.se.resx and everything is fine. But it looks like Edge (on any other browser but on another computer) sent different set of cultures within Accept-Language header which included sv-SE containing in supportedCultures. So resource reader searched for Lingo.sv-se.resx or Lingo.sv.resx file but with no luck and thus no translation were provided.

    If my assumption is right changing se to sv in your code and renaming Lingo.se.resx to Lingo.sv.resx will fix the problem.