简体   繁体   English

Google翻译默认语言

[英]Google Translator default language

I'm trying to implement google transliteration in my website. 我正在尝试在我的网站中实施google音译。 It's working on all supported Indian languages. 它适用于所有受支持的印度语言。 But when I select English it shows some error like ' Unsupported language en in targetLangCode array '. 但是,当我选择英语时,它会显示一些错误,例如“ targetLangCode数组中语言不受支持 ”。 Please help me to resolve this issue. 请帮助我解决此问题。 This is my code : 这是我的代码:

<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>

How can i set default language to 'English'. 如何将默认语言设置为“英语”。 I tried to change sourceLanguage and destinationLanguage to other languages, and its working. 我试图将sourceLanguage和destinationLanguage更改为其他语言,并且可以正常工作。 But if destinationLanguage is set to 'en', then it shows error. 但是,如果destinationLanguage设置为'en',则显示错误。

Your code is failing because you cannot have source language and target language be the same. 您的代码失败,因为您不能使源语言和目标语言相同。 It doesn't make sense to transliterate from English to English. 从英语译成英语没有意义。 it is not a valid pair 这不是有效的一对

The exact error is 确切的错误是

Unsupported sourceLanguage & targetLanguage pair: sourceLanguage: en targetLanguage: en 不支持的sourceLanguage和targetLanguage对:sourceLanguage:zh_targetTargetLanguage:zh_cn

The default source language is english you do not have to set it. 默认的源语言是英语,您无需设置。 From official doc 来自官方文件

sourceLanguage is a mandatory string that specifies the source language using the LanguageCode enum (as in google.elements.transliteration.ENGLISH). sourceLanguage是必填字符串,它使用LanguageCode枚举指定了源语言(如google.elements.transliteration.ENGLISH中所示)。 Currently, English is the only supported source language. 当前,英语是唯一受支持的源语言。

And you are setting the destination language default only.So you have to choose one Indian language as default in HTML you can do it by adding selected attribute to option tag. 并且您仅将目标语言设置为默认语言,因此您必须在HTML中选择一种印度语言作为默认语言,您可以通过在选项标签中添加所选属性来做到这一点。

<option selected >Tamil</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM