javascript – 谷歌翻译默认语言

我正在尝试在我的网站上实施谷歌音译.它正在使用所有支持的印度语言.但是当我选择英语时,它会显示一些错误,例如’targetLangCode array中的’Unsupported language en’.请帮我解决这个问题.

这是我的代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.js"></script>
<script type="text/javascript" src="jquery-2.2.3.min.js"></script>
<script>
    google.load("elements", "1", {
        packages: "transliteration"
    });
</script> 
</head>
<body>
<select id="languageOptions">
<option value="English">English</option>
<option value="Unicode">Unicode</option>
</select>

<select name="langpair" style="height:32px; padding:0; display:none" id="langpair" size="1">

    <option value="HINDI" selected>Hindi</option>
    <option value="BENGALI">BENGALI</option>
    <option value="TELUGU">Telugu</option>
    <option value="MARATHI">Marathi</option>
    <option value="TAMIL">Tamil</option>
    <option value="URDU">Urdu</option>
    <option value="KANNADA">Kannada</option>
    <option value="GUJARATI">Gujarati</option>
    <option value="MALAYALAM">Malayalam</option>
    <option value="PUNJABI">PUNJABI</option>
    <option value="SANSKRIT">SANSKRIT</option>
    <option value="NEPALI">Nepali</option>
    <option value="ARABIC">Arabic</option>
    <option value="SINDHI">Sindhi</option>
</select>
<br/>

<textarea class="form-control" maxlength="160" id="message" name="message" rows="3" placeholder="Message"></textarea>
<hr>

<br/>
<script>
$('#languageOptions).change(function() {
if($(this).val() == 'Unicode') {
$('#langpair').css('display', 'inline-block');
}
else {
$('#langpair').css('display', 'none');
}
)};
    var options = {
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true,
        sourceLanguage: 'en',
        destinationLanguage: ['hi'],
      };
      var control = new google.elements.transliteration.TransliterationControl(options);
       control.makeTransliteratable(['message']);

      $("#langpair").change(function() {

        var data = this.value;

        var destinationLanguage = google.elements.transliteration.LanguageCode[data];
        control.setLanguagePair('en', destinationLanguage);
      });

</script>
</body>
</html>

如何将默认语言设置为“英语”.我试图将sourceLanguage和destinationLanguage更改为其他语言,并且其工作正常.但是如果destinationLanguage设置为’en’,那么它会显示错误.

最佳答案 您的代码失败,因为您不能使源语言和目标语言相同.从英语音译到英语没有意义.它不是有效的一对

确切的错误是

Unsupported sourceLanguage & targetLanguage pair: sourceLanguage: en targetLanguage: en

默认的源语言是英语,您无需设置它.来自官方文件

sourceLanguage is a mandatory string that specifies the source language using the LanguageCode enum (as in google.elements.transliteration.ENGLISH). Currently, English is the only supported source language.

并且您只设置目标语言默认.因此,您必须在HTML中选择一种默认语言,您可以通过将选定属性添加到选项标记来实现.

<option selected >Tamil</option>
点赞