I remembered that about two years ago, I patched Drupal in order to get automatic translation using Google Translate. At that time, Google translate did not provide any web services API, nor AJAX, and the translation of text containing HTML tags was very poor. The patch was doing simple HTTP requests, parsing of the HTTP response, some simple cleanup and in the end, it stored the translation into the Drupal locale messages.
Google Translate nowadays has much more languages than it used to backthen is AJAX based and offers web services, too. I was wondering how does it handle HTML tags and how hard would be to get it into the Moodle language editor. After 2 hours of coding and reading its API, I came up with this (sorry for JQuery, I know Moodle likes YUI):
$translang = preg_replace('/_.*/', '', $currentlang); echo ' <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("language", "1") google.load("jquery", "1.4.1") </script> <script type="text/javascript"> $(document).ready(function() { $("form .translator .stren").click(function() { var src = $(this); var dst = $("[name=\'stringXXX" + this.id + "\']") var tokens = []; var text = src.text().replace(/\$a(->\w*)?/g, function(match) { return "TK" + (tokens.push(match)-1); }); google.language.translate(text, "en", "' . $translang .'", function(result) { if (!result.error) { dst.text(result.translation.replace(/TK\d+/g, function(match) { return tokens[match.substring(2)]; })).css("background-color", "yellow") } }) }) }); </script>';This will allow a translator to click on any string in the Language editing section and get a Google translation for that string. The translation is saved in the corresponding textarea (which is marked with yellow). You can then check, edit, undo, or save these changes.
Interesting! Can you post your patch as an improvement into Moodle tracker and assign it to me. I would like to incorporate it into the next generation of Moodle translation tool.
ReplyDeleteIt posted a patch against Moodle 1.9.7 here http://tracker.moodle.org/browse/MDL-21670
ReplyDeletewhere do i put this code?
ReplyDeleteIf you're familiar with patching, you can use this patch: http://tracker.moodle.org/secure/attachment/19611/lang.php-google-translate.patch against the /admin/lang.php file of Moodle.
ReplyDeleteIf not, then edit the /admin/lang.php file and paste the code somewhere around the line 545. Look for something like this there:
if ($editable) {
$o .= "<form id=\"$currentfile\" action=\"lang.php\" method=\"post\">";
$o .= '<div>';
and paste the code above that line.
Here is a good lozalization tool that works with google translate: https://poeditor.com. I recommend it to you, it would be helpful for your strings. After registration you will benefit of a 10-day free trial and that allows you to do plenty of stuff. Really nice.
ReplyDelete