My favorites | English | Sign in

Google AJAX Language API

Class Reference for Transliteration API

Table of Contents

Class google.elements.transliteration.TransliterationControl

Constructor

Instances of google.elements.transliteration.TransliterationControl are designed to enable transliteration on a set of textfields in your webpage.

Constructor Description

TransliterationControl(options)

Creates a new TransliterationControl instance. The options argument supplies the options that control the transliteration. The options argument can contain the following fields:

  • sourceLanguage - Mandatory string field that specifies the source language. Value should be of type google.elements.transliteration.LanguageCode
  • destinationLanguage - Mandatory array field that specifies the destination language. Values in the array should be of type google.elements.transliteration.LanguageCode. The first language in the array will be selected by default in the menu. Please refer to getDestinationLanguages for more details regarding valid destination languages for a given source language.
  • transliterationEnabled - Optional boolean field that specifies whether transliteration should be enabled by default in the textfields or not. True if transliteration should be enabled by default, and false otherwise. Default value for this field is false.
  • shortcutKey - Optional string field. Specifies the shortcut key to use to toggle transliteration. The shortcut key is specified as a string containing modifiers such as 'ctrl', 'alt' or 'shift' along with an alphabet key. For example, 'ctrl+g', 'ctrl+shift+a' would be valid shortcut key combinations. We do not allow 'shift' as the only modifier. Hence, 'shift+g' would not be a valid shortcut key combination.

Throws exception under the following cases: sourceLanguage or destinationLanguage is invalid, the sourceLangauge-destinationLanguage language pair is not supported for each of the languages specified in the destinationLanguage array, the shortcutKey combination is invalid, or an attempt to create a second instance of TransliterationControl in the same page is made.

Methods

Method Return Type Description

makeTransliteratable(textareaIds, opt_options)

None

Makes textfields as transliteratable and allows whatever is typed in these to be transliterated.

  • textareaIds - Array containing strings of textfield element IDs or textfield element references which should be made transliteratable.
  • opt_options - The optional opt_options argument supplies the options that are applied for this set of textfields. The opt_options arugment can contain the following fields:
    • adjustTextareaStyle - Optional boolean field. If true, the textarea size and font are adjusted optimally to suit Indic and Arabic characters. Default value for this field is true.
    • adjustTextareaDirection - Optional boolean field. If true, the textarea direction is adjusted optimally according to the direction of the destinationLanguage and the contents of the textarea. Default value for this field is true.

Throws exception under the following case: Invalid textareaIds

showControl(divElement)

None

Shows the control in the divElement.

  • divElement - String Id of the div on which the transliteration control will be drawn.

Advanced Methods

Method Return Type Description

enableTransliteration()

None

Turns on transliteration.

disableTransliteration()

None

Turns off transliteration.

toggleTransliteration()

None

Toggles transliteration on or off status.

setLanguagePair(sourceLanguage, destinationLanguage)

Boolean

Changes the language pair for transliteration.

  • sourceLanguage - Value should be of type google.elements.transliteration.LanguageCode
  • destinationLanguage - Value should be of type google.elements.transliteration.LanguageCode

Returns a boolean indicating whether the setLanguage action was successful.

Throws exception under the following cases: sourceLanguage or destinationLanguage is invalid, the souceLangauge-destinationLanguage language pair is not supported

getLanguagePair()

Object with fields sourceLanguage and destinationLanguage

Gets the current language pair for transliteration. Returns an object that contains the fields sourceLanguage & destinationLanguage.

isTransliterationEnabled()

Boolean

Returns a boolean indicating whether transliteration is enabled or not.

addEventListener(eventType, listener, opt_listenerScope)

None

Adds a listener for the given event type. When the particular event type is triggered, the listener is called with the event object. The contents of the event object depend on the type of the event and the EventTypes are described in more detail here.

  • eventType - Value should be one of google.elements.transliteration.TransliterationControl.EventType.
  • listener - Listener function for the event.
  • opt_listenerScope - The listener will be called with "this" set to the opt_listenerScope object.

removeEventListener(eventType, listener, opt_listenerScope)

None

Removes a listener.

  • eventType - Value should be one of google.elements.transliteration.TransliterationControl.EventType.
  • listener - Listener function for the event that needs to be removed.
  • opt_listenerScope - The scope (opt_listenerScope) in which the listener was registered at the time of adding the listener.

Global Methods

Method Return Type Description
google.elements.transliteration.isBrowserCompatible
Boolean
Returns a boolean indicating whether transliteration is supported in the client's browser.
google.elements.transliteration.getDestinationLanguages(sourceLanguage)
Map of supported destination languages with key as language name and value as language code
For the given sourceLanguage, this returns a map of destination languages for which transliteration is supported. The returned map is similar to the one described in the LanguageCode section.
  • sourceLanguage - Value should be of type google.elements.transliteration.LanguageCode

Low Level Transliteration API Methods

google.language.transliterate is a low level API which can be invoked after loading the AJAX Language API as explained in the Developer Guide

Method Return Type Description
google.language.transliterate(wordsArray, srcLang, destLang, callback)
None
A global method that will obtain transliterations for the given words in the destination language. The result is supplied asynchronously to the given callback function as the result object.
  • wordsArray - The words that are to be transliterated, specified in an array format.
  • srcLang - The source language as a language code. See the Language Enum for examples.
  • destLang - The destination language as a language code. See the Language Enum for examples.
  • callback - The callback function that receives the result.

Result Objects

Transliteration Result

This result object is obtained when the google.language.transliterate() call is invoked.

  • <result>
    • error?
      Present if there was an error in transliteration.
    • transliterations
      An array of size equal to the size of the input wordsArray.
      • transliteratedWords
        An array of maximum size 5, with transliterations for the corresponding word in wordsArray.

Enums

EventType Enum

The google.elements.transliteration.TransliterationControl.EventType enumeration provides possible events which can be raised during transliteration. Developers can provide custom handlers for these events in their code.

var google.elements.transliteration.TransliterationControl.EventType = {
   STATE_CHANGED: 'state_changed',
   LANGUAGE_CHANGED: 'language_changed',
   SERVER_REACHABLE:'server_reachable',
   SERVER_UNREACHABLE:'server_unreachable'
};
  • google.elements.transliteration.TransliterationControl.EventType.STATE_CHANGED: Event fired when transliteration is enabled or disabled in the TransliterationControl in any of the following ways:
    • When the transliteration is enabled or disabled using the shortcut key.
    • When the transliteration is enabled or disabled using enableTransliteration, disableTransliteration & toggleTransliteration.
    • When the transliteration is enabled or disabled by clicking the transliteration control drawn by showControl.
    The event object passed to the listener contains the field transliterationEnabled which is true if the transliteration is 'on', else false.
  • google.elements.transliteration.TransliterationControl.EventType.LANGUAGE_CHANGED: Event fired when the transliteration language pair is changed in the TransliterationControl in any of the following ways:
    • When the transliteration language pair is changed using setLanguagePair.
    • When the transliteration language is changed from the transliteration control drawn by showControl.
    The event object passed to the listener contains the fields sourceLanguage & destinationLanguage.
  • google.elements.transliteration.TransliterationControl.EventType.SERVER_REACHABLE: Event fired when the server could be successfully contacted for doing a transliteration.
  • google.elements.transliteration.TransliterationControl.EventType.SERVER_UNREACHABLE: Event fired when the server could not be successfully contacted for doing a transliteration.

LanguageCode Enum

The google.elements.transliteration.LanguageCode enumeration provides a mapping from name constants to language codes that can be used to specify the source and destination languages in the transliteration methods.

var google.elements.transliteration.LanguageCode = {
    ENGLISH: 'en',
    ARABIC: 'ar',
    BENGALI: 'bn',
    GUJARATI: 'gu',
    HINDI: 'hi',
    KANNADA: 'kn',
    MALAYALAM: 'ml',
    MARATHI: 'mr',
    NEPALI: 'ne',
    PERSIAN: 'fa',
    PUNJABI: 'pa',
    TAMIL: 'ta',
    TELUGU: 'te',
    URDU: 'ur'
};

SupportedDestinationLanguages EnumNew!

The google.elements.transliteration.SupportedDestinationLanguages enumeration provides a mapping from name constants to arrays of language codes that can be used to specify groups of destination languages in the TransliterationControl.

var google.elements.transliteration.SupportedDestinationLanguages = {
    // ALL includes all languages supported in the transliteration API.
    // As support for more languages becomes available, this enum will be
    // automatically updated to include the new languages transparently.
    ALL: [
        google.elements.transliteration.LanguageCode.ARABIC,
        google.elements.transliteration.LanguageCode.BENGALI,
        google.elements.transliteration.LanguageCode.GUJARATI,
        google.elements.transliteration.LanguageCode.HINDI,
        google.elements.transliteration.LanguageCode.KANNADA,
        google.elements.transliteration.LanguageCode.MALAYALAM,
        google.elements.transliteration.LanguageCode.MARATHI,
        google.elements.transliteration.LanguageCode.NEPALI,
        google.elements.transliteration.LanguageCode.PERSIAN,
        google.elements.transliteration.LanguageCode.PUNJABI,
        google.elements.transliteration.LanguageCode.TAMIL,
        google.elements.transliteration.LanguageCode.TELUGU,
        google.elements.transliteration.LanguageCode.URDU],

    // INDIC includes all Indic languages supported in the transliteration API.
    // As support for more Indic languages becomes available, this enum will be
    // automatically updated to include the new languages transparently.
    INDIC: [
        google.elements.transliteration.LanguageCode.BENGALI,
        google.elements.transliteration.LanguageCode.GUJARATI,
        google.elements.transliteration.LanguageCode.HINDI,
        google.elements.transliteration.LanguageCode.KANNADA,
        google.elements.transliteration.LanguageCode.MALAYALAM,
        google.elements.transliteration.LanguageCode.MARATHI,
        google.elements.transliteration.LanguageCode.NEPALI,
        google.elements.transliteration.LanguageCode.PUNJABI,
        google.elements.transliteration.LanguageCode.TAMIL,
        google.elements.transliteration.LanguageCode.TELUGU,
        google.elements.transliteration.LanguageCode.URDU]
};