0

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).

3
  • whoever voted to close this, doesn't get the issue at all, this is not argumentative/subjective but rather it is a search for possible solutions or the wealth of knowledge that SO members possess. Some people here are just clicking the links like braindead robots. Commented Jul 27, 2010 at 17:37
  • ouch. IMHO your type of question falls into a "stream of consciousness" category where the questioner asks many things over a long description of a problem or architecture. My experience is that these questions quickly go into discussion mode during which the questioner's many other esoteric requirements are brought to light. All the answers become a ambiguous mess of inaccurate ideas or suggestions. Sorry I offended you. Just trying to make SO a better place. Commented Jul 27, 2010 at 23:42
  • Hey jfar, that's fine, I can see your concern over many questions of this type, however I think it is without grounds because so far none of those questions generated a ton of answers or discussions. In fact, this one only has 1 answer so far. Commented Jul 27, 2010 at 23:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.