Subversion Repositories oidplus

Rev

Rev 386 | Rev 557 | 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
511 daniel-mar 6
 * Copyright 2019 - 2021 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
// Use this script to create files containing all strings of a specific language
22
// These files can be used for proofing (e.g. using Microsoft Word)
23
 
24
$dir = __DIR__ . '/../../';
25
 
26
// ---
27
 
28
$langs = array();
29
$tmp = glob($dir.'/plugins/language/*/messages.xml');
30
foreach ($tmp as $tmp2) {
31
        $tmp3 = explode('/', $tmp2);
32
        $lang = $tmp3[count($tmp3)-2];
33
        if ($lang == 'enus') continue; // ignore base lang
34
        $langs[] = $lang;
35
}
36
 
37
// ---
38
 
39
$all_strings = array();
40
 
41
$it = new RecursiveDirectoryIterator($dir);
42
foreach(new RecursiveIteratorIterator($it) as $file) {
43
        if (strpos(str_replace('\\','/',realpath($file)),'/3p/') !== false) continue; // ignore third-party-code
44
        if (strpos(str_replace('\\','/',realpath($file)),'/dev/') !== false) continue; // ignore development utilities
45
        if ($file->getExtension() == 'php') {
46
                $cont = file_get_contents($file);
47
                $cont = str_replace('function _L($str, ...$sprintfArgs) {', '', $cont);
48
                $strings = get_php_L_strings($cont);
49
                $all_strings = array_merge($all_strings, $strings);
50
        }
51
        if ($file->getExtension() == 'js') {
52
                $cont = file_get_contents($file);
53
                $cont = str_replace('function _L()', '', $cont);
54
                $strings = get_js_L_strings($cont);
55
                $all_strings = array_merge($all_strings, $strings);
56
        }
57
}
58
 
59
$all_strings = array_unique($all_strings);
60
sort($all_strings);
61
 
62
file_put_contents(__DIR__.'/.proof_enus.txt', implode("\r\n\r\n", $all_strings));
63
echo "Done: enus\n";
64
 
65
// ---
66
 
67
foreach ($langs as $lang) {
68
        $all_strings = array();
69
        $translation_file = $dir.'/plugins/language/'.$lang.'/messages.xml';
70
        if (file_exists($translation_file)) {
71
        $xml = simplexml_load_string(file_get_contents($translation_file));
72
        if (!$xml) {
73
                echo "STOP: Cannot load $translation_file\n";
74
                continue;
75
        }
76
        foreach ($xml->message as $msg) {
77
                        $dst = trim($msg->target->__toString());
78
                        $all_strings[] = $dst;
79
                }
80
        }
81
 
82
        sort($all_strings);
83
 
84
        file_put_contents(__DIR__.'/.proof_'.$lang.'.txt', implode("\r\n\r\n", $all_strings));
85
        echo "Done: $lang\n";
86
}
87
 
88
if (count($langs) > 0) {
89
        echo "All done!\n";
90
} else {
91
        echo "Attention: No language plugins found!\n";
92
}
93
 
94
# ---
95
 
96
function get_js_L_strings($cont) {
97
        // Works with JavaScript and PHP
98
        $cont = preg_replace('@/\\*.+\\*/@ismU', '', $cont);
99
        $cont = str_replace('\\"', chr(1), $cont);
100
        $cont = str_replace("\\'", chr(2), $cont);
101
        $cont = str_replace("\\\\", "\\", $cont);
386 daniel-mar 102
        $m = array();
366 daniel-mar 103
        preg_match_all('@[^_A-Za-z0-9]_L\\(.*(["\'])(.+)\\1@ismU', $cont, $m);
104
        foreach ($m[2] as &$x) {
105
                $x = str_replace(chr(1), '"', $x);
106
                $x = str_replace(chr(2), "'", $x);
107
        }
108
        return $m[2];
109
}
110
 
111
function get_php_L_strings($cont) {
112
        // Works only with PHP
113
        $out = array();
114
        $tokens = token_get_all($cont);
115
        $activated = 0;
116
        foreach ($tokens as $token) {
117
                if (is_array($token)) {
118
                        if (($token[0] == T_STRING) && ($token[1] == '_L')) {
119
                                $activated = 1;
120
                        } else if (($activated == 1) && ($token[0] == T_CONSTANT_ENCAPSED_STRING)) {
121
                                $tmp = stripcslashes($token[1]);
122
                                $out[] = substr($tmp,1,strlen($tmp)-2);
123
                                $activated = 0;
124
                        }
125
                }
126
        }
127
        return $out;
128
}