-1

I have a alert message,

alert('Hi') 

it prints, Hi in the alert window.

I want that message Hola to be printed when someone whose IE is set to Spanish.

How can I go about doing that please?

Thank you

tbraun89
  • 2,246
  • 3
  • 25
  • 44
007
  • 2,136
  • 4
  • 26
  • 46
  • You should check this question, it contains a lot of very useful information for you: http://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser – Serdalis Nov 13 '12 at 22:25
  • Don't you also need a list of the proper translations of text from English to Spanish? So if the user's language is Spanish, then use the Spanish list of text. Otherwise, use the English list of text. – Jon Nov 13 '12 at 22:29
  • Thanks Serdalis for the reference. Jon, this is an example. There are few other text (sentences) that I will need to translate and I have asked for proper translations to my team. – 007 Nov 13 '12 at 22:44

2 Answers2

3

Check this:

browserLanguage property

<script type="text/javascript">
window.onload = function() {
   var language = window.navigator.userLanguage || window.navigator.language;
   alert(language); //works IE/SAFARI/CHROME/FF
}
</script>

Once you have detected language you can have a case to switch the alert message.

Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • Always suspicious when people point reference links to MSDN. What if browser doesn't support `getElementById`? What kind of cross-browsers support in general is available for `navigator.browserLanguage`? – Madbreaks Nov 13 '12 at 22:26
  • Umm, that looks neat but kind of confusing for a newbie. Can you explain the code please? – 007 Nov 13 '12 at 22:45
  • You declare this script on the html and it will run on page load, the var (language) will store the browser language and then you can (case) the value and alert different messages for the assigned language. – Carlos Landeras Nov 13 '12 at 22:47
3

This should work in most browsers:

var languageinfo = navigator.language ? navigator.language : navigator.userLanguage;
if(languageinfo.split('-')[0] == 'es') {
    alert('hola')
} else {
    alert('hi');
}
Candide
  • 30,469
  • 8
  • 53
  • 60