source: trunk/contrib/lc_parse_strings.php

Last change on this file was 1402, checked in by gogo, 21 months ago

Rebuild the translation system and language files.

Includes a few kludges to help lc_parse_strings.php detect translation strings.

See README_TRANSLATORS.TXT for description of the translation system.

  • Property svn:executable set to *
  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
File size: 9.1 KB
Line 
1<?php
2//die("this script is disabled for security");
3
4/**
5  * LC-Parse-Strings-Script
6  *
7  * This script parses all xinhas source-files and creates base language files...
8  *
9  *   /lang/lc_base.js
10  *   /modules/.../lang/lc_base.js
11  *   /plugins/.../lang/lc_base.js
12  *   /unsupported_plugins/.../lang/lc_base.js
13  *
14  * This script should be used from the command line, not from a web browser
15  *  (well it should work, but anyway).
16  *
17  * How To use it: - remove the die() in line 2 (security)
18  *                - cd contrib && php ./lc_parse_strings.php
19  *                - lang/lc_base.js will be written
20  *                - open lc_base.js, translate all strings into your language and save it
21  *                  as yourlangauge.js
22  *                - send the translated file to the xinha-team
23 **/
24
25
26error_reporting(E_ALL);
27
28$XinhaRoot = realpath(dirname(__FILE__).'/..');
29
30$ret = array();
31$files = getFiles($XinhaRoot, "/js$/");
32foreach($files as $file) {
33    $fp = fopen($file, "r");
34    $data = "";
35    while(!feof($fp)) {
36        $data .= fread($fp, 1024);
37    }
38
39    preg_match_all("#_lc\(\s*\"((?:[^\"]|\\\\\")+)\"\s*[,)]#", $data, $m);           
40    foreach($m[1] as $i) {
41        if(trim(strip_tags($i))=="") continue;
42        $ret[] = preg_replace('/\\\\"/', '"', $i);
43    }
44   
45    preg_match_all('#_lc\(\s*\'((?:[^\']|\\\\\')+)\'\s*[,)]#', $data, $m);
46    foreach($m[1] as $i) {
47        if(trim(strip_tags($i))=="") continue;
48        $ret[] = preg_replace("/\\\\'/", "'", $i);
49    }
50
51    if(preg_match('/(XinhaCore|XinhaLoader|htmlarea)\.js$/', $file)) {
52        //toolbar-buttons
53        //bold:          [ "Bold"
54        preg_match_all('#[a-z]+: *\[ * "([^"]+)"#', $data, $m);
55        foreach($m[1] as $i) {
56            if(trim($i)=="") continue;
57            $ret[] = $i;
58        }
59
60        //HTMLArea._lc({key: 'button_bold', string
61        preg_match_all('#(Xinha|HTMLArea)\\._lc\\({key: \'(([^\']|\\\\\')+)\'\s*[,)]#', $data, $m);
62        foreach($m[1] as $i) {
63            if(trim($i)=="") continue;
64            $ret[] = $i;
65        }
66
67        //config.fontname, fontsize and formatblock
68        $data = substr($data, strpos($data, "this.fontname = {"), strpos($data, "this.customSelects = {};")-strpos($data, "this.fontname = {"));
69        preg_match_all('#"([^"]+)"[ \t]*:[ \t]*["\'][^"\']*["\'],?#', $data, $m);
70        foreach($m[1] as $i) {
71            if(trim($i)=="") continue;
72            $ret[] = $i;
73        }
74    }
75}
76
77$files = getFiles("{$XinhaRoot}/popups", "/html$/");
78foreach($files as $file)
79{
80    if(preg_match("#custom2.html$#", $file)) continue;
81    if(preg_match('#old_#', $file)) continue;
82    $ret = array_merge($ret, parseHtmlFile($file));
83}
84$ret = array_unique($ret);
85$langData['HTMLArea'] = $ret;
86
87
88foreach(array('plugins', 'modules', 'unsupported_plugins') as $pDir)
89{
90$plugins = getFiles("{$XinhaRoot}/{$pDir}");
91foreach($plugins as $pluginDir) {
92    $plugin = basename($pluginDir);//preg_replace('/\.\.\/[^/]+\//', '', $pluginDir);
93   
94    $ret = array();
95
96    $files = getFiles("$pluginDir", "/js$/");
97    $files = array_merge($files, getFiles("$pluginDir/popups", "/html$/"));
98    $files = array_merge($files, getFiles("$pluginDir", "/php$/"));
99    foreach($files as $file)
100    {
101        $fp = fopen($file, "r");
102        $data = "";
103        if($fp) {
104            echo "$file open\n";
105            while(!feof($fp)) {
106              $data .= fread($fp, 1024);
107            }
108           
109            preg_match_all("#_lc\(\s*\"((?:[^\"]|\\\\\")+)\"\s*[,)]#", $data, $m);           
110            foreach($m[1] as $i) {
111                if(trim(strip_tags($i))=="") continue;
112                $ret[] = preg_replace('/\\\\"/', '"', $i);
113            }
114           
115            preg_match_all('#_lc\(\s*\'((?:[^\']|\\\\\')+)\'\s*[,)]#', $data, $m);
116            foreach($m[1] as $i) {
117                if(trim(strip_tags($i))=="") continue;
118                $ret[] = preg_replace("/\\\\'/", "'", $i);
119            }
120           
121           
122        }
123    }
124
125    $files = array_merge($files, getFiles("$pluginDir", "/\.(html|php)$/"));
126    $files = array_merge($files, getFiles("$pluginDir/popups", "/\.(html|php)$/"));
127    $files = array_merge($files, getFiles("$pluginDir/dialogs", "/\.(html|php)$/"));
128    foreach($files as $file)
129    {
130        $ret = array_merge($ret, parseHtmlFile($file, $plugin));
131    }
132   
133    $langData[$plugin] = array_unique($ret);   
134}
135}
136
137
138
139foreach($langData as $plugin=>$strings) {
140   
141
142    $data = "// I18N constants\n";
143    $data .= "//\n";
144    $data .= "// LANG: \"en\", ENCODING: UTF-8\n";
145    $data .= "// Author: Translator-Name, <email@example.com>\n";
146    $data .= "//\n";   
147    $data .= "// Last revision: ".date('Y-m-d')."\n";
148    $data .= "// Please don´t remove this information\n";
149    $data .= "// If you modify any source, please insert a comment with your name and e-mail\n";
150    $data .= "//\n";
151    $data .= "// Distributed under the same terms as HTMLArea itself.\n";
152    $data .= "// This notice MUST stay intact for use (see license.txt).\n";
153    $data .= "//\n";
154    $data .= "// (Please, remove information below)\n";   
155    $data .= "// FOR TRANSLATORS:\n";
156    $data .= "//\n";
157    $data .= "//   1. PLEASE PUT YOUR CONTACT INFO IN THE ABOVE LINE\n";
158    $data .= "//      (at least a valid email address)\n";
159    $data .= "//\n";
160    $data .= "//   2. PLEASE TRY TO USE UTF-8 FOR ENCODING;\n";
161    $data .= "//      (if this is not possible, please include a comment\n";
162    $data .= "//       that states what encoding is necessary.)\n";
163    $data .= "\n";
164    /*
165    $data .= "{\n";
166   
167    sort($strings);
168    foreach($strings as $string) {
169        $string = str_replace(array('\\', '"'), array('\\\\', '\\"'), $string);
170        $data .= "  \"".$string."\": \"\",\n";
171    }
172    $data = substr($data, 0, -2);
173    $data .= "\n";
174    $data .= "}\n";
175    */
176    sort($strings);
177    $js_data = array();
178    foreach($strings as $string)
179    {
180      $js_data[$string] = '';
181    }   
182    $data .= json_encode($js_data,  JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
183   
184    $langfile = false;
185    if(preg_match('/HTMLArea|Xinha/', $plugin))
186    {
187      $langfile = dirname(__FILE__).'/../lang/lc_base.js';
188    }
189    elseif (is_dir(dirname(__FILE__).'/../modules/'.$plugin))
190    {
191      $langfile = dirname(__FILE__).'/../modules/'.$plugin.'/lang/lc_base.js';
192    }
193    elseif (is_dir(dirname(__FILE__).'/../plugins/'.$plugin))
194    {
195      $langfile = dirname(__FILE__).'/../plugins/'.$plugin.'/lang/lc_base.js';
196    }
197    elseif (is_dir(dirname(__FILE__).'/../unsupported_plugins/'.$plugin))
198    {
199      $langfile = dirname(__FILE__).'/../unsupported_plugins/'.$plugin.'/lang/lc_base.js';
200    }
201    else
202    {
203      echo "Unknown {$plugin}\n";
204      continue;
205      throw new Exception("What sort of plugin is {$plugin}?");
206    }
207   
208    // If we don't have any data, remove any existing lc_base.js
209    if(!count($js_data))
210    {
211      if(file_exists($langfile)) unlink($langfile);
212      continue;
213    }
214   
215     if(!file_exists(dirname($langfile)))
216     {
217       mkdir(dirname($langfile));
218     }
219    file_put_contents($langfile, $data);
220    echo "$langfile written\n";
221}
222
223
224
225
226function parseHtmlFile($file, $plugin="")
227{
228echo "Parsing $file\n";
229    $ret = array();
230   
231    $data = file_get_contents($file);
232   
233    if(preg_match('/<l10n>/i', $data) || preg_match('/"_\([^"]*\)"/', $data))
234    {
235      // Newer plugin dialogs use our <l10n>english</l10n> tag to indicate a translation text.
236      // also a attribute="_([english])"
237      $elems = array('l10n');
238    }
239    else
240    {
241      $elems = array("title", "input", "select", "legend", "span", "option", "td", "button", "div", "label");
242    }
243
244    foreach($elems as $elem) {
245        preg_match_all("#<{$elem}[^>]*>([^<^\"]+)</$elem>#i", $data, $m);
246        foreach($m[1] as $i) {
247            if(trim(strip_tags($i))=="") continue;
248            if($i=="/") continue;
249            if($plugin=="ImageManager" && preg_match('#^--+$#', $i)) continue; //skip those ------
250            if($plugin=="CharacterMap" && preg_match('#&[a-z0-9]+;#i', trim($i)) || $i=="@") continue;
251            if($plugin=="SpellChecker" && preg_match('#^\'\\.\\$[a-z]+\\.\'$#', $i)) continue;
252            $ret[] = trim($i);
253        }
254    }
255   
256    // "_( ... )" is only valide for an attribute value
257    preg_match_all('#"_\(([^"]+)\)"#i', $data, $m);
258    foreach($m[1] as $i) {
259        if(trim($i)=="") continue;
260        $ret[] = $i;
261    }
262     
263    // Older plugins translate the title attribute
264    if(count($elems) > 1)
265    {
266        preg_match_all('#title="([^"]+)"#i', $data, $m);
267        foreach($m[1] as $i) {
268            if(trim(strip_tags($i))=="") continue;
269            if(strip_tags($i)==" - ") continue; //skip those - (ImageManager)
270            $ret[] = $i;
271        }
272    }
273   
274    return($ret);
275}
276
277
278function getFiles($rootdirpath, $preg_match='') {
279 $array = array();
280 if ($dir = @opendir($rootdirpath)) {
281   $array = array();
282   while (($file = readdir($dir)) !== false) {
283     if($file=="." || $file==".." || $file==".svn" || $file == ".htaccess") continue;
284      if($preg_match=="")
285        $array[] = $rootdirpath."/".$file;
286      else if(preg_match($preg_match,$file))
287        $array[] = $rootdirpath."/".$file;
288     
289   }
290   closedir($dir);
291 }
292 return $array;
293}
294
295
296?>
Note: See TracBrowser for help on using the repository browser.