how to set site culture according to visitor location or region?

Muhammad Kaleem asked on May 30, 2019 08:53

i'm using kentico mvc and i want to set culture of site dynamically on basis of visitor location, for example if user visit site from USA then culture of site should be "en-US" and if user visit site from Spain then culture should be "es-ES", please suggest...

thanks

Recent Answers


Mike Wills answered on May 30, 2019 21:13 (last edited on May 30, 2019 21:19)

Muhammad,

You can mimic what Kentico does in portal engine sites, by detect a site visitor's culture based on the visitor's browser settings. This will provide site visitors a better experience. For example, for site visitors from Canada, French speakers will have browsers configured for French, and English speakers will have browsers configured for English. This works better for many countries. The US is another example, there are many people that prefer other languages to English, by using browser settings, you would server them better as well.

To use browser settings, you simply need to add all the cultures that you want to support to the site, and then detect the visitor’s culture by checking the “accept-language” request header. You may also want to give users a way to override the automatically detected culture, by providing a UI for them to select their preferred language. In Portal Engine sites, Kentico also checks the CMSPreferredCulture cookie, which can be different than the “accept-language” request header.

To test this, you can use the ModHeader extension in Chrome to override your "accept-language" request header.

For more information, see https://docs.kentico.com/k12/multilingual-websites/setting-up-multilingual-mvc-projects.

Mike

0 votesVote for this answer Mark as a Correct answer

Muhammad Kaleem answered on May 31, 2019 07:45 (last edited on May 31, 2019 07:46)

Thanks Mike,

i have already added required cultures that i needed, and current default culture is "en-US" i also read the documentation, according to documentation my site working fine, for example

when use opens a news page www.abc.com/en-US/news i'm getting and displaying cotents accordingly, but i want to ask, how this default culture will be change when a visitor visits from spain then culture code will be change from "en-US" to "es-ES",can you please share example code, by which i can detect the visitor’s culture by checking the “accept-language” request header...

thanks

0 votesVote for this answer Mark as a Correct answer

Mike Wills answered on June 3, 2019 21:38

Hi Muhammad,

You can use ASP.Net's UserLanguages property to check the user's preferred language. This is basically reading the "accept-languages" request header, sent by the browser. After getting the preferred language, you would set Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture properties.

Here's an example method that gets the preferred culture:

/// <summary>
/// Get the current user's preferred locale. 
/// </summary>
/// <returns></returns>
private string GetUserPreferredLocale()
{
    string preferredLocale = "en-US";
    var userLanguages = Request.UserLanguages;
    if(userLanguages != null && userLanguages.Length > 0)
    {
        preferredLocale = userLanguages[0];
    }
    return preferredLocale;
}

And, here's an example of code that sets the culture:

var preferredCulture = GetUserPreferredLocale();
var cultureInfo = CultureInfo.CreateSpecificCulture(preferredCulture); // ja-JP, zh-CN, en-US, de-DE
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;        

I hope this helps.

Mike

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.