Subversion Repositories oidplus

Rev

Rev 1162 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
557 daniel-mar 1
#!/usr/bin/env php
366 daniel-mar 2
<?php
3
 
4
/*
5
 * OIDplus 2.0
1127 daniel-mar 6
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
366 daniel-mar 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
$dir = __DIR__ . '/../../';
31
 
938 daniel-mar 32
// TODO: Support the case messages*.xml (for than 1 message file per language)
33
//       Not sure how to solve it, because if there is a string we do not have,
34
//       to which file do we add it?
35
 
366 daniel-mar 36
// ---
37
 
38
$langs = array();
938 daniel-mar 39
// TODO: Instead of hard-coding messages.xml, read it from manifest.xml
635 daniel-mar 40
$tmp = glob($dir.'/plugins/'.'*'.'/language/'.'*'.'/messages.xml');
366 daniel-mar 41
foreach ($tmp as $tmp2) {
42
        $tmp3 = explode('/', $tmp2);
43
        $lang = $tmp3[count($tmp3)-2];
44
        if ($lang == 'enus') continue; // ignore base lang
45
        $langs[] = $lang;
46
}
47
 
48
// ---
49
 
50
$all_strings = array();
51
 
52
$it = new RecursiveDirectoryIterator($dir);
1162 daniel-mar 53
$it->setFlags(FilesystemIterator::SKIP_DOTS); // DOES NOT WORK! Folders with . prefix still get evaluated!
366 daniel-mar 54
foreach(new RecursiveIteratorIterator($it) as $file) {
624 daniel-mar 55
        if ((strpos(str_replace('\\','/',realpath($file)),'/vendor/') !== false) && (strpos(str_replace('\\','/',realpath($file)),'/vendor/danielmarschall/') === false)) continue; // ignore third-party-code
366 daniel-mar 56
        if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities
1127 daniel-mar 57
 
58
        if (preg_match('@[/\\\\]\\.[^\\.]@',$file,$m)) continue; // Alternative to SKIP_DOTS
59
 
366 daniel-mar 60
        if ($file->getExtension() == 'php') {
61
                $cont = file_get_contents($file);
62
                $cont = phpRemoveComments($cont);
1133 daniel-mar 63
                // $cont = str_replace('function _L(string $str, ...$sprintfArgs): string {', '', $cont);
64
                $cont = str_replace('_L(string $', '', $cont);
624 daniel-mar 65
                $cont = str_replace('_L($', '', $cont);
366 daniel-mar 66
                $strings = get_php_L_strings($cont);
1133 daniel-mar 67
                $strings_test = get_js_L_strings($cont); // get_js_L_strings works with JS and PHP
366 daniel-mar 68
 
69
                if (serialize($strings) != serialize($strings_test)) {
70
                        echo "Attention: File ".realpath($file)." ambiguous _L() functions\n";
71
                }
72
 
73
                $all_strings = array_merge($all_strings, $strings);
74
        }
75
        if ($file->getExtension() == 'js') {
76
                $cont = file_get_contents($file);
77
                $cont = str_replace('function _L()', '', $cont);
78
                $strings = get_js_L_strings($cont);
79
                $all_strings = array_merge($all_strings, $strings);
80
        }
81
}
82
 
83
foreach ($all_strings as $str) {
84
        test_missing_placeholder($str);
85
}
86
 
87
$all_strings = array_unique($all_strings);
88
sort($all_strings);
89
 
90
// ---
91
 
92
foreach ($langs as $lang) {
938 daniel-mar 93
        // TODO: Instead of hard-coding messages.xml, read it from manifest.xml
635 daniel-mar 94
        $translation_files = glob($dir.'/plugins/'.'*'.'/language/'.$lang.'/messages.xml');
632 daniel-mar 95
        $translation_file = count($translation_files) > 0 ? $translation_files[0] : null;
366 daniel-mar 96
        if (file_exists($translation_file)) {
632 daniel-mar 97
                $xml = simplexml_load_string(file_get_contents($translation_file));
98
                if (!$xml) {
99
                        echo "STOP: Cannot load $translation_file\n";
100
                        continue;
101
                }
102
                foreach ($xml->message as $msg) {
366 daniel-mar 103
                        $src = trim($msg->source->__toString());
104
                        $dst = trim($msg->target->__toString());
105
                        $translation_array[$src] = $dst;
106
                }
107
        }
108
 
109
        // ---
110
 
111
        echo "Processing ".realpath($translation_file)." ...\n";
112
 
113
        $stats_total = 0;
114
        $stats_translated = 0;
115
        $stats_not_translated = 0;
116
 
1116 daniel-mar 117
        $cont  = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'."\n";
394 daniel-mar 118
        $cont .= '<translation'."\n";
119
        $cont .= '      xmlns="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.4.1"'."\n";
120
        $cont .= '      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
1439 daniel-mar 121
        $cont .= '      xsi:schemaLocation="urn:oid:1.3.6.1.4.1.37476.2.5.2.5.4.1 https://hosted.oidplus.com/viathinksoft/plugins/messages.xsd">'."\n";
394 daniel-mar 122
        $cont .= "\n";
366 daniel-mar 123
        foreach ($all_strings as $string) {
124
                $stats_total++;
506 daniel-mar 125
                $string = trim($string);
366 daniel-mar 126
                $cont .= "      <message>\n";
127
                $cont .= "              <source><![CDATA[\n";
128
                $cont .= "              $string\n";
129
                $cont .= "              ]]></source>\n";
130
                if (isset($translation_array[$string]) && !empty($translation_array[$string])) {
506 daniel-mar 131
                        $translation_array[$string] = trim($translation_array[$string]);
366 daniel-mar 132
                        $stats_translated++;
133
                        if (substr_count($string,'%') != substr_count($translation_array[$string],'%')) {
134
                                echo "\tAttention: Number of %-Replacements differs at translation of message '$string'\n";
135
                        }
136
                        $cont .= "              <target><![CDATA[\n";
137
                        $cont .= "              ".$translation_array[$string]."\n";
138
                        $cont .= "              ]]></target>\n";
139
                        test_missing_placeholder($translation_array[$string]);
140
                } else {
141
                        $stats_not_translated++;
142
                        $cont .= "              <target><![CDATA[\n";
143
                        $cont .= "              ]]></target><!-- TODO: TRANSLATE -->\n";
144
                }
145
                $cont .= "      </message>\n";
146
        }
147
        $cont .= "</translation>\n";
148
        file_put_contents($translation_file, $cont);
149
 
150
        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";
938 daniel-mar 151
        echo "\tDone...\n";
366 daniel-mar 152
}
153
 
154
if (count($langs) > 0) {
155
        echo "All done!\n";
156
} else {
157
        echo "Attention: No language plugins found!\n";
158
}
159
 
160
# ---
161
 
1130 daniel-mar 162
/**
163
 * @param string $cont
164
 * @return string[]
165
 */
166
function get_js_L_strings(string $cont): array {
366 daniel-mar 167
        // Works with JavaScript and PHP
168
        $cont = preg_replace('@/\\*.+\\*/@ismU', '', $cont);
169
        $cont = str_replace('\\"', chr(1), $cont);
170
        $cont = str_replace("\\'", chr(2), $cont);
171
        $cont = str_replace("\\\\", "\\", $cont);
386 daniel-mar 172
        $m = array();
879 daniel-mar 173
        preg_match_all('@[^_A-Za-z0-9]_L\\s*\\(([^\\)]*)(["\'])(.+)\\2@ismU', $cont, $m);
878 daniel-mar 174
        foreach ($m[3] as &$x) {
366 daniel-mar 175
                $x = str_replace(chr(1), '"', $x);
176
                $x = str_replace(chr(2), "'", $x);
177
        }
878 daniel-mar 178
        return $m[3];
366 daniel-mar 179
}
180
 
1130 daniel-mar 181
/**
182
 * @param string $cont
183
 * @return string[]
184
 */
185
function get_php_L_strings(string $cont): array {
366 daniel-mar 186
        // Works only with PHP
187
        $out = array();
188
        $tokens = token_get_all($cont);
189
        $activated = 0;
190
        foreach ($tokens as $token) {
191
                if (is_array($token)) {
192
                        if (($token[0] == T_STRING) && ($token[1] == '_L')) {
193
                                $activated = 1;
194
                        } else if (($activated == 1) && ($token[0] == T_CONSTANT_ENCAPSED_STRING)) {
195
                                $tmp = stripcslashes($token[1]);
196
                                $out[] = substr($tmp,1,strlen($tmp)-2);
197
                                $activated = 0;
198
                        }
199
                }
200
        }
201
        return $out;
202
}
203
 
1130 daniel-mar 204
/**
205
 * @param string $test
206
 * @return void
207
 */
208
function test_missing_placeholder(string $test) {
366 daniel-mar 209
        $max = -1;
210
        for ($i=99; $i>=1; $i--) {
211
                if (strpos($test, '%'.$i) !== false) {
212
                        $max = $i;
213
                        break;
214
                }
215
        }
216
 
217
        for ($i=1; $i<=$max; $i++) {
218
                if (strpos($test, '%'.$i) === false) {
219
                        echo "Attention: %$i is missing in string '$test'!\n";
220
                        $max = $i;
221
                        break;
222
                }
223
        }
370 daniel-mar 224
 
1140 daniel-mar 225
        $test = preg_replace('@%([1-9][0-9]|%)*@im', '', $test);
370 daniel-mar 226
        if (strpos($test,'%') !== false) {
227
                echo "Attention: Wrong percentage sign in '$test'!\n";
228
        }
366 daniel-mar 229
}
230
 
231
# ---
232
 
1130 daniel-mar 233
/**
234
 * @param string $fileStr
235
 * @return string
236
 */
237
function phpRemoveComments(string $fileStr): string {
366 daniel-mar 238
 
239
        // https://stackoverflow.com/questions/503871/best-way-to-automatically-remove-comments-from-php-code
240
 
241
        $newStr  = '';
242
 
243
        $commentTokens = array(T_COMMENT);
244
 
245
        if (defined('T_DOC_COMMENT')) $commentTokens[] = T_DOC_COMMENT; // PHP 5
246
        if (defined('T_ML_COMMENT'))  $commentTokens[] = T_ML_COMMENT;  // PHP 4
247
 
248
        $tokens = token_get_all($fileStr);
249
 
250
        foreach ($tokens as $token) {
251
                if (is_array($token)) {
252
                        if (in_array($token[0], $commentTokens)) continue;
253
                        $token = $token[1];
254
                }
255
                $newStr .= $token;
256
        }
257
 
258
        return $newStr;
259
 
260
}