00001 <?php
00028 if(!extension_loaded('mbstring')) {
00029 die("Error: This script requires the mbstring PHP extension.\n");
00030 }
00031
00032 ini_set('default_charset', 'UTF-8');
00033 mb_internal_encoding('UTF-8');
00034
00035 function post($url, $params)
00036 {
00037 $urlparts = parse_url($url);
00038 $paramstr = '';
00039 foreach($params as $k=>$v) {
00040 $paramstr .= "&$k=".urlencode($v);
00041 }
00042 $paramstr = substr($paramstr, 1);
00043
00044 $fp = fsockopen($urlparts['host'], 80);
00045 if($fp === false) die("Error: could not connect to {$urlparts['host']}\n");
00046 fwrite($fp, "POST {$urlparts['path']} HTTP/1.0\r\n");
00047 fwrite($fp, "Host: {$urlparts['host']}\r\n");
00048 fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
00049 fwrite($fp, "Content-Length: ".strlen($paramstr)."\r\n\r\n");
00050 fwrite($fp, $paramstr);
00051
00052 $result = '';
00053 while(!feof($fp)) {
00054 $result .= fread($fp, 4096);
00055 }
00056 fclose($fp);
00057
00058 return $result;
00059 }
00060
00065 function utf8_strrev($str, $reverse_numbers=false) {
00066 preg_match_all('/./us', $str, $ar);
00067 if ($reverse_numbers)
00068 return join('',array_reverse($ar[0]));
00069 else {
00070 $temp = array();
00071 foreach ($ar[0] as $value) {
00072 if (is_numeric($value) && !empty($temp[0]) && is_numeric($temp[0])) {
00073 foreach ($temp as $key => $value2) {
00074 if (is_numeric($value2))
00075 $pos = ($key + 1);
00076 else
00077 break;
00078 }
00079 $temp2 = array_splice($temp, $pos);
00080 $temp = array_merge($temp, array($value), $temp2);
00081 } else
00082 array_unshift($temp, $value);
00083 }
00084 return implode('', $temp);
00085 }
00086 }
00087
00088 if($_SERVER['argc'] < 2) {
00089 echo "desc: Translate a messages file into another language using Google's\n";
00090 echo " Translate service.\n\n";
00091 echo "usage: google_translate.php <messages_file>\n";
00092 echo "ex: google_translate.php config/i18n/fr/messages.php\n\n";
00093 exit;
00094 }
00095 $MESSAGES_FILE = $_SERVER['argv'][1];
00096
00097 if(!is_readable($MESSAGES_FILE)) {
00098 die("Error: cannot open file for read: $MESSAGES_FILE\n");
00099 }
00100
00101 include($MESSAGES_FILE);
00102 $LANGUAGE_CODE = str_replace('_','-',$LANGUAGE_CODE);
00103
00104 $params = array(
00105 'ie' => 'UTF8',
00106 'hl' => 'en',
00107 'langpair' => 'en|'.$LANGUAGE_CODE,
00108 'text' => ""
00109 );
00110 foreach($MESSAGES as $k=>$v) {
00111 $params['text'] .= "$k\n";
00112 }
00113
00114 $r = post('http://www.google.com/translate_t', $params);
00115
00116
00117 $headers = array();
00118 $hdrtext = substr($r, 0, strpos($r, "\r\n\r\n"));
00119 foreach(explode("\r\n", $hdrtext) as $hdr) {
00120 if(strpos($hdr, ':') === false) continue;
00121 $parts = explode(':', $hdr);
00122 $k = array_shift($parts);
00123 $headers[$k] = trim(join(':', $parts));
00124 }
00125
00126
00127 $parts = explode(';', $headers['Content-Type']);
00128 $parts2 = explode('=', $parts[1]);
00129 $charset = trim($parts2[1]);
00130 $r = iconv($charset, 'UTF-8//TRANSLIT', $r);
00131
00132
00133 preg_match('#<div id=result_box dir="(ltr|rtl)">(.*)</div>#Uus', $r, $matches);
00134 if(empty($matches[2])) {
00135 echo ("Error: did find translation results in output\n");
00136
00137
00138 exit(1);
00139 }
00140
00141 $outfile = file_get_contents($MESSAGES_FILE);
00142 reset($MESSAGES);
00143
00144 $words = explode("<br>", $matches[2]);
00145 foreach($words as $word) {
00146 $word = trim($word);
00147 $word = html_entity_decode($word, ENT_COMPAT, 'UTF-8');
00148
00149
00150
00151
00152 $word = str_replace(array('% s','% S','s%','S%'), '%s', $word);
00153 $word = str_replace(array('% d','% D','d%','D%'), '%d', $word);
00154
00155
00156 $word = str_replace('"', '\\"', $word);
00157
00158 $orig = key($MESSAGES);
00159 $outfile = str_replace("=> \"$orig\"", "=> \"$word\"", $outfile);
00160 next($MESSAGES);
00161 }
00162
00163 $fp = fopen($MESSAGES_FILE, 'w');
00164 fputs($fp, $outfile);
00165 fclose($fp);
00166
00167 ?>