source: trunk/contrib/lc_parse_strings.php @ 288

Last change on this file since 288 was 288, checked in by niko, 14 years ago

php-script that parses all files for lc-strings
#203
see comment on top of lc_parse_strings.php on how to use it.

  • Property svn:executable set to *
File size: 7.1 KB
Line 
1<?php
2die("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 lang-files
8  * in the lang-folders (one for base and one every plugin)
9  *
10  * How To use it: - remove the die() in line 2 (security)
11  *                - make sure all lang-folders are writeable for your webserver
12  *                - open the contrib/lc_parse_strings.php in your browser
13  *                - lang/base.js will be written
14  *                - open base.js, translate all strings into your language and save it
15  *                  as yourlangauge.js
16  *                - send the translated file to the xinha-team
17 **/
18
19
20
21error_reporting(E_ALL);
22
23$ret = array();
24$files = getFiles("../", "js$");
25foreach($files as $file)
26{
27    $fp = fopen($file, "r");
28    $data = "";
29    while(!feof($fp)) {
30        $data .= fread($fp, 1024);
31    }
32
33    preg_match_all('#_lc\("([^"]+)"\)|_lc\(\'([^\']+)\'\)#', $data, $m);
34    foreach($m[1] as $i) {
35        if(trim($i)=="") continue;
36        $ret[] = $i;
37    }
38    foreach($m[2] as $i) {
39        if(trim($i)=="") continue;
40        $ret[] = $i;
41    }
42    if($file=="htmlarea.js") {
43        //toolbar-buttons
44        //bold:          [ "Bold"
45        preg_match_all('#[a-z]+: *\[ * "([^"]+)"#', $data, $m);
46        foreach($m[1] as $i) {
47            if(trim($i)=="") continue;
48            $ret[] = $i;
49        }
50
51        //config.fontname, fontsize and formatblock
52        $data = substr($data, strpos($data, "this.fontname = {"), strpos($data, "this.customSelects = {};")-strpos($data, "this.fontname = {"));
53        preg_match_all('#"([^"]+)"[ \t]*:[ \t]*["\'][^"\']*["\'],?#', $data, $m);
54        foreach($m[1] as $i) {
55            if(trim($i)=="") continue;
56            $ret[] = $i;
57        }
58    }
59}
60
61$files = getFiles("../popups/", "html$");
62foreach($files as $file)
63{
64    if(preg_match("#custom2.html$#", $file)) continue;
65    if(preg_match('#old_#', $file)) continue;
66    $ret = array_merge($ret, parseHtmlFile($file));
67}
68$ret = array_unique($ret);
69$langData['HTMLArea'] = $ret;
70
71
72
73$plugins = getFiles("../plugins/");
74foreach($plugins as $pluginDir)
75{
76    $plugin = substr($pluginDir, 11);
77    if($plugin=="ibrowser") continue;
78    $ret = array();
79
80    $files = getFiles("$pluginDir/", "js$");
81    $files = array_merge($files, getFiles("$pluginDir/popups/", "html$"));
82    $files = array_merge($files, getFiles("$pluginDir/", "php$"));
83    foreach($files as $file)
84    {
85        $fp = fopen($file, "r");
86        $data = "";
87        while(!feof($fp)) {
88            $data .= fread($fp, 1024);
89        }
90        preg_match_all('#_lc\("([^"]+)"|_lc\(\'([^\']+)\'#', $data, $m);
91        foreach($m[1] as $i) {
92            if(trim(strip_tags($i))=="") continue;
93            $ret[] = $i;
94        }
95        foreach($m[2] as $i) {
96            if(trim(strip_tags($i))=="") continue;
97            $ret[] = $i;
98        }
99    }
100
101    if($plugin=="TableOperations")
102    {
103        preg_match_all('#options = \\[([^\\]]+)\\];#', $data, $m);
104        foreach($m[1] as $i) {
105            preg_match_all('#"([^"]+)"#', $i, $m1);
106            foreach($m1[1] as $i) {
107                $ret[] = $i;
108            }
109        }
110       
111        //["cell-delete",        "td", "Delete cell"],
112        preg_match_all('#\\["[^"]+",[ \t]*"[^"]+",[ \t]*"([^"]+)"\\]#', $data, $m);
113        foreach($m[1] as $i) {
114            $ret[] = $i;
115        }
116    }
117
118
119    $files = getFiles("$pluginDir/", "html$");
120    $files = array_merge($files, getFiles("$pluginDir/", "php$"));
121    foreach($files as $file)
122    {
123        $ret = array_merge($ret, parseHtmlFile($file, $plugin));
124    }
125   
126    $files = getFiles("$pluginDir/popups/", "html$");
127    foreach($files as $file)
128    {
129        $ret = array_merge($ret, parseHtmlFile($file, $plugin));
130    }
131    $ret = array_unique($ret);
132
133    $langData[$plugin] = $ret;
134}
135
136foreach($langData as $plugin=>$strings)
137{
138    if(sizeof($strings)==0) continue;
139   
140
141    $data = "// I18N constants\n";
142    $data .= "//\n";
143    $data .= "//LANG: \"base\", ENCODING: UTF-8\n";
144    $data .= "//Author: Translator-Name, <email@example.com>\n";
145    $data .= "// FOR TRANSLATORS:\n";
146    $data .= "//\n";
147    $data .= "//   1. PLEASE PUT YOUR CONTACT INFO IN THE ABOVE LINE\n";
148    $data .= "//      (at least a valid email address)\n";
149    $data .= "//\n";
150    $data .= "//   2. PLEASE TRY TO USE UTF-8 FOR ENCODING;\n";
151    $data .= "//      (if this is not possible, please include a comment\n";
152    $data .= "//       that states what encoding is necessary.)\n";
153    $data .= "\n";
154    $data .= "{\n";
155    sort($strings);
156    foreach($strings as $string) {
157        $string = str_replace(array('\\', '"'), array('\\\\', '\\"'), $string);
158        $data .= "  \"".$string."\": \"\",\n";
159    }
160    $data = substr($data, 0, -2);
161    $data .= "\n";
162    $data .= "}\n";
163
164    if($plugin=="HTMLArea")
165        $file = "../lang/base.js";
166    else
167        $file = "../plugins/$plugin/lang/base.js";
168   
169    $fp = fopen($file, "w");
170    if(!$fp) continue;
171    fwrite($fp, $data);
172    fclose($fp);
173    echo "$file written...<br>";
174}
175
176
177
178
179function parseHtmlFile($file, $plugin="")
180{
181    $ret = array();
182   
183    $fp = fopen($file, "r");
184    if(!$fp) {
185        die("invalid fp");
186    }
187    $data = "";
188    while(!feof($fp)) {
189        $data .= fread($fp, 1024);
190    }
191   
192    if($plugin=="FormOperations" || $plugin=="SuperClean" || $plugin=="Linker") {
193        //<l10n>-tags for inline-dialog or panel-dialog based dialogs
194        $elems = array("l10n");
195    } else {
196        $elems = array("title", "input", "select", "legend", "span", "option", "td", "button", "div", "label");
197    }
198    foreach($elems as $elem) {
199        preg_match_all("#<{$elem}[^>]*>([^<^\"]+)</$elem>#i", $data, $m);
200        foreach($m[1] as $i) {
201            if(trim(strip_tags($i))=="") continue;
202            if($i=="/") continue;
203            if($plugin=="ImageManager" && preg_match('#^--+$#', $i)) continue; //skip those ------
204            if($plugin=="CharacterMap" && preg_match('#&[a-z0-9]+;#i', trim($i)) || $i=="@") continue;
205            if($plugin=="SpellChecker" && preg_match('#^\'\\.\\$[a-z]+\\.\'$#', $i)) continue;
206            $ret[] = trim($i);
207        }
208    }
209   
210    if($plugin=="FormOperations" || $plugin=="SuperClean" || $plugin=="Linker")
211    {
212        //_( for inline-dialog or panel-dialog based dialogs
213        preg_match_all('#"_\(([^"]+)\)"#i', $data, $m);
214        foreach($m[1] as $i) {
215            if(trim($i)=="") continue;
216            $ret[] = $i;
217        }
218    }
219    else
220    {
221        preg_match_all('#title="([^"]+)"#i', $data, $m);
222        foreach($m[1] as $i) {
223            if(trim(strip_tags($i))=="") continue;
224            if(strip_tags($i)==" - ") continue; //skip those - (ImageManager)
225            $ret[] = $i;
226        }
227    }
228    return($ret);
229}
230
231
232function getFiles($rootdirpath, $eregi_match='') {
233 $array = array();
234 if ($dir = @opendir($rootdirpath)) {
235   $array = array();
236   while (($file = readdir($dir)) !== false) {
237     if($file=="." || $file==".." || $file==".svn") continue;
238      if($eregi_match=="")
239        $array[] = $rootdirpath."/".$file;
240      else if(eregi($eregi_match,$file))
241        $array[] = $rootdirpath."/".$file;
242     
243   }
244   closedir($dir);
245 }
246 return $array;
247}
248
249
250
251
252
253?>
Note: See TracBrowser for help on using the repository browser.