如何从JavaScript动态更改google transliterate API中的语言

时间:2017-05-23 12:54:57

标签: javascript google-api google-api-client google-translate jsapi

我只能更改一次语言(加载页面后第一次)。如果我在第一次选择后选择另一种语言,则语言不会改变。 这是我的代码

localStorage.getItem("prueba");

 <select id="selectedLanguageId" onchange="changeLanguage(this.value)">
<option value="ENGLISH" selected="selected" >English</option>
<option value="HINDI">Hindi</option>
<option value="TAMIL">Tamil</option>
     <option value="MALAYALALM">Malayalam

1 个答案:

答案 0 :(得分:0)

这可能对你有所帮助!

<script type="text/javascript">


  // Load the Google Transliterate API
  google.load("elements", "1", {
        packages: "transliteration"
      });

  function changelanguage(lan) {

   if (lan == 'Arabic') {alert (" لا تنس أن تضغط G+ctrl قبل البدء بكتابة اللغة العربية");}

  switch (lan){
    case "Hindi":
     var options = {
        sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage:
            [google.elements.transliteration.LanguageCode.HINDI],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
    };
    break;

    case "Arabic":

     var options = {
        sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage:
            [google.elements.transliteration.LanguageCode.ARABIC],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
    };
    break;

    case "Malayalam":
    var options = {
        sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage:
            [google.elements.transliteration.LanguageCode.MALAYALAM],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
    };
    break;

    default:
     var options = {
        sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage:
            [google.elements.transliteration.LanguageCode.ENGLISH],
        shortcutKey: 'ctrl+g',
        transliterationEnabled: true
    };

  }



    // Create an instance on TransliterationControl with the required
    // options.
    var control =
        new google.elements.transliteration.TransliterationControl(options);

    // Enable transliteration in the textbox with id
    // 'transliterateTextarea'.
    control.makeTransliteratable(['stdTextArea']);

  }


</script>