2

Here's the sample (available everywhere) code for integrating Google Transliteration code in ASP.Net Pages.

But my question is, how to enable transliteration in TextBoxes which will be generated on runtime? This script demands the ID of the textbox for applying Transliteration. But my textboxes will be generated on runtime.

Need an alternative for this line of code:
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

2 Answers 2

2

Use RegisterStartupScript. RegisterStartupScript will be be executed after page is loaded completely.

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
}

In the code behind,

protected override void OnPreRender(EventArgs e)
{
  Page.ClientScript.RegisterStartupScript(GetType(), "EnableTransalation", "EnableTransalation('" + ctrl.ClientID + "')", true);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, and sorry for late reply. Further, is it possible to always allow only One Language? I dont want the user to be able to Toggle between languages.
@IshGoel - You are using a dropdown list to load the languages?
Not exactly. Language preference will be stored in Database. I want all user input always in One language as stored in datbase
2

First you have to set all text box class name as hindiFont.

Use this Code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.