1

Can someone confirm for me whether this code changes the culture for all users of the application, or just for the current user?

var cultureInfo = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

I want to show a combo with the different cultures accepted for the application and change the culture when I select it in the combo, but if I open the application in, for example Chrome and Firefox when I change in one, it seems that the culture changes in the other, and this is scary.

TylerH
  • 20,799
  • 66
  • 75
  • 101
JG73
  • 90
  • 1
  • 10

2 Answers2

2

you can use bellow code

Startup.ConfigureServices

CultureInfo[] supportedCultures = new[]
       {
        new CultureInfo("ar"),
        new CultureInfo("fa"),
        new CultureInfo("en")
    };

    services.Configure<RequestLocalizationOptions>(options =>
    {
        options.DefaultRequestCulture = new RequestCulture("ar");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
        options.RequestCultureProviders = new List<IRequestCultureProvider>
            {
                new QueryStringRequestCultureProvider(),
                new CookieRequestCultureProvider()
            };

    });

Startup.Configure

app.UseRequestLocalization();

change language:

[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
    Response.Cookies.Append(
        CookieRequestCultureProvider.DefaultCookieName,
        CookieRequestCultureProvider.MakeCookieValue(new     RequestCulture(culture)),
    new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
    );

    return LocalRedirect(returnUrl);
}

More Details: here

Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34
1

By default this is set to a the culture of the machine,So it automatically applicable to all users.

If you do intend to allow the user to set their own culture in their browser, you intend to use a query string to define the culture, or you intend to do a custom request culture provider (outlined in the next section) to allow code to set a custom culture based on other parameters, then you need to provide a list of supported cultures

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<RequestLocalizationOptions>(options =>
    {
        options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
        //By default the below will be set to whatever the server culture is. 
        options.SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"),new CultureInfo("en-NZ") };

        options.RequestCultureProviders = new List<IRequestCultureProvider>();
    });

    services.AddMvc();
} 

For detailed Info:-refer_this_document