alecpl
2012-04-22 390959bb323679f7611ee1585d8e1f55007c7773
commit | author | age
50ee4c 1 #!/usr/bin/env php
T 2 <?php
3 /*
4
5  +-----------------------------------------------------------------------+
6  | bin/exportgettext.sh                                                  |
7  |                                                                       |
8  | This file is part of the Roundcube Webmail client                     |
9  | Copyright (C) 2011, The Roundcube Dev Team                            |
070607 10  | Licensed under the GNU General Public License                         |
50ee4c 11  |                                                                       |
T 12  | PURPOSE:                                                              |
13  |   Export PHP-based localization files to PO files for gettext         |
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 DESTDIR\n");
27 }
28
29 $srcdir = unslashify(realpath($argv[1]));
84ff80 30 $destdir = unslashify($argv[2]);
T 31 $layout = 'launchpad';  # or 'narro';
c7dcb3 32 $langcode_map = array(
15dfdb 33     'hy_AM' => 'hy',
c7dcb3 34     'ar_SA' => 'ar',
T 35     'az_AZ' => 'az',
36     'bg_BG' => 'bg',
37     'bs_BA' => 'bs',
38     'ca_ES' => 'ca',
15dfdb 39     'cs_CZ' => 'cs',
c7dcb3 40     'cy_GB' => 'cy',
T 41     'da_DK' => 'da',
42     'et_EE' => 'et',
43     'el_GR' => 'el',
44     'eu_ES' => 'eu',
a0be7a 45     'fa_IR' => 'fa',
c7dcb3 46     'ga_IE' => 'ga',
15dfdb 47     'ka_GE' => 'ka',
c7dcb3 48     'gl_ES' => 'gl',
T 49     'he_IL' => 'he',
50     'hi_IN' => 'hi',
51     'hr_HR' => 'hr',
52     'ja_JP' => 'ja',
53     'ko_KR' => 'ko',
54     'km_KH' => 'km',
55     'ms_MY' => 'ms',
56     'mr_IN' => 'mr',
57     'pl_PL' => 'pl',
58     'si_LK' => 'si',
59     'sl_SI' => 'sl',
60     'sq_AL' => 'sq',
61     'sr_CS' => 'sr',
62     'sv_SE' => 'sv',
63     'uk_UA' => 'uk',
64     'vi_VN' => 'vi',
65 );
50ee4c 66
T 67
68 // converting roundcube localization dir
69 if (is_dir($srcdir.'/en_US')) {
70     load_en_US($srcdir.'/en_US');
71     
72     foreach (glob($srcdir.'/*') as $locdir) {
73         if (is_dir($locdir)) {
74             $lang = basename($locdir);
75             //echo "$locdir => $destdir$lang\n";
76             convert_dir($locdir, $destdir . ($layout != 'launchpad' ? $lang : ''));
77         }
78     }
79 }
7d84ff 80 // converting single localization directory
50ee4c 81 else if (is_dir($srcdir)) {
7d84ff 82     if (is_file($srcdir.'/en_US.inc'))  // plugin localization
T 83         load_en_US($srcdir.'/en_US.inc');
84     else
85         load_en_US(realpath($srcdir.'/../en_US'));  // single language
50ee4c 86     convert_dir($srcdir, $destdir);
T 87 }
88 // converting a single file
89 else if (is_file($srcdir)) {
90     //load_en_US();
91     convert_file($srcdir, $destdir);
92 }
93
94
95 /**
96  * Load en_US localization which is used to build msgids
97  */
98 function load_en_US($fn)
99 {
100     $texts = array();
101     
102     if (is_dir($fn)) {
103         foreach (glob($fn.'/*.inc') as $ifn) {
104             include($ifn);
105             $texts = array_merge($texts, (array)$labels, (array)$messages);
106         }
107     }
108     else if (is_file($fn)) {
109         include($fn);
19145d 110         $texts = array_merge($texts, (array)$labels, (array)$messages);
50ee4c 111     }
T 112     
113     $GLOBALS['en_US'] = $texts;
114 }
115
116 /**
117  * Convert all .inc files in the given src directory
118  */
119 function convert_dir($indir, $outdir)
120 {
121     global $layout;
122     
123     if (!is_dir($outdir))  // attempt to create destination dir
124         mkdir($outdir, 0777, true);
125
126     foreach (glob($indir.'/*.inc') as $fn) {
127         $filename = basename($fn);
128
129         // create subdir for each template (launchpad rules)
130         if ($layout == 'launchpad' && preg_match('/^(labels|messages)/', $filename, $m)) {
131             $lang = end(explode('/', $indir));
132             $destdir = $outdir . '/' . $m[1];
133             if (!is_dir($destdir))
134                 mkdir($destdir, 0777, true);
135             $outfn = $destdir . '/' . $lang . '.po';
136         }
137         else {
138             $outfn = $outdir . '/' . preg_replace('/\.[a-z0-9]+$/i', '', basename($fn)) . '.po';
139         }
140
141         convert_file($fn, $outfn);
142     }
143 }
144
145 /**
146  * Convert the given Roundcube localization file into a gettext .po file
147  */
148 function convert_file($fn, $outfn)
149 {
c7dcb3 150     global $layout, $langcode_map;
50ee4c 151
T 152     $basename =  basename($fn);
153     $srcname = str_replace(INSTALL_PATH, '', $fn);
84ff80 154     $product = preg_match('!plugins/(\w+)!', $srcname, $m) ? 'roundcube-plugin-' . $m[1] : 'roundcubemail';
50ee4c 155     $lang = preg_match('!/([a-z]{2}(_[A-Z]{2})?)[./]!', $outfn, $m) ? $m[1] : '';
T 156     $labels = $messages = $seen = array();
157
158     if (is_dir($outfn))
159         $outfn .= '/' . $basename . '.po';
160
161     // launchpad requires the template file to have the same name as the directory
162     if (strstr($outfn, '/en_US') && $layout == 'launchpad') {
163         $a = explode('/', $outfn);
164         array_pop($a);
165         $templ = end($a);
166         $a[] = $templ . '.pot';
167         $outfn = join('/', $a);
168         $is_pot = true;
169     }
170     // launchpad is very picky about file names
c7dcb3 171     else if ($layout == 'launchpad' && preg_match($regex = '!/([a-z]{2})_([A-Z]{2})!', $outfn, $m)) {
T 172         if ($shortlang = $langcode_map[$lang])
173             $outfn = preg_replace($regex, '/'.$shortlang, $outfn);
174         else if ($m[1] == strtolower($m[2]))
175             $outfn = preg_replace($regex, '/\1', $outfn);
50ee4c 176     }
T 177
178     include($fn);
c53293 179     $texts = array_merge($labels, $messages);
50ee4c 180     
T 181     // write header
182     $header = <<<EOF
183 # Converted from Roundcube PHP localization files
184 # Copyright (C) 2011 The Roundcube Dev Team
185 # This file is distributed under the same license as the Roundcube package.
186 #
187 #: %s
188 msgid ""
189 msgstr ""
84ff80 190 "Project-Id-Version: %s\\n"
50ee4c 191 "Report-Msgid-Bugs-To: \\n"
7d84ff 192 "%s: %s\\n"
50ee4c 193 "Last-Translator: \\n"
T 194 "Language-Team: Translations <hello@roundcube.net>\\n"
195 "Language: %s\\n"
196 "Content-Type: text/plain; charset=UTF-8\\n"
197 "Content-Transfer-Encoding: 8bit\\n"
198 EOF;
199     
c53293 200     $out = sprintf($header, $srcname, $product, $is_pot ? "POT-Creation-Date" : "PO-Revision-Date", date('c'), $shortlang ? $shortlang : $lang);
50ee4c 201     $out .= "\n";
T 202     
203     $messages = array();
204     foreach ((array)$texts as $label => $msgstr) {
7d84ff 205         $msgid = $is_pot ? $msgstr : ($GLOBALS['en_US'][$label] ?: $label);
50ee4c 206         $messages[$msgid][] = $label;
T 207     }
208     
209     foreach ($messages as $msgid => $labels) {
210         $out .= "\n";
211         foreach ($labels as $label)
212             $out .= "#: $srcname:$label\n";
213         $msgstr = $texts[$label];
214         $out .= 'msgid ' . gettext_quote($msgid) . "\n";
215         $out .= 'msgstr ' . gettext_quote(!$is_pot ? $msgstr : '') . "\n";
216     }
217
218     if ($outfn == '-')
219         echo $out;
220     else {
221         echo "$fn\t=>\t$outfn\n";
222         file_put_contents($outfn, $out);
223     }
224 }
225
226 function gettext_quote($str)
227 {
228     $out = "";
070607 229     $lines = explode("\n", wordwrap(stripslashes($str)));
5b5ed4 230     $last = count($lines) - 1;
T 231     foreach ($lines as $i => $line)
232         $out .= '"' . addcslashes($line, '"') . ($i < $last ? ' ' : '') . "\"\n";
50ee4c 233     return rtrim($out);
T 234 }
235
7fe381 236 ?>