JavaScript/jQuery - 获取文本并翻译它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5388127/
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
JavaScript/jQuery - Get text and translate it
提问by Charles Yeung
Is it possible to use jQuery to get a text from an element and translate it to other languages?
是否可以使用 jQuery 从元素中获取文本并将其翻译成其他语言?
Before
前
<p>Hello</p>
After
后
<p>bonjour</p>
回答by mfernandes
Use this JQuery plugin https://github.com/tinoni/translate.js
使用这个 JQuery 插件 https://github.com/tinoni/translate.js
Disclaimer: I am the author
免责声明:我是作者
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
回答by Hussein
Use Google translation API. Easy to use. The following translates Spanish to English. To translate from and to other languages, simply change 'es'
and 'en'
使用谷歌翻译 API。便于使用。以下将西班牙语翻译成英语。要从其他语言翻译成其他语言,只需更改'es'
和'en'
<div id="content"></div>
<div id="content"></div>
google.load("language", "1");
function initialize() {
var content = document.getElementById('content');
content.innerHTML = '<div id="text">Hola, me alegro mucho de verte.<\/div><div id="translation"/>';
var text = document.getElementById("text").innerHTML;
google.language.translate(text, 'es', 'en', function(result) {
var translated = document.getElementById("translation");
if (result.translation) {
translated.innerHTML = result.translation;
}
});
}
google.setOnLoadCallback(initialize);
Check working example at http://jsfiddle.net/wJ2QP/1/
在http://jsfiddle.net/wJ2QP/1/检查工作示例
回答by Mark Kahn
try google translate: http://code.google.com/apis/language/translate/overview.html
尝试谷歌翻译:http: //code.google.com/apis/language/translate/overview.html
回答by Levitikon
Use the Bing translator, since the free Google Translate API has been discontinued on December 1, 2011
使用 Bing 翻译器,因为免费的 Google Translate API 已于 2011 年 12 月 1 日停止使用
回答by Jhourlad Estrella
You can use Google Translate's Javascript API.
您可以使用谷歌翻译的 Javascript API。
<p id="some">Hello</p>
<input id="trans" value="Translate" type="button">
<script>
$('#trans').click(function() {
google.language.translate($('#some').html(), 'en', 'fr', function(result) {
$('#some').html(result.translation);
});
});
</script>
You will need to load the js library in your page's HEAD section in order to use the API.
您需要在页面的 HEAD 部分加载 js 库才能使用 API。
回答by Adrian P.
On this PHP/JS solution you should use include PHP language filesand set language on session/cookienot on $_GET. For the sake of simplicityI will do it on the
在此 PHP/JS 解决方案中,您应该使用包含 PHP 语言文件并在会话/cookie上设置语言,而不是在 $_GET 上。为简单起见,我将在
index.php file
index.php 文件
<?php
$lang = $_GET['lang'];
if ($lang == 'fr'){
$w = array(
'Trouvé',
' non trouvé.',
'Erreur. Veuillez réessayer.'
);
}else if($lang == 'en'){
$w = array(
'Found',
' not found.',
'Error. Please try again.'
);
}else{
$w = array(
'Trouvé',
' non trouvé.',
'Erreur. Veuillez réessayer.'
);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
....................
<body>
....................
<script type="text/javascript">
/* Translate JS
Declare JS variables for translation in PHP file as below (Global vars outside $(document).ready).
Inside JS file call the variable as $.lang_mynamespace.var_name
*/
$.lang_scan = {
found_js:"<?=$w[0];?>",
not_found_js:"<?=$w[1];?>",
error_js:"<?=$w[2];?>"
};
</script>
</body>
</html>
JS file
JS文件
$(function() {
$("#scan_result").on('change', function(){
//check number
$.ajax({
url: "check.php",
dataType: "json",
type: "post",
data: {'scan_no': scan_value} ,
success: function (response) {
if (response.status == true){
alert("Scan no. " + response.scan_no + $.lang_scan.found_js);
}else{
alert("Scan no. " + response.scan_no + $.lang_scan.not_found_js);
}
},
error: function(jqXHR, textStatus, errorThrown) {
//ajax error
alert($.lang_scan.error_js);
}
});
});
});
check.php return a json
check.php 返回一个 json
{"scan_no": "123", "status": true/false}
回答by Adrian P.
Why not try this:
为什么不试试这个:
var body = $("body");
var html = body.html();
var nhtml = html.split(" ");
var dict = [];
for (var i = 0; i < nhtml.length; i++) {
nhtml[i].replace(dict[index]);
}
It can replace anything.
它可以代替任何东西。