Javascript 用JS翻译?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2377244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:59:24  来源:igfitidea点击:

translation in JS?

translationjavascript

提问by simple

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

基本上需要在 JS 中生成自定义(一些不同然后是不是)消息(警报),如何处理其中的翻译?

回答by YOU

Plese take a look at jQuery-i18ntranslation plugin

请看一下jQuery-i18n翻译插件

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

jQuery 本地化

$.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 translate api

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

回答by mfernandes

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

使用这个 JQuery 插件 http://www.openxrest.com/translatejs

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

1 - 将“trn”类包含到要翻译的文本中:

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

2 - Define a dictionary:

2 - 定义字典:

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

3 - Translate the entire page body:

3 - 翻译整个页面正文:

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

4 - Change to another language:

4 - 更改为另一种语言:

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