3

I'm looking for a way to localize a Windows Sidebar Gadget. It is written primarily in Javascript. What I specifically want is a way to get the user's "language code" (eg. en-us) because the language code will be used to get a localized XML file from a server.

If anyone has any ideas for how to get this language code or, even better, some code that accomplishes this task that would be great. Thanks in advance.

Edit: It looks like whoever asked for code deleted his comment, so I deleted the sample code as it was just clutter that didn't contribute.

user535617
  • 634
  • 2
  • 7
  • 22
  • So, do you need to get user's language? http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference – Fran Verona Feb 04 '11 at 20:40
  • @Fran Yes I do, and thank you, although I'm not 100% sure if the solutions given will work I am going to try them. I feel like there's got to be something in the gadget framework I'm not aware of. – user535617 Feb 04 '11 at 20:57
  • @user535617 More links: http://forums.digitalpoint.com/showthread.php?t=631706. You only can access to browser's language; it's impossible to go more deeper and to know OS's language without installing some plugin (ActiveX or similar). As far as I know :) – Fran Verona Feb 04 '11 at 21:04
  • @Fran, alright well the links you have given me have helped, one of the answers in your original link seems like it will serve my purpose. – user535617 Feb 04 '11 at 21:17

2 Answers2

1

It's not possible. You can't get that setting from the Browser, so it's not available to Javascript. Define a standard language and let the user to choose another.

Rafael S.
  • 21
  • 3
  • Actually navigator.userLanguage gives me what I want. I don't think it's technically from the browser but it works in Javascript and gives me what I'm looking for. – user535617 Feb 04 '11 at 21:18
1

There are several ways of accomplishing this, which method you wish to use depends on your needs and what your setup allows you.

  1. navigator.language will tell you what language the installed browser has - not what the user has chosen as language from the preferences tho. So this will only give you a hint, but it might be enough for you.

  2. Geolocation. There are several APIs you can use for this, but again, they will not tell you the browsers language preference set by the user, but where they are in the world. again, this might be what you need and might not.

  3. On the server look at the request headers for the js-file/html-page/etc. Specifically, look at the Accept-Language header which reflects the users desired language preference as set in the browser. From your server, seed your js with this in a variable. Alas, this value cannot be read from javascript itself, so you need the server for it.

Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
  • Number 1 is what I'm going with since 3 is out of the question and 2 will not be nearly worth the effort. I have already found this navigator.language thanks to Fran but you did formally post this answer, so win for you sir. – user535617 Feb 04 '11 at 21:34
  • +1 - btw, `navigator.userLanguage` will give you the user's chosen language. `navigator.language` isn't implemented in Internet Explorer and, subsequently, Windows Desktop Gadgets. – Andy E Feb 08 '11 at 15:47