开发者

translation in JS?

开发者 https://www.devze.com 2022-12-21 18:07 出处:网络
Basically need to generate custom(some different开发者_StackOverflow then yes no) messeges(alert) in JS , how to deal with translation in it?Plese take a look at jQuery-i18n translation plugin

Basically need to generate custom(some different开发者_StackOverflow then yes no) messeges(alert) in JS , how to deal with translation in it?


Plese take a look at jQuery-i18n translation plugin

var my_dictionary = { 
    "some text"  : "a translation",
    "some more text"  : "another translation"
}
$.i18n.load(my_dictionary);


$('div#example').text($.i18n._('some text'));

And jQuery localisation

$.localise('js/greeting');
$('#greeting').val(greeting);
$('#languages').val($.localise.defaultLanguage);
$('#changeLocale').change(function() {
    var newLang = $(this).val();
    $.localise('js/greeting', {language: newLang, loadBase: true});
    $('#greeting').val(greeting);
    $('#languages').val(newLang);
});

Or, If you really want more translation than looking up data, try google translate api

google.language.translate("Hello world", "en", "es", function(result) {
  if (!result.error) {
    var container = document.getElementById("translation");
    container.innerHTML = result.translation;
  }
});


Use this JQuery plugin http://www.openxrest.com/translatejs

1 - Include the "trn" class to the text you want to translate:

<span class="trn">text to translate</span>

2 - Define a dictionary:

var dict = {
  "text to translate": {
    pt: "texto para traduzir"
  },
  "Download plugin": {
    pt: "Descarregar plugin",
    en: "Download plugin"
  }
}

3 - Translate the entire page body:

var translator = $('body').translate({lang: "en", t: dict}); //use English

4 - Change to another language:

translator.lang("pt"); //change to Portuguese
0

精彩评论

暂无评论...
验证码 换一张
取 消