geeth
geeth

Reputation: 714

Google Translator default language

I'm trying to implement google transliteration in my website. It's working on all supported Indian languages. But when I select English it shows some error like 'Unsupported language en in targetLangCode array'. 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. But if destinationLanguage is set to 'en', then it shows error.

Upvotes: 4

Views: 2873

Answers (1)

Shyam Babu
Shyam Babu

Reputation: 1079

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

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

<option selected >Tamil</option>

Upvotes: 1

Related Questions