tags:

views:

56

answers:

1

This is a question about setting our website's Language and Culture settings with regards to the settings we read from the user visiting the site.

Let's assume our website supports 2 languages, English (en) and German (de). Let's also assume we want to disregard locale (region) (at least on the server side, so we only know that we support "en" and "de", so we have that specified either in application code, config file or somewhere elese). So we don't care if a user comes from US or UK.

What we are doing is matching "en" or "de" to possible matches in user's browser defined languages/cultures.

The issue I am having is that if I do this

        /* Gets Languages from Browser */
        IList<string> BrowserLanguages = filterContext.RequestContext
            .HttpContext
            .Request
            .UserLanguages;

we get all sorts of results.

We might receive lists like

  • en, (for instance Firefox has this), - en-US, - en-UK.

  • en-US, - en-UK.

  • en, - de, - it-IT.

  • de, - en-US, - en.

What I would like to ask here is:

  1. Is it ok to use compare strings here (checking whether "en" exists as a substring)? See sample list 2

  2. Do we have to take the order into account or would you just disregard it?

  3. Am I overcomplicating this? The problem is though that IE and Firefox (and others) have different strings for regional settings (for instance, "sl" in Firefox and "sl-SI" in IE8)

I just want to direct all visitors for which language does not exist to English and all others to their appropriate language (disregarding their location), you might think of it like if we support Portugese (pt) and our visitors come from Portugal and Brazil we will redirect them to Portugese version of the site even if the match is not 100% perfect (we would rather redirect them to Portugese version than English version).

+1  A: 

Interesting question. Let me try to answer...

Is it ok to use compare strings here (checking whether "en" exists as a substring)?

You could something like this. Note, I am just providing a way that does not use strings, however, I think that in this case substring approach will also work since its simpler.

CultureInfo enCulture = new CultureInfo("en"); // use "de"
var langPref = Request.UserLanguages[0];
var userCulture = CultureInfo.GetCultureInfo(langPref);
var baseCulture = CultureInfo.GetCultureInfo(cult.TwoLetterISOLanguageName); // get the base culture
var isSame = baseCulture.Equals(enCulture);

What about using the Headers["Accept-Language"]. Section 14.4 Accept-Language of RFC 2616. There may be a bit more work involved using this, but off hand it seems that that it can hold more valuable information.

Do we have to take the order into account or would you just disregard it?

The UserLanguages array is sorted by preference (MSDN). Having said that, I would assume that each browser has its own specific way to create the Language String (I stand under correction, but I think that FF4 is considering removal of this part of the user-agent string). You could check each language and decide when the correct language is found using the approach described above.

Am I overcomplicating this? The problem is though that IE and Firefox (and others) have different strings for regional settings (for instance, "sl" in Firefox and "sl-SI" in IE8)

To me localisation is tricky. I would suggest having read through RFC 1766 and RFC 2616 (HTTP Protocol, Section 3.10.

I hope this helps.

Ahmad
Nice answer, thanks.
mare