代码之家  ›  专栏  ›  技术社区  ›  Prashanth Benny

如何在Google音译中动态地改变语言?

  •  0
  • Prashanth Benny  · 技术社区  · 6 年前

    我在这里遇到了一个问题,差不多一天过去了。 我有一个语言选择框和一个文本区域框。

    以下是HTML:

    <select id="language" class="form-control">
        <option id="1">HINDI</option>
        <option id="2">KANNADA</option>
        <option id="3">BANGLA</option>
    </select>
    <textarea class="form-control" type="text" id="TextArea" maxlength ="500" placeholder="Enter text here..."></textarea>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    

    这里的脚本是:

    // Load the Google Transliterate API
    google.load("elements", "1", {
        packages: "transliteration"
    });
    
    var language = $('#language option:selected').val().toUpperCase();
    
    function onLoad() {
        debugger;
        var options = {
             sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH,
             destinationLanguage:[google.elements.transliteration.LanguageCode[language]],
             shortcutKey: 'ctrl+g',
             transliterationEnabled: true
        };
        var control = new google.elements.transliteration.TransliterationControl(options);
        control.makeTransliteratable(['TextArea']);
    }
    
    google.setOnLoadCallback(onLoad);
    
    //change the language on dropdown change
    $('#language').on('change', function(event){
        debugger;
        language = $(this,':selected').val().toUpperCase();
        google.setOnLoadCallback(onLoad);
    });
    

    这里的问题是它不会弹出任何错误。 音译API也不起作用。

    因为一种语言,当它是静态的,就像一个魅力。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Prashanth Benny    6 年前

    在Google音译API中有一个处理语言变化的专用函数。

    这是密码

    <select id="language" class="form-control">
        <option id="1">HINDI</option>
        <option id="2">KANNADA</option>
        <option id="3">BANGLA</option>
    </select>
    <textarea class="form-control" type="text" id="TextArea" maxlength ="500" placeholder="Enter text here..."></textarea>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    

    JS代码:

    // Load the Google Transliterate API
    google.load("elements", "1", {
        packages: "transliteration"
    });
    
    var language = $('#language option:selected').val().toUpperCase();
    var control;
    
    function onLoad() {
        var options = {
             sourceLanguage: google.elements.transliteration.LanguageCode.ENGLISH,
             destinationLanguage:[google.elements.transliteration.LanguageCode[language]],
             shortcutKey: 'ctrl+g',
             transliterationEnabled: true
        };
        control = new google.elements.transliteration.TransliterationControl(options);
        control.makeTransliteratable(['TextArea']);
    }
    
    google.setOnLoadCallback(onLoad);
    
    //change the language on dropdown change
    $('#language').on('change', function(event){
        language = $(this,':selected').val().toUpperCase();
    
        //function to change the language dynamically(Google API)
        control.setLanguagePair(
            google.elements.transliteration.LanguageCode.ENGLISH,
            google.elements.transliteration.LanguageCode[language]);
    });
    

    希望这有帮助。