Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 197 lines (167 sloc) 5.339 kb
50ee4cc @thomascube Script to export Roundcube (PHP) localization files to gettext .po(t) fo...
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 |
10 | Licensed under the GNU GPLv3 |
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]));
30 $destdir = slashify($argv[2]);
31 $layout = 'launchpad' # or 'narro';
32
33
34 // converting roundcube localization dir
35 if (is_dir($srcdir.'/en_US')) {
36 load_en_US($srcdir.'/en_US');
37
38 foreach (glob($srcdir.'/*') as $locdir) {
39 if (is_dir($locdir)) {
40 $lang = basename($locdir);
41 //echo "$locdir => $destdir$lang\n";
42 convert_dir($locdir, $destdir . ($layout != 'launchpad' ? $lang : ''));
43 }
44 }
45 }
46 // converting plugin localization
47 else if (is_dir($srcdir)) {
48 load_en_US($srcdir.'/en_US.inc');
49 convert_dir($srcdir, $destdir);
50 }
51 // converting a single file
52 else if (is_file($srcdir)) {
53 //load_en_US();
54 convert_file($srcdir, $destdir);
55 }
56
57
58 /**
59 * Load en_US localization which is used to build msgids
60 */
61 function load_en_US($fn)
62 {
63 $texts = array();
64
65 if (is_dir($fn)) {
66 foreach (glob($fn.'/*.inc') as $ifn) {
67 include($ifn);
68 $texts = array_merge($texts, (array)$labels, (array)$messages);
69 }
70 }
71 else if (is_file($fn)) {
72 include($fn);
73 $texts = array_merge($texts, (array)$labels);
74 }
75
76 $GLOBALS['en_US'] = $texts;
77 }
78
79 /**
80 * Convert all .inc files in the given src directory
81 */
82 function convert_dir($indir, $outdir)
83 {
84 global $layout;
85
86 $outdir = unslashify($outdir);
87 if (!is_dir($outdir)) // attempt to create destination dir
88 mkdir($outdir, 0777, true);
89
90 foreach (glob($indir.'/*.inc') as $fn) {
91 $filename = basename($fn);
92
93 // create subdir for each template (launchpad rules)
94 if ($layout == 'launchpad' && preg_match('/^(labels|messages)/', $filename, $m)) {
95 $lang = end(explode('/', $indir));
96 $destdir = $outdir . '/' . $m[1];
97 if (!is_dir($destdir))
98 mkdir($destdir, 0777, true);
99 $outfn = $destdir . '/' . $lang . '.po';
100 }
101 else {
102 $outfn = $outdir . '/' . preg_replace('/\.[a-z0-9]+$/i', '', basename($fn)) . '.po';
103 }
104
105 convert_file($fn, $outfn);
106 }
107 }
108
109 /**
110 * Convert the given Roundcube localization file into a gettext .po file
111 */
112 function convert_file($fn, $outfn)
113 {
114 global $layout;
115
116 $basename = basename($fn);
117 $srcname = str_replace(INSTALL_PATH, '', $fn);
118 $lang = preg_match('!/([a-z]{2}(_[A-Z]{2})?)[./]!', $outfn, $m) ? $m[1] : '';
119 $labels = $messages = $seen = array();
120
121 if (is_dir($outfn))
122 $outfn .= '/' . $basename . '.po';
123
124 // launchpad requires the template file to have the same name as the directory
125 if (strstr($outfn, '/en_US') && $layout == 'launchpad') {
126 $a = explode('/', $outfn);
127 array_pop($a);
128 $templ = end($a);
129 $a[] = $templ . '.pot';
130 $outfn = join('/', $a);
131 $is_pot = true;
132 }
133 // launchpad is very picky about file names
134 else if ($layout == 'launchpad' && preg_match($regex = '!/([a-z]{2})_([A-Z]{2})!', $outfn, $m) && $m[1] == strtolower($m[2])) {
135 $outfn = preg_replace($regex, '/\1', $outfn);
136 }
137
138 include($fn);
139 $texts = $labels ? $labels : $messages;
140
141 // write header
142 $header = <<<EOF
143 # Converted from Roundcube PHP localization files
144 # Copyright (C) 2011 The Roundcube Dev Team
145 # This file is distributed under the same license as the Roundcube package.
146 #
147 #: %s
148 msgid ""
149 msgstr ""
150 "Project-Id-Version: roundcubemail\\n"
151 "Report-Msgid-Bugs-To: \\n"
152 "POT-Creation-Date: %s\\n"
153 "PO-Revision-Date: %s\\n"
154 "Last-Translator: \\n"
155 "Language-Team: Translations <hello@roundcube.net>\\n"
156 "Language: %s\\n"
157 "Content-Type: text/plain; charset=UTF-8\\n"
158 "Content-Transfer-Encoding: 8bit\\n"
159 EOF;
160
161 $out = sprintf($header, $srcname, date('c'), date('c'), $lang);
162 $out .= "\n";
163
164 $messages = array();
165 foreach ((array)$texts as $label => $msgstr) {
166 $msgid = $GLOBALS['en_US'][$label] ?: $label;
167 $messages[$msgid][] = $label;
168 }
169
170 foreach ($messages as $msgid => $labels) {
171 $out .= "\n";
172 foreach ($labels as $label)
173 $out .= "#: $srcname:$label\n";
174 $msgstr = $texts[$label];
175 $out .= 'msgid ' . gettext_quote($msgid) . "\n";
176 $out .= 'msgstr ' . gettext_quote(!$is_pot ? $msgstr : '') . "\n";
177 }
178
179 if ($outfn == '-')
180 echo $out;
181 else {
182 echo "$fn\t=>\t$outfn\n";
183 file_put_contents($outfn, $out);
184 }
185 }
186
187 function gettext_quote($str)
188 {
189 $out = "";
190 $lines = explode("\n", $str);
191 $suffix = count($lines) > 1 ? '\n' : '';
192 foreach ($lines as $line)
193 $out .= '"' . addcslashes($line, '"') . $suffix . "\"\n";
194 return rtrim($out);
195 }
196
197 ?>
Something went wrong with that request. Please try again.