Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 196 lines (164 sloc) 5.377 kB
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
1 #!/usr/bin/env php
2 <?php
3 /*
4 +-----------------------------------------------------------------------+
5 | bin/importgettext.sh |
6 | |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2011, The Roundcube Dev Team |
9 | Licensed under the GNU General Public License |
10 | |
11 | PURPOSE: |
12 | Import localizations from gettext PO format |
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\n");
23 }
24
25 $srcdir = unslashify(realpath($argv[1]));
26
27 if (is_dir($srcdir)) {
28 $out = import_dir($srcdir);
29 }
30 else if (is_file($srcdir)) {
31 $out = import_file($srcdir);
32 }
33
34 // write output files
35 foreach ($out as $outfn => $texts) {
36 $lang = preg_match('!/([a-z]{2}(_[A-Z]{2})?)[./]!', $outfn, $m) ? $m[1] : '';
37 $varname = strpos($outfn, 'messages.inc') !== false ? 'messages' : 'labels';
041c93c @alecpl Removed $Id$
alecpl authored
38
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
39 $header = <<<EOF
40 <?php
41
42 /*
43 +-----------------------------------------------------------------------+
44 | localization/%s/%-51s|
45 | |
46 | Language file of the Roundcube Webmail client |
47 | Copyright (C) %s, The Roundcube Dev Team |
48 | Licensed under the GNU General Public License |
49 | |
50 +-----------------------------------------------------------------------+
51 | Author: %-62s|
52 +-----------------------------------------------------------------------+
53 */
54
55 $%s = array();
56
57 EOF;
58
bd7b8a6 @alecpl Remove <Unknown> from localization author line
alecpl authored
59 $author = preg_replace('/\s*<Unknown>/i', '', $texts['_translator']);
60 $output = sprintf($header, $lang, $varname.'.inc', date('Y'), $author, $varname);
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
61
62 foreach ($texts as $label => $value) {
6a41bef @thomascube Fix gettext importer to correctly convert launchpad .po files
thomascube authored
63 if (is_array($value)) { var_dump($outfn, $label, $value); exit; }
64 if ($label[0] != '_' && strlen($value))
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
65 $output .= sprintf("\$%s['%s'] = '%s';\n", $varname, $label, strtr(addcslashes($value, "'"), array("\r" => '', "\n" => '\n')));
66 }
67
68 $output .= "\n";
69 $dir = dirname($outfn);
cc9f35a @thomascube Set the right permissions on new localization folders
thomascube authored
70 @mkdir($dir, 0755, true);
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
71 if (file_put_contents($outfn, $output))
72 echo "-> $outfn\n";
73 }
74
75
76 /**
77 * Convert all .po files in the given src directory
78 */
79 function import_dir($indir)
80 {
81 $out = array();
82 foreach (glob($indir.'/*.po') as $fn) {
83 $out = array_merge_recursive($out, import_file($fn));
84 }
85 return $out;
86 }
87
88 /**
89 * Convert the given .po file into a Roundcube localization array
90 */
91 function import_file($fn)
92 {
93 $out = array();
94 $lines = file($fn);
6a41bef @thomascube Fix gettext importer to correctly convert launchpad .po files
thomascube authored
95 $language = '';
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
96 $translator = '';
97
ae4bad9 @thomascube Get language code from filename if not specified in the .po file itse…
thomascube authored
98 // get language code from file name
99 if (preg_match('/-([a-z_]+).po$/i', $fn, $m))
100 $language = expand_langcode($m[1]);
101
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
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 }
041c93c @alecpl Removed $Id$
alecpl authored
130
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
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 }
041c93c @alecpl Removed $Id$
alecpl authored
166
6a41bef @thomascube Fix gettext importer to correctly convert launchpad .po files
thomascube authored
167 return $language ? $out : array();
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
168 }
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 {
6a41bef @thomascube Fix gettext importer to correctly convert launchpad .po files
thomascube authored
181 static $rcube_language_aliases, $rcube_languages;
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
182
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];
6a41bef @thomascube Fix gettext importer to correctly convert launchpad .po files
thomascube authored
188 else if (strlen($lang) == 2 && !isset($rcube_languages[$lang]))
0706075 @thomascube Fix newline handling in gettext export; add import script to read .po…
thomascube authored
189 return strtolower($lang) . '_' . strtoupper($lang);
190 else
191 return $lang;
192 }
193
194
7fe3811 @thomascube Changed license to GNU GPLv3+ with exceptions for skins and plugins
thomascube authored
195 ?>
Something went wrong with that request. Please try again.