commit | author | age
|
070607
|
1 |
#!/usr/bin/env php |
T |
2 |
<?php |
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| bin/importgettext.sh | |
|
6 |
| | |
|
7 |
| This file is part of the Roundcube Webmail client | |
|
8 |
| Copyright (C) 2011, The Roundcube Dev Team | |
|
9 |
| Licensed under the GNU General Public License | |
|
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Import localizations from gettext PO format | |
|
13 |
+-----------------------------------------------------------------------+ |
|
14 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
*/ |
|
17 |
|
0ea079
|
18 |
define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' ); |
070607
|
19 |
require INSTALL_PATH.'program/include/clisetup.php'; |
T |
20 |
|
|
21 |
if ($argc < 2) { |
|
22 |
die("Usage: " . basename($argv[0]) . " SRCDIR\n"); |
|
23 |
} |
|
24 |
|
|
25 |
$srcdir = unslashify(realpath($argv[1])); |
|
26 |
|
|
27 |
if (is_dir($srcdir)) { |
|
28 |
$out = import_dir($srcdir); |
|
29 |
} |
|
30 |
else if (is_file($srcdir)) { |
|
31 |
$out = import_file($srcdir); |
|
32 |
} |
|
33 |
|
|
34 |
// write output files |
|
35 |
foreach ($out as $outfn => $texts) { |
|
36 |
$lang = preg_match('!/([a-z]{2}(_[A-Z]{2})?)[./]!', $outfn, $m) ? $m[1] : ''; |
|
37 |
$varname = strpos($outfn, 'messages.inc') !== false ? 'messages' : 'labels'; |
041c93
|
38 |
|
070607
|
39 |
$header = <<<EOF |
T |
40 |
<?php |
|
41 |
|
|
42 |
/* |
|
43 |
+-----------------------------------------------------------------------+ |
|
44 |
| localization/%s/%-51s| |
|
45 |
| | |
|
46 |
| Language file of the Roundcube Webmail client | |
|
47 |
| Copyright (C) %s, The Roundcube Dev Team | |
|
48 |
| Licensed under the GNU General Public License | |
|
49 |
| | |
|
50 |
+-----------------------------------------------------------------------+ |
|
51 |
| Author: %-62s| |
|
52 |
+-----------------------------------------------------------------------+ |
|
53 |
*/ |
|
54 |
|
|
55 |
$%s = array(); |
|
56 |
|
|
57 |
EOF; |
|
58 |
|
de1040
|
59 |
$author = preg_replace('/\s*<Unknown>/i', '', $texts['_translator']); |
bd7b8a
|
60 |
$output = sprintf($header, $lang, $varname.'.inc', date('Y'), $author, $varname); |
070607
|
61 |
|
T |
62 |
foreach ($texts as $label => $value) { |
de1040
|
63 |
if (is_array($value)) { var_dump($outfn, $label, $value); exit; } |
6a41be
|
64 |
if ($label[0] != '_' && strlen($value)) |
070607
|
65 |
$output .= sprintf("\$%s['%s'] = '%s';\n", $varname, $label, strtr(addcslashes($value, "'"), array("\r" => '', "\n" => '\n'))); |
T |
66 |
} |
|
67 |
|
|
68 |
$output .= "\n"; |
|
69 |
$dir = dirname($outfn); |
cc9f35
|
70 |
@mkdir($dir, 0755, true); |
070607
|
71 |
if (file_put_contents($outfn, $output)) |
T |
72 |
echo "-> $outfn\n"; |
|
73 |
} |
|
74 |
|
|
75 |
|
|
76 |
/** |
|
77 |
* Convert all .po files in the given src directory |
|
78 |
*/ |
|
79 |
function import_dir($indir) |
|
80 |
{ |
|
81 |
$out = array(); |
|
82 |
foreach (glob($indir.'/*.po') as $fn) { |
|
83 |
$out = array_merge_recursive($out, import_file($fn)); |
|
84 |
} |
|
85 |
return $out; |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* Convert the given .po file into a Roundcube localization array |
|
90 |
*/ |
|
91 |
function import_file($fn) |
|
92 |
{ |
|
93 |
$out = array(); |
|
94 |
$lines = file($fn); |
6a41be
|
95 |
$language = ''; |
070607
|
96 |
$translator = ''; |
T |
97 |
|
ae4bad
|
98 |
// get language code from file name |
TB |
99 |
if (preg_match('/-([a-z_]+).po$/i', $fn, $m)) |
|
100 |
$language = expand_langcode($m[1]); |
|
101 |
|
070607
|
102 |
$is_header = true; |
T |
103 |
$msgid = null; |
|
104 |
$msgstr = ''; |
|
105 |
$dests = array(); |
|
106 |
foreach ($lines as $i => $line) { |
|
107 |
$line = trim($line); |
|
108 |
|
|
109 |
// parse header |
|
110 |
if ($is_header && $line[0] == '"') { |
|
111 |
list($key, $val) = explode(": ", preg_replace('/\\\n$/', '', trim($line, '"')), 2); |
|
112 |
switch (strtolower($key)) { |
|
113 |
case 'language': |
|
114 |
$language = expand_langcode($val); |
|
115 |
break; |
|
116 |
case 'last-translator': |
|
117 |
$translator = $val; |
|
118 |
break; |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
// empty line |
|
123 |
if ($line == '') { |
|
124 |
if ($msgid && $dests) { |
|
125 |
foreach ($dests as $dest) { |
|
126 |
list($file, $label) = explode(':', $dest); |
|
127 |
$out[$file][$label] = $msgstr; |
de1040
|
128 |
$out[$file]['_translator'] = $translator; |
070607
|
129 |
} |
T |
130 |
} |
041c93
|
131 |
|
070607
|
132 |
$msgid = null; |
T |
133 |
$msgstr = ''; |
|
134 |
$dests = array(); |
|
135 |
} |
|
136 |
|
|
137 |
// meta line |
|
138 |
if ($line[0] == '#') { |
|
139 |
$value = trim(substr($line, 2)); |
|
140 |
if ($line[1] == ':') |
|
141 |
$dests[] = str_replace('en_US', $language, $value); |
|
142 |
} |
|
143 |
else if (strpos($line, 'msgid') === 0) { |
|
144 |
$msgid = gettext_decode(substr($line, 6)); |
|
145 |
|
|
146 |
if (!empty($msgid)) |
|
147 |
$is_header = false; |
|
148 |
} |
|
149 |
else if (strpos($line, 'msgstr') === 0) { |
|
150 |
$msgstr = gettext_decode(substr($line, 7)); |
|
151 |
} |
|
152 |
else if ($msgid && $line[0] == '"') { |
|
153 |
$msgstr .= gettext_decode($line); |
|
154 |
} |
|
155 |
else if ($msgid !== null && $line[0] == '"') { |
|
156 |
$msgid .= gettext_decode($line); |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
if ($msgid && $dests) { |
|
161 |
foreach ($dests as $dest) { |
|
162 |
list($file, $label) = explode(':', $dest); |
|
163 |
$out[$file][$label] = $msgstr; |
|
164 |
$out[$file]['_translator'] = $translator; |
|
165 |
} |
|
166 |
} |
041c93
|
167 |
|
6a41be
|
168 |
return $language ? $out : array(); |
070607
|
169 |
} |
T |
170 |
|
|
171 |
|
|
172 |
function gettext_decode($str) |
|
173 |
{ |
|
174 |
return stripslashes(trim($str, '"')); |
|
175 |
} |
|
176 |
|
|
177 |
/** |
|
178 |
* Translate two-chars language codes to our internally used language identifiers |
|
179 |
*/ |
|
180 |
function expand_langcode($lang) |
|
181 |
{ |
6a41be
|
182 |
static $rcube_language_aliases, $rcube_languages; |
070607
|
183 |
|
T |
184 |
if (!$rcube_language_aliases) |
|
185 |
include(INSTALL_PATH . 'program/localization/index.inc'); |
|
186 |
|
|
187 |
if ($rcube_language_aliases[$lang]) |
|
188 |
return $rcube_language_aliases[$lang]; |
6a41be
|
189 |
else if (strlen($lang) == 2 && !isset($rcube_languages[$lang])) |
070607
|
190 |
return strtolower($lang) . '_' . strtoupper($lang); |
T |
191 |
else |
|
192 |
return $lang; |
|
193 |
} |
|
194 |
|
|
195 |
|
7fe381
|
196 |
?> |