Skip to content

Instantly share code, notes, and snippets.

@kares
Created November 15, 2011 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kares/1366404 to your computer and use it in GitHub Desktop.
Save kares/1366404 to your computer and use it in GitHub Desktop.
Google Translate automatic for the people !
/**
* Google Translate automatic for the people !
*
* - visit http://translate.google.com make sure instant translation is on
* - set source and target languages and open firebug
* - copy your JSON translation object in (string key/value pairs) :
* var input = { "hello.world": "Hello World !", ... };
* - make sure non of your values to be translated end with '...' !
* - declare and output object where translations will be stored :
* var output = {};
* - copy the code bellow into firebug (should start translating)
* - when it stoppes saying "stopped after trying 10 times ..."
* you might "restart" it by copying the code in bellow again
* (and maybe before doing so clear out the source textarea)
* - when it says "and we're just about done translating ..."
* all should be done - translations are stored in `output` object
* - run JSON.stringify(output) in the console and copy JSON string
*
*/
(function(input, output) {
var console = window.console || { log: function() { /* NOOP */ } };
// source textarea :
var source = document.getElementById('source');
// translation results are printed here :
var resultBox = document.getElementById('result_box');
// extract the value to be translated
function extract(source) {
if ( ! source ) return null;
//source = source.trim();
//source = source.replace(/^\"/, '');
//source = source.replace(/\"$/, '');
return source;
}
// format the translated value
function format(translated, source) {
//return ' "' + translated + '"' + ' # ' + source;
return translated;
}
var translateAttempts = 0;
function translate(val, callback) {
var content = resultBox.textContent;
source.value = val;
translateAttempts = 0;
setTimeout(function trackValue() {
if (translateAttempts++ == 10) {
console.log('stopped after trying 10 times for for: ', val);
return;
}
var newContent = resultBox.textContent;
var tNewContent = newContent && newContent.trim();
if ( content == newContent || ! tNewContent ||
tNewContent.substring(tNewContent.length - 3) === '...' ) {
source.value = val;
setTimeout(trackValue, 300);
}
else {
callback(newContent);
}
}, 500);
}
var keys = [];
for ( var k in input ) {
if ( input.hasOwnProperty(k) ) keys.push(k);
}
var count = 0;
var prevVal = null, prevKey = null;
(function nextKey() {
var key = keys.shift();
if (key) {
if (output[key]) {
setTimeout(nextKey, 1);
return;
}
var eVal = extract( input[key] );
if (eVal) {
if (prevVal == eVal) {
output[key] = output[prevKey];
prevKey = key;
setTimeout(nextKey, 100);
return;
}
console.log(' translating: ', eVal);
translate(eVal, function(tVal) {
console.log(' translated: ', tVal); count++;
output[key] = format( tVal, eVal );
prevKey = key; prevVal = eVal;
setTimeout(nextKey, 1);
});
}
else {
output[key] = input[key]; // ""
prevKey = key; prevVal = null;
setTimeout(nextKey, 1);
}
}
else {
console.log("and we're just about done translating " + count + " values ...");
}
})(); // nextKey();
})( window.input, window.output );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment