2

这是用于在 ASP.Net 页面中集成 Google 音译代码的示例(随处可用)代码。

但我的问题是,如何在运行时生成的 TextBoxes 中启用音译?此脚本需要文本框的 ID 以应用音译。但是我的文本框将在运行时生成。

这行代码需要一个替代方案:
control.makeTransliteratable(['transliterateTextarea']);

  //Script Starts here

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

  function onLoad() {
    var options = {
        sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
        destinationLanguage:
            [google.elements.transliteration.LanguageCode.HINDI],
        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(['transliterateTextarea']);
  }
  google.setOnLoadCallback(onLoad);

 //End here
4

2 回答 2

2

首先,您必须将所有文本框类名称设置为hindiFont。

使用此代码:

google.load("elements", "1", {
    packages: "transliteration"
});

function onLoad() {
    var options = {
        sourceLanguage: [google.elements.transliteration.LanguageCode.ENGLISH],
        destinationLanguage: [google.elements.transliteration.LanguageCode.HINDI],
        transliterationEnabled: true,
        shortcutKey: 'ctrl+g'

    };

    var control = new google.elements.transliteration.TransliterationControl(options);

    $('.hindiFont').each(function(){
        var id = this.id;
        control.makeTransliteratable([id]);
    })
}
google.setOnLoadCallback(onLoad);
于 2015-01-30T13:38:01.333 回答
2

使用 RegisterStartupScript。RegisterStartupScript 将在页面完全加载后执行。

function EnableTransalation(ctrlId) {
    //Script Starts here

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

    function onLoad() {
        var options = {
            sourceLanguage:
            google.elements.transliteration.LanguageCode.ENGLISH,
            destinationLanguage:
            [google.elements.transliteration.LanguageCode.HINDI],
            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(["'" + ctrlId + "'"]);
    }
    google.setOnLoadCallback(onLoad);

    //End here
}

在后面的代码中,

protected override void OnPreRender(EventArgs e)
{
  Page.ClientScript.RegisterStartupScript(GetType(), "EnableTransalation", "EnableTransalation('" + ctrl.ClientID + "')", true);
}
于 2013-06-04T06:30:36.160 回答