12

I'm having some odd issues with a web application written using Asp.Net Core 1.1, using the full .Net Framework v4.6.2.

I want to force the application to use a swedish locale (sv-SE). This is working just fine on the development machine (of course...) but not on the server where it's supposed to run (running Windows Server 2012r2).

I've tried the following:

1) Putting "SiteLocale": "sv-SE" in the appsettings.json file.

2) Putting the following in the web.config

<configuration>
    <system.web>
        <globalization culture="sv-SE" uiCulture="sv-SE" />
    </system.web>
    ...
</configuration

3) In program.cs before initializing application setting the default locale

System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("sv-SE");
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new System.Globalization.CultureInfo("sv-SE");

4) Added RequestLocalization to Configure(...) in Startup.cs

var supportedCultures = new[]
{
    new CultureInfo("sv-SE")
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
    DefaultRequestCulture = new RequestCulture("sv-SE"),
    SupportedCultures = supportedCultures,
    SupportedUICultures = supportedCultures
});

5) In my controller constructor setting the current threads culture

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("sv-SE");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("sv-SE");

6) In my controller actions setting the current threads culture

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("sv-SE");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("sv-SE");

7) Installed swedish language pack for the server (server was initially only english)

After doing all of the above any dates or numbers output in the views are still rendered using en-US locale.

Looking at current locale in the view I can see the following:

System.Globalization.CultureInfo.CurrentCulture.Name => "sv-SE"
System.Globalization.CultureInfo.CurrentUICulture.Name => "sv-SE"
System.Globalization.CultureInfo.InstalledUICulture => "en-US"

Again, on my development machine this is working ok, numbers & dates are formatted according to swedish locale. But on the server they are not.

On the server the application is running under the ApplicationPoolIdentity. Which I'm assuming is configured to use "en-US" as locale since the OS was initially installed with english language only.

Perplexed as to why setting CurrentThread.CurrentCulture/CurrentUICulture has no effect.

NeoDarque
  • 3,222
  • 3
  • 19
  • 21

3 Answers3

20

I had a similar issue with ASP Net Core 3.0. The site hosting was in a different country causing issues with formats.

I added the following to the startup:

using Microsoft.AspNetCore.Localization;
using System.Globalization;

in ConfigureServices:

services.AddLocalization();

in Configure:

var supportedCultures = new[]{
    new CultureInfo("en-US")
};
app.UseRequestLocalization(new RequestLocalizationOptions {
    DefaultRequestCulture = new RequestCulture("en-US"),
    SupportedCultures = supportedCultures,
    FallBackToParentCultures= false
});
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Tom
  • 16,842
  • 17
  • 45
  • 54
William Mendoza
  • 327
  • 2
  • 4
9

Did you try setting the CultureInfo.CurrentCulture property in Program.cs:

using System.Globalization;

CultureInfo.CurrentCulture = new CultureInfo("sv-SE");
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • 1
    Just tried this too, but still the same result. I would assume setting these properties in the Controller's constructor or Actions would cover this case too. But thanks for the suggestion. – NeoDarque Feb 28 '17 at 21:57
  • @NeoDarque did you configure it before creating the `WebHostBuilder` and calling `Build()`? – Henk Mollema Feb 28 '17 at 22:09
  • Yes, correct. Tried setting both CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture, CultureInfo.DefaultThreadCurrentCulture & CultureInfo.DefaultThreadCurrentUICulture before creating the WebHostBuilder. – NeoDarque Mar 01 '17 at 05:18
  • That one helped me, I added it in the `Main()` method just before the `BuildWebHost(args).Run();` – retif Feb 23 '18 at 02:38
5

Take a look at Microsoft ASP.NET Core Docs - Globalization and localization.

4) Added RequestLocalization to Configure(...) in Startup.cs

From docs:

The current culture on a request is set in the localization Middleware. The localization middleware is enabled in the Configure method of Startup.cs file.

Note, the localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvc()).

5) In my controller constructor setting the current threads culture

6) In my controller actions setting the current threads culture

Instead of setting in the constructor or in every action, try set the culture at OnActionExecuting filter. Here is an example code:

public class InternationalizationAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var cultureInfo = CultureInfo.GetCultureInfo("sv-SE");

        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }
}

In your controller class, put this: [Internationalization].

Paulo Pereira
  • 1,064
  • 8
  • 16
  • I did try this too, but still does not help. As I wrote above I can see in the View that CurrentCulture & CurrentUICulture is set correctly to "sv-SE" so this is probably not the issue. But even if this is so, the rendering of viewmodel properties are still rendered with en-US locale. – NeoDarque Mar 01 '17 at 05:46