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 16089 times 17 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.

Smithf57 (3/23/2018 4:57:29 PM)

I'm not sure exactly why but this weblog is loading very slow for me. Is anyone else having this problem or is it a problem on my end? I'll check back later and see if the problem still exists. deaaedefkedekage

Pharme982 (3/25/2018 3:16:04 AM)

Hello!sildenafil generic india

viagra_canada (4/11/2018 1:40:48 PM)

online cialis , viagra price , buy viagra , cialis , viagra canada ,

buy_cialis (4/23/2018 7:37:18 PM)

buy cheap cialis , buy cialis , buy cialis , buy cialis , viagra online ,

generic_cialis (6/27/2018 4:23:02 AM)

viagra , cialis cheap , generic cialis , buy viagra , cialis online ,

buy_cialis (8/9/2018 5:52:20 AM)

buy cialis , buy cialis , cialis , viagra online , cialis ,

buy_cialis (9/21/2018 2:54:31 AM)

cialis online , viagra super active , viagra , buy cialis , cialis online ,

cialis (9/21/2018 7:19:07 AM)

cialis , cheap viagra , buy viagra , cialis , cialis ,

canadian pharmacies shipping to usa (10/1/2018 1:45:01 PM)

Hey, I think your site might be having browser compatibility issues.
When I look at your blog in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up! Other then that, wonderful blog!

canadadrugs (10/2/2018 11:45:28 PM)

I absolutely love your blog and find most of your post's to be exactly what I'm looking
for. Do you offer guest writers to write content in your
case? I wouldn't mind creating a post or elaborating on a few of the subjects you write regarding here.
Again, awesome website!

canadian pharmacies vipps (10/5/2018 1:29:33 PM)

I would like to thank you for the efforts you've put in penning this
website. I am hoping to check out the same high-grade content by you in the future as well.

In truth, your creative writing abilities has encouraged me to
get my own blog now ;)

Sophie Miller (11/13/2018 1:47:32 AM)

Vi ringrazio per l'informazione! Stavo cercando e non poteva trovare. Lei mi ha aiutato!
university of wisconsin madison sat scores

Leave a Comment