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