3

Is it possible to detect the language which is used by the mobile phone to auto switch my mobile site to that corresponding language?

Say for example, My website should display in english to peoples who have english as their language in mobile, similarly for user who have french they should view the site in french.

I have developed the mobile site with jQueryMobile. Its just a static site with HTML & some Js calculations that's all.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Logesh Paul
  • 7,600
  • 3
  • 26
  • 23
  • possible duplicate of [JavaScript for detecting browser language preference](http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference) - seems a rather complex problem. Once you have the language code, redirecting is trivial using `location.href` – Pekka Oct 12 '11 at 07:47
  • @pekka thanks for your reply. I'm not targeting desktop browsers because its a mobile only site. I'm trying to detect the language of mobile browser is there any useful information you can help with? – Logesh Paul Oct 12 '11 at 09:43
  • 2
    What does detecting the language have to do with mobile vs. desktop? The solutions presented in the duplicate should work in both worlds. Do they not? – Pekka Oct 12 '11 at 12:18

2 Answers2

5

You can detect the users language like this:

  var l_lang;
  if (navigator.userLanguage) // Explorer
    l_lang = navigator.userLanguage;
  else if (navigator.language) // FF
    l_lang = navigator.language;
  else
    l_lang = "en";

Then go to the language specific page:

  location.href = 'www.yourpage.com/' + l_lang + '.html';
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
0

I believe you can not do this with JS.

One of the option is to parse request's "Accept-Laguage", but do this with caution, cause there is could be several with different priority set. This can be done as with some server-side script as well as with apache's config.

YuS
  • 2,025
  • 1
  • 15
  • 24
  • Read this on navigator.language http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference – YuS Oct 12 '11 at 07:56