Can JavaScript be used to program a human language translator?
I’m trying to think up ideas for projects. I was wondering if JavaScript could be used to create a human language translator? For example: If I were to type Hello and click a button to have it converted to spanish…the program would output: Hola.
Is this possible with this language? I’m not looking to do it in a different language. If it is possible, any suggestions,links, etc…that would help get me started?
Use the Google Translate API:
http://code.google.com/apis/ajaxlanguage/documentation/
Here’s a sample…
googleTranslationTest.html
~~~~~~~~~~~~~~~~~~~~~~~~~~
<html>
<head>
<meta http-equiv = "content-type" content = "text/html; charset=utf-8" />
<script type = "text/javascript" src = "http://www.google.com/jsapi"></script>
<script type = "text/javascript" src = "googleTranslationController.js"></script>
</head>
<body>
<form>
Enter text in any language, pick a language to <br >
which to translate, and click "translate."<br /><br />
<textarea id="text" cols="60" rows="5"></textarea><br />
<select id="toLang">
<option value="en" selected="true" >English</option>
<option value="fr" >Français</option>
<option value="de" >Deutsch</option>
<option value="fi" >Suomen</option>
<option value="it" >Italiano</option>
<option value="pt" >Português</option>
<option value="ru" >Русский</option>
<option value="zh-CN" >漢語</option>
</select>
<input type = "button" value = "translate" onclick = "doTranslate();" />
</form>
<div id = "gBranding"></div>
<p id = "translation"></p>
</body>
</html>
googleTranslationController.js
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// depends on pre-inclusion of http://www.google.com/jsapi
google.load("language", "1");// load language API (version 1) to page
function get(eid) {return document.getElementById( eid );} // convenience function
;
function doTranslate(){
var text = get( ‘text’ ).value;// get text to be translated
// detect source language of text – send result to translate() for handling
google.language.detect( text, translate );
}
;
function translate( r ) {
if (r.error) {// if from language detection failed, error property exists
showTranslatedText( r );// send response with error to display handler
return;// exit immediately
}
//…otherwise, attempt translation – implicit {else} of preceding {if}
var fromLang = r.language;// source language determined by API
var toLang = get( ‘toLang’ ).value;// get language to which to translate
var text = get( ‘text’ ).value;// get text to be translated
// attempt translation – send result to function showTranslatedText()
google.language.translate( text, fromLang, toLang, showTranslatedText );
}
;
function showTranslatedText( r ) {
// if error, replace translation test with error message
if (r.error) r.translation = ‘translation error: ‘ + r.error.message;
var t = get( ‘translation’ );// get reference to target display element
t.innerHTML = r.translation;// insert translated text or error message
}
;
window.onload = function() { google.language.getBranding( ‘gBranding’ ); };