Skip to content

Instantly share code, notes, and snippets.

@andykant
Created June 17, 2010 16:33
Show Gist options
  • Save andykant/442361 to your computer and use it in GitHub Desktop.
Save andykant/442361 to your computer and use it in GitHub Desktop.
A mass Google Translate bookmarklet
/*
1. Copy the compressed version into a bookmark address
2. Go to Google Translate: http://translate.google.com/
3. Paste your message block in the textarea (default delimiters are new lines for each message and '=' to separate the key/value
4. Click the bookmarklet
5. The result is injected back into the textarea, focused, and selected
6. Copy + paste back into your message file
*/
// compressed
javascript:(function(){var $=function(el){return document.getElementById(el)},chomp=function(str){return str.replace(/^[\s\n\r]*/,'').replace(/[\s\n\r]*$/,'')},source=$('source'),messages=chomp(source.value).split(/[\r\n]/),delimiter='=',data=[],text=[],message,i;for(i=0;(message=messages[i]);i++){message=messages[i]=chomp(message).split(delimiter);message[1]=messages[i][1]=message.slice(1).join(delimiter);data.push(message[1])}source.value=data.join('\n');window.location.hash='#'+$('old_sl').value+'|'+$('old_tl').value+'|'+encodeURIComponent(data.join('\n'));setTimeout(function parse(){var translations=$('result_box').childNodes,results=[];if(translations.length>messages.length){alert('Found too many translated messages, please try a smaller subset')}else if(translations.length!==messages.length){setTimeout(parse,100);return}for(i=0;(message=messages[i]);i++){results.push(message[0]+'='+(translations[i]?translations[i].firstChild.textContent:''))}source.value=results.join('\n');source.focus();source.setSelectionRange(0,source.value.length)},100)})()
// expanded
javascript:(function() {
var $ = function(el) { return document.getElementById(el); },
chomp = function(str) { return str.replace(/^[\s\n\r]*/,'').replace(/[\s\n\r]*$/,''); },
source = $('source'),
messages = chomp(source.value).split(/[\r\n]/),
delimiter = '=',
data = [],
text = [],
message, i;
for (i = 0; (message = messages[i]); i++) {
message = messages[i] = chomp(message).split(delimiter);
message[1] = messages[i][1] = message.slice(1).join(delimiter);
data.push(message[1]);
}
source.value = data.join('\n');
window.location.hash = '#' + $('old_sl').value + '|' + $('old_tl').value + '|' + encodeURIComponent(data.join('\n'));
setTimeout(function parse() {
var translations = $('result_box').childNodes,
results = [];
if (translations.length > messages.length) {
alert('Found too many translated messages, please try a smaller subset');
}
else if (translations.length !== messages.length) {
setTimeout(parse, 100);
return;
}
for (i = 0; (message = messages[i]); i++) {
results.push(message[0] + '=' + (translations[i] ? translations[i].firstChild.textContent : ''));
}
source.value = results.join('\n');
source.focus();
source.setSelectionRange(0, source.value.length);
}, 100);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment