1

Im creating a web app and Im already on the part where I need to know the language used by the computer (ex. 'en-US', ja) and change the language of the app based on what the client use.

How do I do it using javascript?

srh snl
  • 797
  • 1
  • 19
  • 42
  • 1
    Here are a few possible solutions I found: [1](http://stackoverflow.com/questions/11889925/get-locale-language-of-website-visitor), [2](http://stackoverflow.com/questions/2678230/how-to-getting-browser-current-locale-preference-using-javascript), [3](http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference) – Ernest Mallett Mar 12 '14 at 03:55

2 Answers2

2

There are limitations when trying to do localization with just JavaScript. The bulk of your effort should be on the server side to support your application. Are you using pure HTML/JavaScript or do you have a server side language as well?

Look into the navigator object. It's a better approach to check the Accept-Language in the header, however I don't believe you can do this with just JavaScript.

Cory
  • 658
  • 3
  • 7
  • 19
  • Now i know what you meant with "There are limitations when trying to do localization with just JavaScript." and "however I don't believe you can do this with just JavaScript." The server side language used is C but I dont have any idea how to manipulate the language of client with the use of the server.I'm having issue w/ changing the language of the webapp based on the choice of the user(I made a dropdown that would let the user choose the lang). saving the preferred lang to cookie and reloading the page didnt work.Cant think of a way to load the page with language that the user want. – srh snl Mar 13 '14 at 07:12
0

Unfortunately, there is no way to access OS locale through JavaScript. What JavaScript gives you is user most preferred language (not necessary the locale, that is you might not get country part). To access this information you may want to use the following code:

var locale = navigator.userLanguage || navigator.language;

Personally, I wouldn't use it, for there is no fall-back mechanism. So, for instance if you have no translations for given language, you are most likely to use your application's default language, whether or not the user understands it. The better way, as Cory suggested is to use server-side language negotiation, but it has its drawbacks as well. However, on the server side you will be access Accept-Language header which looks similar to this:

   Accept-Language: da, en-gb;q=0.8, en;q=0.7

This is the list of preferred language tags and if it happens that your application does not support user's most preferred language, but it does support one in this list, it makes sense to use the latter for User Interface translations.
Conversely, you would like to use the most preferred locale for formatting (dates, numbers, etc.).

Last but not least, in any sufficiently complex application you would have some means of a user profile. The best practice is to put User Interface language as well as formatting language settings in their and use Accept-Language as a source for valid defaults.

Paweł Dyda
  • 18,366
  • 7
  • 57
  • 79