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