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