Reputation: 2317
I have a single MVC5 site which is accessed via several different regional URLs. In my case .co.uk (for the UK), .de (for Germany) and .fr (for France).
The site content is localised using RESX files and users can switch language via a cookie for persistence and a HttpModule which sets the asp.net thread locale based on the cookie (I used this approach).
I want the default language to be relevant to the top-level domain the user is accessing the site as. For example if a user is on .de, the default language should be de-DE. The user may choose to change the language in which case the default is overwritten, but it is very important that the default language is appropriate to the top-level domain (for users and search engines).
How can I achieve this in MVC5? The best I have got to so far is using JavaScript to check the url, set the cookie and refresh the page, but i know this is nasty and there must be a better way.
PS: Please note it is the top level domain that I want to drive this. I'm not using regional routing, for example http://whatever.com/DE or http://whatever.com/EN
PPS: I do not want to use the browser language detection feature either because that causes problems for search engines. i.e. it may cause the .de site to show in en-GB because that is what the search engine uses (or the search engine has no language so that is the default). If that happens the .de site will be treated as a duplicate of the .co.uk site which is never good for SEO
Upvotes: 0
Views: 1238
Reputation: 96
In my case I set Persian cluture in global.asax and works well
protected void Application_BeginRequest(object sender, EventArgs e)
{
var persianCulture = new PersianCulture();
persianCulture.DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
persianCulture.DateTimeFormat.LongDatePattern = "dddd d MMMM yyyy";
persianCulture.DateTimeFormat.AMDesignator = "صبح";
persianCulture.DateTimeFormat.PMDesignator = "عصر";
Thread.CurrentThread.CurrentCulture = persianCulture;
Thread.CurrentThread.CurrentUICulture = persianCulture;
}
Upvotes: 0
Reputation: 2317
I figured out how to do this. Add this to global.asax
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (Request.Cookies[Constants.LanguageCookieName] == null)
{
var culture = GetCultureFromHost();
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
}
}
private CultureInfo GetCultureFromHost()
{
//set default culture of en-GB
CultureInfo ci = new CultureInfo("en-GB");
//get top level domain
string host = Request.Url.Host.ToLower();
//check for other known domains and set culture accordingly
if (host.Contains("whatever.de"))
{
ci = new CultureInfo("de-DE");
}
return ci;
}
Upvotes: 2