ASP.NET Core Request Localization Options

Here is my custom request culture provider which returns “en” as a default culture if no culture specified in url (for example http://sypalo.com/ru or http://sypalo.com/en).
My idea to show website on that language which is default in user’s browser, so I’m looking a way how to determine it and return it instead of: return Task.FromResult(new ProviderCultureResult(“en”, “en”));

services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new List<CultureInfo>
    {
        new CultureInfo("en"),
        new CultureInfo("ru")                            
    };

    options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;

    options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
    {                    
        var pathSegments = context.Request.Path.Value.Split('/');
        if (pathSegments.Count() > 0)
        if (supportedCultures.Select(x => x.TwoLetterISOLanguageName).Contains((pathSegments[1])))
            return Task.FromResult(new ProviderCultureResult(pathSegments[1], pathSegments[1]));
       return Task.FromResult(new ProviderCultureResult("en", "en"));
   }));
});

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can get Accept-Language header from the current Request and set default language. Your code should be something like this:

services.Configure<RequestLocalizationOptions>(options =>
{
    //...

    options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
    {                    
       //...
       var userLangs = context.Request.Headers["Accept-Language"].ToString();
       var firstLang = userLangs.Split(',').FirstOrDefault();
       var defaultLang = string.IsNullOrEmpty(firstLang) ? "en" : firstLang;
       return Task.FromResult(new ProviderCultureResult(defaultLang, defaultLang));
   }));
});

Method 2

Another way to get the Accept-Language header with a framework call (ASP.NET Core):

HttpContext.Request.GetTypedHeaders().AcceptLanguage

Method 3

Inside your public class Startup:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //...

    app.Use((context, next) =>
    {
        //get client prefered language
        var userLangs = context.Request.Headers["Accept-Language"].ToString();
        var firstLang = userLangs.Split(',').FirstOrDefault();

        //set allowed alanguage
        var lang = "en"; //default
        switch (firstLang)
        {
            case "hy": //allowed
            case "ru": //allowed
                lang = firstLang; 
                break;
            default:
                //client language not supported
                lang = "en"; //use our default
                break;
        }

        //switch culture
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(lang);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        //save for later use
        context.Items["ClientLang"] = lang;
        context.Items["ClientCulture"] = Thread.CurrentThread.CurrentUICulture.Name;
        
        // Call the next delegate/middleware in the pipeline
        return next();
    });

    //... then goes app.UseMvc etc..

}

Method 4

as an additional option, Here is a full solution: Handle culture in route (URL) via RequestCultureProviders

as an option as well, here is a simplified code for CustomRequestCultureProvider:

options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(context =>
{
    var lang = context.Request.GetTypedHeaders().AcceptLanguage.FirstOrDefault()?.Value.Value ?? Constants.Languages.EN_US;

    return Task.FromResult(new ProviderCultureResult(lang, lang));

}));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x