1 | <?php |
---|
2 | |
---|
3 | // Author: Niels Baggesen, <nba@users.sourceforge.net>, 2009-08-16 |
---|
4 | |
---|
5 | // Distributed under the same terms as Xinha itself. |
---|
6 | // This notice MUST stay intact for use (see license.txt). |
---|
7 | |
---|
8 | // merge-strings.php - merge translation strings from master into |
---|
9 | // individual language files. |
---|
10 | |
---|
11 | |
---|
12 | function usage() |
---|
13 | { |
---|
14 | print "merge-strings.php - merge translation strings\n"; |
---|
15 | print "Options:\n"; |
---|
16 | print " -l xx Process language xx. Required option\n"; |
---|
17 | print " -m master Master file. Defaults to 'de.js'\n"; |
---|
18 | print " -m base Base directory. Defaults to '..'\n"; |
---|
19 | print " -v Verbose\n"; |
---|
20 | print " -c Tell about files that must be created\n"; |
---|
21 | print " -d Debug. Very noisy\n"; |
---|
22 | exit(1); |
---|
23 | } |
---|
24 | |
---|
25 | // This function taken from the php.net manual page for glob |
---|
26 | |
---|
27 | function rglob($pattern='*', $flags = 0, $path='') |
---|
28 | { |
---|
29 | $paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT); |
---|
30 | $files=glob($path.$pattern, $flags); |
---|
31 | foreach ($paths as $path) |
---|
32 | { |
---|
33 | $files=array_merge($files,rglob($pattern, $flags, $path)); |
---|
34 | } |
---|
35 | return $files; |
---|
36 | } |
---|
37 | |
---|
38 | error_reporting(E_ALL); |
---|
39 | |
---|
40 | $opts = getopt('l:b:m:cvd'); |
---|
41 | |
---|
42 | if ($opts === false) usage; |
---|
43 | // here we should check extra options, but php lacks that functionality |
---|
44 | |
---|
45 | $lang = 'xx'; // The language we process |
---|
46 | $create = 0; // Tell about missing files? |
---|
47 | $verbose = 0; // Log the details to stdout? |
---|
48 | $debug = 0; // ? |
---|
49 | $basedir = '..'; // Base directory to process |
---|
50 | $mastername = 'de.js'; // The best bet on a complete translation file |
---|
51 | |
---|
52 | if (isset($opts['l'])) $lang = $opts['l']; |
---|
53 | else die("Missing -l option\n"); |
---|
54 | if (isset($opts['c'])) $create = 1; |
---|
55 | if (isset($opts['v'])) $verbose = 1; |
---|
56 | if (isset($opts['d'])) $debug = 1; |
---|
57 | if (isset($opts['b'])) $basedir = $opts['b']; |
---|
58 | if (isset($opts['m'])) $mastername = $opts['m']; |
---|
59 | |
---|
60 | if (!preg_match('#/$#', $basedir)) $basedir .= '/'; |
---|
61 | |
---|
62 | // So, find all the master files |
---|
63 | |
---|
64 | $files = rglob($mastername, 0, $basedir); |
---|
65 | if (count($files) == 0) |
---|
66 | { |
---|
67 | print "No master files found. Check your -b and -m options!\n"; |
---|
68 | exit(1); |
---|
69 | } |
---|
70 | |
---|
71 | // and process them |
---|
72 | |
---|
73 | $filenum = 0; |
---|
74 | foreach ($files as $masterjs) |
---|
75 | { |
---|
76 | $langjs = preg_replace("/$mastername/", "$lang.js", $masterjs); |
---|
77 | $langnew = $langjs.'.new'; |
---|
78 | |
---|
79 | if (!file_exists($langjs)) |
---|
80 | { |
---|
81 | if ($create) print "Missing file: $langjs\n"; |
---|
82 | continue; |
---|
83 | } |
---|
84 | |
---|
85 | // Populate $trans with the strings that must be translated |
---|
86 | |
---|
87 | $filenum++; |
---|
88 | $min = fopen($masterjs, "r"); |
---|
89 | $trans = array(); |
---|
90 | $strings = 0; |
---|
91 | while ($str = fgets($min)) |
---|
92 | { |
---|
93 | $str = trim($str); |
---|
94 | if (preg_match('#^ *"([^"]*)" *: *"([^"]*)"(,)? *(//.*)?$#', $str, $m)) |
---|
95 | { |
---|
96 | if (isset($trans[$m[1]])) |
---|
97 | { |
---|
98 | print "Duplicate string in $masterjs: $m[1]\n"; |
---|
99 | continue; |
---|
100 | } |
---|
101 | if ($debug) print "Translate: $m[1]\n"; |
---|
102 | $trans[$m[1]] = 1; |
---|
103 | $strings++; |
---|
104 | } |
---|
105 | } |
---|
106 | fclose($min); |
---|
107 | |
---|
108 | // Now copy from $lin to $lout while verifying that the strings |
---|
109 | // are still current. |
---|
110 | // Break out when we hit the last string in the master (no ',' |
---|
111 | // after the translation. |
---|
112 | |
---|
113 | $lin = fopen($langjs, "r"); |
---|
114 | $lout = fopen($langnew, "w"); |
---|
115 | $obsolete = 0; |
---|
116 | $new = 0; |
---|
117 | $kept = 0; |
---|
118 | while ($fstr = fgets($lin)) |
---|
119 | { |
---|
120 | $str = trim($fstr); |
---|
121 | if (preg_match('#^ *"([^"]*)" *: *"([^"]*)"(,)? *(//.*)?$#', $str, $m)) |
---|
122 | { |
---|
123 | if (!isset($trans[$m[1]])) |
---|
124 | { |
---|
125 | if ($verbose) print "Obsolete: $m[1]\n"; |
---|
126 | $obsolete++; |
---|
127 | fprintf($lout, " // \"%s\": \"%s\" // OBSOLETE\n", $m[1], $m[2]); |
---|
128 | } |
---|
129 | else |
---|
130 | { |
---|
131 | if ($debug) print "Keep: $m[1]\n"; |
---|
132 | unset($trans[$m[1]]); |
---|
133 | $strings--; |
---|
134 | $kept++; |
---|
135 | fprintf($lout, " \"%s\": \"%s\"%s\n", $m[1], $m[2], $strings ? ',' : ''); |
---|
136 | } |
---|
137 | if (!isset($m[3]) || $m[3] != ',') |
---|
138 | break; |
---|
139 | } |
---|
140 | else |
---|
141 | fprintf($lout, "%s", $fstr); |
---|
142 | } |
---|
143 | |
---|
144 | // Add the strings that are missing |
---|
145 | |
---|
146 | foreach ($trans as $tr => $v) |
---|
147 | { |
---|
148 | if ($verbose) print("New: $tr\n"); |
---|
149 | $new++; |
---|
150 | $strings--; |
---|
151 | fprintf($lout, " \"%s\": \"%s\"%s // NEW\n", $tr, $tr, $strings ? ',' : ''); |
---|
152 | } |
---|
153 | |
---|
154 | // And then the final part of $lin |
---|
155 | |
---|
156 | while ($str = fgets($lin)) |
---|
157 | fprintf($lout, "%s", $str); |
---|
158 | |
---|
159 | // Clean up, and tell user what happened |
---|
160 | |
---|
161 | fclose($lin); |
---|
162 | fclose($lout); |
---|
163 | |
---|
164 | if ($obsolete == 0 && $new == 0) |
---|
165 | { |
---|
166 | if ($verbose) print "$langjs: Unchanged\n"; |
---|
167 | unlink($langnew); |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | print "$langnew: $new new, $obsolete obsoleted, $kept unchanged entries.\n"; |
---|
172 | // rename($langnew, $langjs); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | print "$filenum files processed.\n"; |
---|
177 | |
---|
178 | ?> |
---|