root/releases/0.9.1/utils/i18n.php

Revision 1547, 6.6 kB (checked in by dramirez, 10 months ago)

Removed gettext calls from setup.php. It would fix the #252 ticket

Line 
1 <?php
2 /*
3  * i18n.php
4  *
5  *  Scripts that scan an generates the po file for the specified directory
6  *
7  * Created on May 11, 2007
8  *
9  * @author Diego Andrᅵs Ramᅵrez Aragᅵn <diego@somosmas.org>
10  * @copyright Corporaciᅵn Somos mᅵs - 2007
11  */
12
13 class i18n_Manager {
14   var $_directory;
15   var $_translations= array ();
16   var $_default_locale;
17   var $_header;
18
19   function i18n_Manager($directory, $locale= "en_GB",$header="elgg_header.txt") {
20     if (is_dir($directory)) {
21       $this->_directory= $directory;
22     }
23     $this->_default_locale= $locale;
24     $this->_header = $header;
25   }
26
27   function initCurrentTranslations($file) {
28     $file= file($file);
29     $i= 0;
30     foreach ($file as $line) {
31       if (strpos($line, 'msgid') === 0) {
32         $key = trim(substr($line,strlen('msgid')));
33         if (strpos($file[$i -1], "#: ") >= 0) {
34           $this->_translations[$key]['file']= trim($file[$i -1]);
35         }
36         $this->_translations[$key]['msgid']= $key;
37         $translation = trim(substr($file[$i +1],strlen('msgstr')));
38         $this->_translations[$key]['msgstr']= $translation;
39       }
40       $i++;
41     }
42   }
43
44   function isTranslated($string) {
45     return (array_key_exists($string, $this->_translations));
46   }
47
48   function addTranslation($key, $file= "") {
49     if (!array_key_exists($key, $this->_translations)) {
50       $this->_translations[$key]['msgid']= $key;
51       $this->_translations[$key]['msgstr']= "";
52       if ($file != "") {
53         $file= (!strpos($file, "#: ")) ? "#: " . $file : $file;
54         $this->_translations[$key]['file']= $file;
55       }
56     }
57   }
58
59   function process() {
60     $po_dir= $this->_directory . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR . $this->_default_locale . DIRECTORY_SEPARATOR . "LC_MESSAGES";
61     if (is_dir($po_dir)) {
62       $po_files= scandir($po_dir);
63       foreach ($po_files as $po_file) {
64         if (strpos($po_file, ".po") > 0) {
65           echo "\tLoading current translations from $po_file\n";
66           $this->initCurrentTranslations($po_dir . DIRECTORY_SEPARATOR . $po_file);
67         }
68       }
69     } else {
70       echo "\tBuilding language directory layout\n";
71       @mkdir($this->_directory . DIRECTORY_SEPARATOR . "languages");
72       @mkdir($this->_directory . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR . $this->_default_locale);
73       @mkdir($this->_directory . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR . $this->_default_locale . DIRECTORY_SEPARATOR . "LC_MESSAGES");
74     }
75     $current_strings= $this->scan();
76     foreach ($current_strings as $string) {
77       if (!$this->isTranslated($string['string'])) {
78         echo "\tAdding new translation string: " . $string['string'] ."\n";
79         $this->addTranslation($string['string'], $string['file']);
80       }
81     }
82     $this->export($this->_header,$po_dir.DIRECTORY_SEPARATOR.$this->_default_locale.".po");
83   }
84
85   function scan() {
86     global $grep_path;
87     $result= array ();
88
89     $strings= shell_exec($grep_path.'grep -nrPo \'__gettext\("(.*?[^\\\])"\)\' ' . $this->_directory . DIRECTORY_SEPARATOR);
90     $strings.= shell_exec($grep_path.'grep -nrEo "__gettext\(\'(.*?[^\\\])\'\)" ' . $this->_directory . DIRECTORY_SEPARATOR);
91     $strings= explode("\n", $strings);
92     foreach ($strings as $string) {
93       if (!strpos($string, "svn")) {
94         $string= str_replace($this->_directory . DIRECTORY_SEPARATOR, "", $string);
95         // echo $string . "\n\n";
96         preg_match("/([\w\.\/]+:[\d]+:)(.+)/", $string, $matches);
97         // print_r($matches);
98         if (count($matches) == 3) {
99           // echo "line: ".$matches[1]."\n";
100           $_file = $matches[1];
101           //echo "gettext ".$matches[2]."\n";
102           // preg_match("/__gettext\(([\w\s\"\'\d\D]+)\)((.+))/i", $matches[2], $matches);
103           preg_match("/__gettext\((.+)\)/i", $matches[2], $matches);
104           //print_r($matches);
105           // Drop locale domain
106           $_matches = preg_split('/,\s*"|,\s*\'/',$matches[1]);
107           //print_r($_matches);
108           $_line = $_matches[0];
109           $result[]= array('file'=>$_file,'string'=>$_line);
110           //echo "------------------\n";
111         }
112       }
113     }
114     return $result;
115   }
116
117   function export($header, $outputfile, $overwrite= false) {
118     if (file_exists($outputfile) && !$overwrite) {
119       $outputfile= $outputfile . ".new";
120     }
121     echo "Creating language file $outputfile\n";
122     $mr= fopen($outputfile, "w");
123     $header = file_get_contents($header);
124
125     if(function_exists('mb_convert_encoding')){
126       echo "Setting UTF-8\n";
127       mb_internal_encoding('UTF-8');
128       $header = mb_convert_encoding($header,'UTF-8');
129     }
130
131     fwrite($mr, $header);
132     fwrite($mr, "\n");
133     foreach ($this->_translations as $translation) {
134       if($translation['msgid']=="\"\"") continue;
135       if (array_key_exists('file', $translation)) {
136         fwrite($mr, $translation['file'] . "\n");
137       }
138       $msgid = $translation['msgid'];
139       $msgstr = $translation['msgstr'];
140
141       if(function_exists('mb_convert_encoding')){
142         $msgid = mb_convert_encoding($msgid,"UTF-8");
143         $msgstr = mb_convert_encoding($msgstr,"UTF-8");
144       }
145
146       fwrite($mr,"msgid ". $msgid . "\n");
147       $msg = (empty($msgstr))?"\"\"":$msgstr;
148       fwrite($mr,"msgstr ". $msg . "\n\n");
149     }
150     fclose($mr);
151   }
152 }
153
154 function print_use(){
155   echo "Use i18n.php <directory> <locale> <header_file>\n\n";
156   echo "  If you use this comments without parameteres it assumes:\n";
157   echo "  \t <directory> = . \n";
158   echo "  \t <locale> en_GB \n";
159   echo "  \t <header_file> elgg_header.txt (taked from the utils directory)\n\n";
160   exit;
161 }
162
163 /*
164  * Command excution
165  */
166 require_once dirname(__FILE__) . "/../config.php";
167
168 $grep_path = "/bin/";
169 // For ubuntu systems your need to install a perl-regexp capable grep version
170 //   sudo apt-get install libpcre3-dev
171 //   wget ftp://mirrors.kernel.org/gnu/grep/grep-2.5.1a.tar.gz
172 //   tar xvzf  grep-2.5.1a.tar.gz
173 //   cd grep-2.5.1a
174 //   ./configure --prefix=/
175 //   make
176 //   sudo make install
177
178 if($argc > 4 ){
179   print_use();
180 }
181
182 $directory = ($argc>1)?$argv[1]:".";
183 $locale = ($argc>2)?$argv[2]:"en_GB";
184 $header = ($argc>3)?$argv[3]:"elgg_header.txt";
185
186 if(!is_dir($directory)){
187   echo "\nERROR: $directory is not a directory!\n\n";
188   print_use();
189 }
190
191 if(!file_exists($header)){
192   echo "\nERROR: $header its not a file. falling back to the default header\n\n";
193   $header = "elgg_header.txt";
194 }
195
196 echo "Running with the following parameters:\n";
197 echo "  Directory:\t$directory\n";
198 echo "  Locale:\t$locale\n";
199 echo "  Header:\t$header\n\n";
200
201 $header = ("elgg_header.txt"==$header)?dirname(__FILE__)."/$header":$header;
202
203 $i18n= new i18n_Manager($directory, $locale,$header);
204 $strings = $i18n->scan();
205 $i18n->process();
206 echo "\n";
207 ?>
208
Note: See TracBrowser for help on using the browser.