source: trunk/contrib/lc_parse_strings.php @ 291

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

fixed bug and some changes for changeset:290

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