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