Rev 394 | Rev 511 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
366 | daniel-mar | 1 | #!/usr/bin/php |
2 | <?php |
||
3 | |||
4 | /* |
||
5 | * OIDplus 2.0 |
||
6 | * Copyright 2020 Daniel Marschall, ViaThinkSoft |
||
7 | * |
||
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
9 | * you may not use this file except in compliance with the License. |
||
10 | * You may obtain a copy of the License at |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
21 | // This script updates the language message files (adding new entries and |
||
22 | // removing entries that are not existing anymore). |
||
23 | // It requires that PHP scripts are using following syntax for translations: |
||
24 | // _L('hello world',optionalParams) <recommended> |
||
25 | // _L("hello world",optionalParams) |
||
26 | // and JS files: |
||
27 | // _L('hello world',optionalParams) |
||
28 | // _L("hello world",optionalParams) <recommended> |
||
29 | |||
30 | |||
31 | $dir = __DIR__ . '/../../'; |
||
32 | |||
33 | // --- |
||
34 | |||
35 | $langs = array(); |
||
36 | $tmp = glob($dir.'/plugins/language/*/messages.xml'); |
||
37 | foreach ($tmp as $tmp2) { |
||
38 | $tmp3 = explode('/', $tmp2); |
||
39 | $lang = $tmp3[count($tmp3)-2]; |
||
40 | if ($lang == 'enus') continue; // ignore base lang |
||
41 | $langs[] = $lang; |
||
42 | } |
||
43 | |||
44 | // --- |
||
45 | |||
46 | $all_strings = array(); |
||
47 | |||
48 | $it = new RecursiveDirectoryIterator($dir); |
||
49 | foreach(new RecursiveIteratorIterator($it) as $file) { |
||
50 | if (strpos(str_replace('\\','/',realpath($file)),'/3p/') !== false) continue; // ignore third-party-code |
||
51 | if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities |
||
52 | if ($file->getExtension() == 'php') { |
||
53 | $cont = file_get_contents($file); |
||
54 | $cont = phpRemoveComments($cont); |
||
55 | $cont = str_replace('function _L($str, ...$sprintfArgs) {', '', $cont); |
||
56 | $strings = get_php_L_strings($cont); |
||
57 | $strings_test = get_js_L_strings($cont); |
||
58 | |||
59 | if (serialize($strings) != serialize($strings_test)) { |
||
60 | echo "Attention: File ".realpath($file)." ambiguous _L() functions\n"; |
||
61 | } |
||
62 | |||
63 | $all_strings = array_merge($all_strings, $strings); |
||
64 | } |
||
65 | if ($file->getExtension() == 'js') { |
||
66 | $cont = file_get_contents($file); |
||
67 | $cont = str_replace('function _L()', '', $cont); |
||
68 | $strings = get_js_L_strings($cont); |
||
69 | $all_strings = array_merge($all_strings, $strings); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | foreach ($all_strings as $str) { |
||
74 | test_missing_placeholder($str); |
||
75 | } |
||
76 | |||
77 | $all_strings = array_unique($all_strings); |
||
78 | sort($all_strings); |
||
79 | |||
80 | // --- |
||
81 | |||
82 | foreach ($langs as $lang) { |
||
83 | $translation_array = array(); |
||
84 | $translation_file = $dir.'/plugins/language/'.$lang.'/messages.xml'; |
||
85 | if (file_exists($translation_file)) { |
||
86 | $xml = simplexml_load_string(file_get_contents($translation_file)); |
||
87 | if (!$xml) { |
||
88 | echo "STOP: Cannot load $translation_file\n"; |
||
89 | continue; |
||
90 | } |
||
91 | foreach ($xml->message as $msg) { |
||
92 | $src = trim($msg->source->__toString()); |
||
93 | $dst = trim($msg->target->__toString()); |
||
94 | $translation_array[$src] = $dst; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | // --- |
||
99 | |||
100 | echo "Processing ".realpath($translation_file)." ...\n"; |
||
101 | |||
102 | $stats_total = 0; |
||
103 | $stats_translated = 0; |
||
104 | $stats_not_translated = 0; |
||
105 | |||
106 | $cont = ''; |
||
394 | daniel-mar | 107 | $cont .= '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n"; |
108 | $cont .= '<translation'."\n"; |
||
109 | $cont .= ' xmlns="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.4.1"'."\n"; |
||
110 | $cont .= ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n"; |
||
111 | $cont .= ' xsi:schemaLocation="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.4.1 https://oidplus.viathinksoft.com/oidplus/plugins/messages.xsd">'."\n"; |
||
112 | $cont .= "\n"; |
||
366 | daniel-mar | 113 | foreach ($all_strings as $string) { |
114 | $stats_total++; |
||
506 | daniel-mar | 115 | $string = trim($string); |
366 | daniel-mar | 116 | $cont .= " <message>\n"; |
117 | $cont .= " <source><![CDATA[\n"; |
||
118 | $cont .= " $string\n"; |
||
119 | $cont .= " ]]></source>\n"; |
||
120 | if (isset($translation_array[$string]) && !empty($translation_array[$string])) { |
||
506 | daniel-mar | 121 | $translation_array[$string] = trim($translation_array[$string]); |
366 | daniel-mar | 122 | $stats_translated++; |
123 | if (substr_count($string,'%') != substr_count($translation_array[$string],'%')) { |
||
124 | echo "\tAttention: Number of %-Replacements differs at translation of message '$string'\n"; |
||
125 | } |
||
126 | $cont .= " <target><![CDATA[\n"; |
||
127 | $cont .= " ".$translation_array[$string]."\n"; |
||
128 | $cont .= " ]]></target>\n"; |
||
129 | test_missing_placeholder($translation_array[$string]); |
||
130 | } else { |
||
131 | $stats_not_translated++; |
||
132 | $cont .= " <target><![CDATA[\n"; |
||
133 | $cont .= " ]]></target><!-- TODO: TRANSLATE -->\n"; |
||
134 | } |
||
135 | $cont .= " </message>\n"; |
||
136 | } |
||
137 | $cont .= "</translation>\n"; |
||
138 | file_put_contents($translation_file, $cont); |
||
139 | |||
140 | echo "\t$stats_total total messages, $stats_translated already translated (".round($stats_translated/$stats_total*100,2)."%), $stats_not_translated not translated (".round($stats_not_translated/$stats_total*100,2)."%)\n"; |
||
141 | echo "\tDone..."; |
||
142 | } |
||
143 | |||
144 | if (count($langs) > 0) { |
||
145 | echo "All done!\n"; |
||
146 | } else { |
||
147 | echo "Attention: No language plugins found!\n"; |
||
148 | } |
||
149 | |||
150 | # --- |
||
151 | |||
152 | function get_js_L_strings($cont) { |
||
153 | // Works with JavaScript and PHP |
||
154 | $cont = preg_replace('@/\\*.+\\*/@ismU', '', $cont); |
||
155 | $cont = str_replace('\\"', chr(1), $cont); |
||
156 | $cont = str_replace("\\'", chr(2), $cont); |
||
157 | $cont = str_replace("\\\\", "\\", $cont); |
||
386 | daniel-mar | 158 | $m = array(); |
366 | daniel-mar | 159 | preg_match_all('@[^_A-Za-z0-9]_L\\(.*(["\'])(.+)\\1@ismU', $cont, $m); |
160 | foreach ($m[2] as &$x) { |
||
161 | $x = str_replace(chr(1), '"', $x); |
||
162 | $x = str_replace(chr(2), "'", $x); |
||
163 | } |
||
164 | return $m[2]; |
||
165 | } |
||
166 | |||
167 | function get_php_L_strings($cont) { |
||
168 | // Works only with PHP |
||
169 | $out = array(); |
||
170 | $tokens = token_get_all($cont); |
||
171 | $activated = 0; |
||
172 | foreach ($tokens as $token) { |
||
173 | if (is_array($token)) { |
||
174 | if (($token[0] == T_STRING) && ($token[1] == '_L')) { |
||
175 | $activated = 1; |
||
176 | } else if (($activated == 1) && ($token[0] == T_CONSTANT_ENCAPSED_STRING)) { |
||
177 | $tmp = stripcslashes($token[1]); |
||
178 | $out[] = substr($tmp,1,strlen($tmp)-2); |
||
179 | $activated = 0; |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | return $out; |
||
184 | } |
||
185 | |||
186 | function test_missing_placeholder($test) { |
||
187 | $max = -1; |
||
188 | for ($i=99; $i>=1; $i--) { |
||
189 | if (strpos($test, '%'.$i) !== false) { |
||
190 | $max = $i; |
||
191 | break; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | for ($i=1; $i<=$max; $i++) { |
||
196 | if (strpos($test, '%'.$i) === false) { |
||
197 | echo "Attention: %$i is missing in string '$test'!\n"; |
||
198 | $max = $i; |
||
199 | break; |
||
200 | } |
||
201 | } |
||
370 | daniel-mar | 202 | |
203 | $test = preg_replace('@%([1-9][0-9]|%)*@ism', '', $test); |
||
204 | if (strpos($test,'%') !== false) { |
||
205 | echo "Attention: Wrong percentage sign in '$test'!\n"; |
||
206 | } |
||
366 | daniel-mar | 207 | } |
208 | |||
209 | # --- |
||
210 | |||
211 | function phpRemoveComments($fileStr) { |
||
212 | |||
213 | // https://stackoverflow.com/questions/503871/best-way-to-automatically-remove-comments-from-php-code |
||
214 | |||
215 | $newStr = ''; |
||
216 | |||
217 | $commentTokens = array(T_COMMENT); |
||
218 | |||
219 | if (defined('T_DOC_COMMENT')) $commentTokens[] = T_DOC_COMMENT; // PHP 5 |
||
220 | if (defined('T_ML_COMMENT')) $commentTokens[] = T_ML_COMMENT; // PHP 4 |
||
221 | |||
222 | $tokens = token_get_all($fileStr); |
||
223 | |||
224 | foreach ($tokens as $token) { |
||
225 | if (is_array($token)) { |
||
226 | if (in_array($token[0], $commentTokens)) continue; |
||
227 | $token = $token[1]; |
||
228 | } |
||
229 | $newStr .= $token; |
||
230 | } |
||
231 | |||
232 | return $newStr; |
||
233 | |||
234 | } |