javascript 如何在不使用组合框更改语言的情况下在网站上使用谷歌翻译器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20998646/
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
How to use google translator on a web site without using the combo box to change language?
提问by Magic AndWebDev
I want to add the multi languages features on my website so I've added the google translate tool (Translate Tool) but I want to translate the website only when the user click on a button in the page and not when it uses the combo! Is there any way to change automatically the value of the combo when a button is clicked?
我想在我的网站上添加多语言功能,所以我添加了谷歌翻译工具(翻译工具),但我只想在用户点击页面中的按钮而不是使用组合时翻译网站!单击按钮时,有什么方法可以自动更改组合的值?
This is the code of the translate tools:
这是翻译工具的代码:
<div id="google_translate_element"></div><script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({ pageLanguage: 'it', autoDisplay: false },'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
I'm trying to change the value of the containing the languages.
我正在尝试更改包含语言的值。
<script>
window.onclick = function() {
var container = document.getElementById('google_translate_element');
var select = container.getElementsByTagName('select')[0];
select.value="en";
}
</script>
This code works and the selected value changes but only that! The page is not translated. I think that I have to validate this operation! Anyone knows how I can do that?
此代码有效并且选定的值会更改,但仅此而已!该页面未翻译。我想我必须验证这个操作!有谁知道我怎么能做到这一点?
回答by Cynthia Fridsma
I created a PHP class for an open source project:
我为一个开源项目创建了一个 PHP 类:
---// source code of google_translate.php //----
---// google_translate.php 的源代码 //----
<?php
class google_translate{
public $original_language;
public $translate;
public $domain;
function __construct ($original_language, $translate, $domain){
$this->original_language = $original_language;
$this->translate = $translate;
$this->domain = $domain;
}
function translate(){
$url = "http://translate.googleusercontent.com/translate_c?act=url&depth=1&hl=" . $this->original_language;
$url .= "&ie=UTF8&prev=_t&rurl=translate.google.com&sl=" . $this->original_language;
$url .= "&tl=". $this->translate;
$url .= "&u=" . $this->domain;
echo "<script language=\"javascript\">document.location=\"$url\"</script>";
}
}
?>
---// source code of google_translate.php //----
---// google_translate.php 的源代码 //----
This script works in combination with another script, named translate.php.
此脚本与名为 translate.php 的另一个脚本结合使用。
---// source code of translate.php //----
---// translate.php的源代码 //----
<?php
include_once("include/google_translate.php");
?>
<form method="post">
<table width="640" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/language/choose_your_language.jpg" width="640" height="360" /></td>
</tr>
<tr>
<td valign="top" align="left">
<select name="language">
<?php echo $default_language; ?>
<option value="ar">Arabic</option>
<option value="be">Belarusian</option>
<option value="bn">Bengali</option>
<option value="bs">Bosnian</option>
<option value="bg">Bulgarian</option>
<option value="ca">Catalan</option>
<option value="zh-CN">Chinese (China)</option>
<option value="zh-TW">Chinese (Taiwan)</option>
<option value="hr">Croatian</option>
<option value="cs">Czech</option>
<option value="da">Danish</option>
<option value="nl">Dutch</option>
<option value="en">English</option>
<option value="fil">Filipino</option>
<option value="fi">Finnish</option>
<option value="fr">French</option>
<option value="de">German</option>
<option value="el">Greek</option>
<option value="gu">Gujarati</option>
<option value="he">Hebrew</option>
<option value="hi">Hindi</option>
<option value="hu">Hungarian</option>
<option value="id">Indonesian</option>
<option value="it">Italian</option>
<option value="ja">Japanese</option>
<option value="kn">Kannada</option>
<option value="ko">Korean</option>
<option value="lv">Latvian</option>
<option value="lt">Lithuanian</option>
<option value="mr">Marathi</option>
<option value="no">Norwegian</option>
<option value="fa">Persian</option>
<option value="pl">Polish</option>
<option value="pt-BR">Portuguese (Brazil)</option>
<option value="pt-PT">Portuguese (Portugal)</option>
<option value="ro">Romanian</option>
<option value="ru">Russian</option>
<option value="sr">Serbian</option>
<option value="sk">Slovak</option>
<option value="sl">Slovenian</option>
<option value="es">Spanish</option>
<option value="sv">Swedish</option>
<option value="sw">Swahili</option>
<option value="ta">Tamil</option>
<option value="te">Telugu</option>
<option value="th">Thai</option>
<option value="tr">Turkish</option>
<option value="uk">Ukrainian</option>
<option value="vi">Vietnamese</option>
</select>
</td>
</tr>
<tr>
<td valign="top" align="left">
<input name="submit" type="submit" value="Translate" /></td>
</tr>
</table>
</form>
---// source code of translate.php //----
---// translate.php的源代码 //----
And below you see an example of the code how it's used in Horizon QCMS:
下面你会看到一个代码示例,它是如何在 Horizon QCMS 中使用的:
---// how to use the code //----
---// 如何使用代码 //----
<?php
include_once('translate.php');
# Google translator
# script generated with Horizon QCMS 4
if(isset($_POST['submit'])){
$language='en';
$translate = new google_translate('en', $_POST['language'], 'http://www.hnqcms.com/wiki/');
$translate->translate();
}
---// how to use the code //----
---// 如何使用代码 //----
A working example can be found here: http://www.hnqcms.com/wiki/index-page-28-category-28.html
一个工作示例可以在这里找到:http: //www.hnqcms.com/wiki/index-page-28-category-28.html
P.s. You can of course adjust the script a bit so that the user doesn't have to click on the "submit" button.
Ps 你当然可以稍微调整一下脚本,这样用户就不必点击“提交”按钮。
回答by kiki
I posted this answerwhich may help you. It uses two javascript functions to set and get the cookie 'googtrans'. If present, Google Website Translator gets its value and translates the page accordingly. The case in the answer applies to just one flag in a link to change language, but it can easily be extended.
我发布了这个答案,可能会对您有所帮助。它使用两个 javascript 函数来设置和获取 cookie 'googtrans'。如果存在,Google 网站翻译器会获取其价值并相应地翻译页面。答案中的情况仅适用于更改语言链接中的一个标志,但可以轻松扩展。
回答by alessandrio
manually
手动
redirects to google translatetranslate button exampleonly works online
here code jsfiddle
重定向到谷歌翻译翻译按钮示例仅适用于在线
代码 jsfiddle
I hope that is what you want
我希望这就是你想要的