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