Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlug] Language localization in Javacript



On Sep 28, 2011, at 5:19 PM, Lewske Wada wrote:
> On Sep 28, 2011, at 4:40 PM, Martin G wrote:
>> How would I set up a conditional for three or more possible languages?
>> I'm likely to have a dozen or more languages available for this
>> application.
> 
> Why don't you add more branches in the switch-case statement?
> 
> switch (navigator.language.substr(0, 2)) {
>  case "en" :
>    document.writeln("Hello.");
>    break;
>  case "de" :
>    document.writeln("Guten Tag.");
>    break;
>  case "fr" :
>    document.writeln("Bonjour.");
>    break;
>  case "es" :
>    document.writeln("Hola!");
>    break;
>  case "pt" :
>    document.writeln("Boa tarde.");
>    break;
>  case "ru" :
>    document.writeln("Здравствуйте!");
>    break;
>  case "ja" :
>    document.writeln("こんにちは.");
>    break;
> }


For multi-platform (multi-browser) enhancement,
you may want to create and use your own function like this :

function getBrowserLanguage() {
  if (document.all)                       // for e4,e5,e6,o6,o7
    return navigator.browserLanguage
  else if (document.layers)               // for n4
    return navigator.language
  else if (document.getElementById)       // for n6,n7,m1,s1
    return navigator.language.substr(0,2)
}

switch (getBrowserLanguage()) {
........ whatever goes on.

Anyway it's always complicated when it comes to supporting
different browsers.

Cheers,
Lewske Wada
Web: http://run.sh/
Email: ryu@example.com
Facebook: Lewske Wada
Skype: lewske




Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links