google翻译api翻译页面使用js

时间:2018-06-06 11:13:02

标签: javascript jquery api google-translate

我想使用js更改页面语言我正在使用谷歌翻译。

使用我的代码,下拉值正在改变,但我认为我们需要触发 任何ajax所以它会被翻译。 请建议任何解决方案

我正在尝试此代码,但它无效。

$('.goog-te-combo').val('ur').trigger('change');
$('.goog-te-combo').find('select').trigger('change');

这是我的代码

<!DOCTYPE html>
<html>
 <head>

  <script type="text/javascript">
  function googleTranslateElementInit() {
    new google.translate.TranslateElement({pageLanguage: '/en/ur'}, 'google_translate_element');
  }
  </script>
  <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

</head>
<body>

<p>You can translate the content of this page by selecting a language in the select box.</p>


<h1>My Web Page</h1>
<p>Hello everybody!</p>
<p>Translate this page:</p>
<div id="google_translate_element"></div>




<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(window).bind('load', function()
{
    $('.goog-te-combo').val('ur');

});
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

根据您提供的代码,似乎您从未进行过API调用来请求翻译某些文本。以下documentation详细介绍了API调用所需的查询格式。您需要指定在查询中需要翻译的网页部分。转换后的字符串将按照传递的相同顺序以数组形式返回。以下是根据您提供的内容提供的示例代码:

<body>
    <p id="textField">You can translate the content of this page by selecting a language in the select box.</p>
    <h1 id="title">My Web Page</h1>
    <p>Hello everybody!</p>
    <p>Translate this page:</p>
    <form>
        <select id="targetLanguage">
            <option value="ZH">Chinese (Mandarin)</option>
            <option value="CS">Czech</option>
            <option value="DA">Danish</option>
            <option value="NL">Dutch</option>
            <option value="EN">English</option>
            <option value="ET">Estonian</option>
            <option value="FR" selected = "selected">French</option>
        </select>

        <input type="button" id="translateButton" value="Translate" />
    </form>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("#translateButton").click(function () {

            var url = "https://translation.googleapis.com/language/translate/v2";
            //Strings requiring translation
            url += "?q=" + escape($("#textField").text());
            url += "&q=" + escape($("#title").text());
            //Target language
            url += "&target=" + $("#targetLanguage").val();
            //Replace with your API key
            url += "&key=YOUR_API_KEY";
            $.get(url, function (data, status) {
                //Results are returned in an array following the order they were passed. 
                $("#textField").text(data.data.translations[0].translatedText);
                $("#title").text(data.data.translations[1].translatedText);
            });       
        });
    </script>  
</body>

您可以通过在下拉菜单中包含更多目标语言来添加它们。这是受支持的语言及其代码的完整list