Localization & Configuration in ASP.NET Core 2.0: Part 1 - JSON Request Culture Provider

Localization & Configuration in ASP.NET Core 2.0: Part 1 - JSON Request Culture Provider

Nov 05, 2017     Viewed 6676 times 4 Comments
Posted in #Localization  #JSON 

It had been awhile since I blogged about localization stuff, today I wanna blog about how to use JSON file to specify the current cultures for the ASP.NET Core application.

As we knew appsettings.json is one of the famous files that every ASP.NET Core applications may have, which is store the settings for the application. One of the things that may we need to store are the culture & UI culture for the application, which is the things that I will talk about in this post.

  "Localization": {
    "Culture": "ar-YE",
    "UICulture": "ar-YE"

In the JSON file above, I declared a Localization section that contains both Culture and UICulture keys, Now it's the time to create a JsonRequestCultureProvider to read the localization information from appsettings.json or any JSON file that you like.

public class JsonRequestCultureProvider : RequestCultureProvider
    public static readonly string DefaultJsonFileName = "AppSettings.json";

    public static readonly string LocalizationSection = "Localization";

    public string JsonFileName { get; set; } = DefaultJsonFileName;

    public string CultureKey { get; set; } = "culture";

    public string UICultureKey { get; set; } = "ui-culture";

    public IConfigurationRoot Configuration { get; set; }

    public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
        if (httpContext == null)
            throw new ArgumentNullException();

        var env = httpContext.RequestServices.GetService<IHostingEnvironment>();
        var builder = new ConfigurationBuilder()

        Configuration = builder.Build();

        string culture = null;
        string uiCulture = null;
        var localizationSection = Configuration.GetSection(LocalizationSection);

        if (!string.IsNullOrEmpty(CultureKey))
            culture = localizationSection[CultureKey];

        if (!string.IsNullOrEmpty(UICultureKey))
            uiCulture = localizationSection[UICultureKey];

        if (culture == null && uiCulture == null)
            return Task.FromResult((ProviderCultureResult)null);

        if (culture != null && uiCulture == null)
            uiCulture = culture;

        if (culture == null && uiCulture != null)
            culture = uiCulture;

        var providerResultCulture = new ProviderCultureResult(culture, uiCulture);

        return Task.FromResult(providerResultCulture);

The code above is similar to other built-in request culture providers, the good part is we can use the Configuration APIs to simplify the process of reading the localization information from the JSON file.

Finally we can use the JsonRequestCultureProvider easily in the Configure() method as the following:

var supportedCultures = new[]
    new CultureInfo("en-US"),
    new CultureInfo("ar-YE")

var options = new RequestLocalizationOptions
    DefaultRequestCulture = new RequestCulture("en-US"),
    SupportedCultures = supportedCultures,
    SupportedUICultures = supportedCultures

options.RequestCultureProviders.Insert(0, new JsonRequestCultureProvider());

If you look closely I inserted the newly created request culture provider as the first position to be a winning provider, so the its code can be executed before any other request culture provider.

You can download the source code for this post from my My.AspNetCore.Localization.Json repository on GitHub.


ovis (1/18/2018 6:23:08 PM)

whats the purpose of this article.

Hisham Bin Ateya (1/28/2018 8:49:31 PM)

@ovis this blog post show you how to create a `RequestCultureProvider` that read culture information from JSON file

dapug (2/1/2018 1:07:44 PM)

Why? Perhaps I missed something, but it seems by doing this, it forces the app to use the culture specified in appsettings, and by doing so, will override all other providers (such as from querystring, or cookie). Basically, your site only will ever render ar-YE, and the user can never request a different language. Why would you want to do that?

Cool example of how to override this and access config from httpContext at this point in the pipeline though.

Dave Johnson (3/9/2018 9:10:56 PM)

Great article - thanks this is just what we needed. We have a web app that we want to assign a specific culture depending on the customer - customers in the US have different labels on certain things than customers in Canada so we didn't need the option to have culture assigned to a specific user. This fit the bill nicely.

Leave a Comment