Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatic Google transliterating form elements
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google AJAX Language API - Basic Transliteration</title>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("language", "1");
google.load("elements", "1", { packages: "transliteration" });
function intialize_transliteration(){
var options = {
sourceLanguage: 'en',
destinationLanguage: ['kn'],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
var control =
new google.elements.transliteration.TransliterationControl(options);
//add as many as required
var ids = [ "fnk", "lnk" ];
control.makeTransliteratable(ids);
control.showControl('translControl');
}
google.setOnLoadCallback(intialize_transliteration);
function auto_transliterate(from, to) {
if(from.value){
google.language.transliterate(from.value.split(" "), "en", "kn", function(result) {
if (!result.error) {
if (result.transliterations && result.transliterations.length > 0 ){
if(result.transliterations[0].transliteratedWords.length > 0){
to.value = result.transliterations[0].transliteratedWords[0];
}
if(result.transliterations[1].transliteratedWords.length > 0) {
to.value = to.value +" "+result.transliterations[1].transliteratedWords[0] ;
}
if(result.transliterations[2].transliteratedWords.length > 0) {
to.value = to.value +" "+result.transliterations[2].transliteratedWords[0] ;
}
if(result.transliterations[3].transliteratedWords.length > 0) {
to.value = to.value +" "+result.transliterations[3].transliteratedWords[0] ;
}
if(result.transliterations[4].transliteratedWords.length > 0) {
to.value = to.value +" "+result.transliterations[4].transliteratedWords[0] ;
}
}
}
});
}
}
function auto_transliterate_all(){
auto_transliterate(document.getElementById("fn"), document.getElementById("fnk"));
auto_transliterate(document.getElementById("ln"), document.getElementById("lnk"));
//Add more if required!
}
</script>
</head>
<body>
<br> First Name <input value="Thejesh Gangaiah" id="fn" />
<br> First Name (in Kannada) <input value="" id="fnk"/>
<br> Last Name <input value="Nagarathna" id="ln" />
<br> Last Name (in Kannada) <input value="" id="lnk"/>
<div id='translControl'></div>
<input type="button" onclick="auto_transliterate_all()" value="Auto Transliterate"/>
</body>
</html>
@thejeshgn

This comment has been minimized.

Copy link
Owner Author

@thejeshgn thejeshgn commented Feb 14, 2011

Now it can actually transliterate space separated strings too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.