Showing posts with label google. Show all posts
Showing posts with label google. Show all posts

Friday, April 29, 2011

Tuesday, December 09, 2008

Google Translate for Trac timeline comments

JavaScript that translates changeset comments in Trac timeline using Google Language API. How to use it with Trac should be well-described at this Trac-Hacks page. Here I just want to state that this code can be reused for other .html pages.

This code is used at http://farmanager.rainforce.org/timeline

Just do not forget to add jQuery and the following line to your header.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>

Here goes the main code:

// Translate changeset comments in Trac timeline
// Using Google Language API version 1

// Code in public domain
// techtonik // rainforce.org


// wrap the code to use $ as jQuery function name
(function($){


// setup language parameters

var srclang = "ru";
var tgtlang = "en";

var gtrans_btn_text = "Translate russian comments";

var srclang_a_z = Array();
srclang_a_z["en"] = /[a-z]+/i;
srclang_a_z["ru"] = /[Р°-С_]+/i;


// load Google API

google.load("language", "1");


// make this anonymous function executed by jQuery when document is loaded
jQuery(document).ready(function() {

// add "powered by Google" logo into footer
$('<p id="googleattr" class="left"></p>').insertAfter("#footer p.left:last")
google.language.getBranding('googleattr');

// add trac_timeline_translate() button to timeline menu
var gtrans_btn = $("<input type='button'/>")
.attr({
id: "gtrans_btn",
value:gtrans_btn_text
})
.click(function() {
trac_timeline_translate();
gtrans_btn.attr("disabled", "disables");
return false;
});

$("#prefs").append("<hr/>");
$("#prefs")
.append(
$("<div align='center'></div>").append(gtrans_btn)
)
})


function trac_timeline_translate() {
// translate changeset messages
g_trans_nodes( $("dd.changeset") );
}


function g_trans_nodes(nodes) {
// translate nodes one by one
// todo: group many small nodes into one packet < 5000 bytes

var transTexts = Array();
var transNodes = Array();

for (i=0; i<nodes.size();i++) {
var text = nodes[i].innerHTML;

if (text.match(srclang_a_z[srclang])) {
transTexts.push(text);
transNodes.push(nodes[i]);
}
}

for (i=0; i<transTexts.length;i++) {
function makeTransNodeClosure(target_node) {
// closure to save node parameter to be accessible
// in callback function
var node = target_node; // local variable
var trans_callback = function (result) {
node.innerHTML = result.translation;
}
return trans_callback; // return function
}

google.language.translate(transTexts[i], srclang, tgtlang, makeTransNodeClosure(transNodes[i]));
}
}


})(jQuery);

Thursday, September 20, 2007

Offline J2ME Google Reader / Gears

Since I do not have much time to implement all crazy ideas by myself, at least I can dedicate some time to describe them for future elaboration in case somebody decides to do something similar and stumble upon a search engine to find out what is already done.

This time it is an idea about bringing Google Reader to J2ME mobile platform, so that feeds can be read and marked offline mode (marked as read or for check in online mode later), preferably downloaded through data cable to keep synchronized with Gears read/marked feeds on PC.

Why mobile and offline? Most of the time we spend time plugged in the Internet, but things are different when you on the move. Theoretically you may use mobile phone to access the Net, but practically it is not always true.

First of all, mobile Internet is quite expensive in many countries. I can say that in my country (located in Europe) 100Mb of mobile traffic may cost from 5% (with monthly fee on dedicated plan) to 105% (use on demand) of average salary for the same operator. 5% is too expensive for just 100Mb.

Second reason is that there are hours underground and airborne where mobile phones are either do not have internet connection or switched to offline plane mode. There are also cases when you run out of money and have nothing to do until nearest ATM. Time spend by mobile in offline is much greater than of working station. If there is a Google Gears for PC then there should be a Gears for mobile, at least a part of mobile Gears.

Well, "maybe" not a "should", but "would" be nice.
Just to keep track of the issue, here a link for original proposal.
http://groups.google.com/group/google-reader-feedback/browse_thread/thread/d9bf21e1b341d38c