2012-03-02 18 views
7

Sto sviluppando un'applicazione HTML5 con CSS3 e jQuery.Applicazione HTML5 multi-lingua

Ho un testo che voglio mostrare nella lingua dell'utente. Sai come posso farlo?

Questa applicazione verrà eseguita su Blackberry Playbook e su altri dispositivi mobili.

non so come ottenere la lingua del sistema operativo dell'utente, o dove devo mettere le mie corde localizzare, ecc

Qualsiasi indizio?

risposta

4

devo conservare il mio stringa localizzata nel file JS (1 per ogni lingua supportata). Ex. :

stringa-en.js:

MyApp.STR = {"Hi":"Hi","By":"By", etc.}; 

stringa-fr.js:

MyApp.STR = {"Hi":"Salut","By":"Par", etc.}; 

E all'avvio, si carica il file giusta per quanto riguarda la lingua del navigatore:

loadLocalizedString: function(langParam/*optional*/) { 
    var language = window.navigator.language, lang; 
    console.log('loadLocalizedString with Navigator Language: ' + language); 
    if (!langParam) { 
     //Try to guess the best suited language 
     if(language) { 
      lang = language.substring(0,2); 
     } else { 
      lang = 'en'; 
     } 
     if($.inArray(lang, this.SUPPORTED_LANGUAGE) <= -1) { 
      lang = 'en';//If the language is not available : english by default 
     } 
    } else { 
     lang = langParam; 
    } 

    console.log('language: ' + lang); 

    this.loadString('lib/string-'+lang+'.js'); 
}, 
    SUPPORTED_LANGUAGE : ["en", "fr", "es", "it"], 
loadString:function(fileName) { 
    try { 
     $.ajaxSetup({async: false}); 
     $.getScript(fileName);//We don't use the async callback, because we need the translation in the next method 
     $.ajaxSetup({async: true}); 

    } catch (e) { 
     console.error('Error while loading : ' + fileName); 
    } 

} 

E per utilizzare la stringa localizzata nell'app:

html = MyApp.STR.Hi+' '+userName+' !';