summaryrefslogtreecommitdiff
path: root/bin/importgettext.sh
blob: 5a91e6bc153aa8713e805a68338e2f327bb12203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env php
<?php
/*
 +-----------------------------------------------------------------------+
 | bin/importgettext.sh                                                  |
 |                                                                       |
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2011, The Roundcube Dev Team                            |
 | Licensed under the GNU General Public License                         |
 |                                                                       |
 | PURPOSE:                                                              |
 |   Import localizations from gettext PO format                         |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
 +-----------------------------------------------------------------------+
*/

define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
require INSTALL_PATH.'program/include/clisetup.php';

if ($argc < 2) {
	die("Usage: " . basename($argv[0]) . " SRCDIR\n");
}

$srcdir = unslashify(realpath($argv[1]));

if (is_dir($srcdir)) {
	$out = import_dir($srcdir);
}
else if (is_file($srcdir)) {
	$out = import_file($srcdir);
}

// write output files
foreach ($out as $outfn => $texts) {
	$lang = preg_match('!/([a-z]{2}(_[A-Z]{2})?)[./]!', $outfn, $m) ? $m[1] : '';
	$varname = strpos($outfn, 'messages.inc') !== false ? 'messages' : 'labels';

	$header = <<<EOF
<?php

/*
 +-----------------------------------------------------------------------+
 | localization/%s/%-51s|
 |                                                                       |
 | Language file of the Roundcube Webmail client                         |
 | Copyright (C) %s, The Roundcube Dev Team                            |
 | Licensed under the GNU General Public License                         |
 |                                                                       |
 +-----------------------------------------------------------------------+
 | Author: %-62s|
 +-----------------------------------------------------------------------+
*/

$%s = array();

EOF;

	$author = preg_replace('/\s*<Unknown>/i', '', $texts['_translator']);
	$output = sprintf($header, $lang, $varname.'.inc', date('Y'), $author, $varname);

	foreach ($texts as $label => $value) {
		if (is_array($value)) { var_dump($outfn, $label, $value); exit; }
		if ($label[0] != '_' && strlen($value))
			$output .= sprintf("\$%s['%s'] = '%s';\n", $varname, $label, strtr(addcslashes($value, "'"), array("\r" => '', "\n" => '\n')));
	}

	$output .= "\n";
	$dir = dirname($outfn);
	@mkdir($dir, 0755, true);
	if (file_put_contents($outfn, $output))
		echo "-> $outfn\n";
}


/**
 * Convert all .po files in the given src directory
 */
function import_dir($indir)
{
	$out = array();
	foreach (glob($indir.'/*.po') as $fn) {
		$out = array_merge_recursive($out, import_file($fn));
	}
	return $out;
}

/**
 * Convert the given .po file into a Roundcube localization array
 */
function import_file($fn)
{
	$out = array();
	$lines = file($fn);
	$language = '';
	$translator = '';

	// get language code from file name
	if (preg_match('/-([a-z_]+).po$/i', $fn, $m))
	  $language = expand_langcode($m[1]);

	$is_header = true;
	$msgid = null;
	$msgstr = '';
	$dests = array();
	foreach ($lines as $i => $line) {
		$line = trim($line);

		// parse header
		if ($is_header && $line[0] == '"') {
			list($key, $val) = explode(": ", preg_replace('/\\\n$/', '', trim($line, '"')), 2);
			switch (strtolower($key)) {
				case 'language':
					$language = expand_langcode($val);
					break;
				case 'last-translator':
					$translator = $val;
					break;
			}
		}

		// empty line
		if ($line == '') {
			if ($msgid && $dests) {
				foreach ($dests as $dest) {
					list($file, $label) = explode(':', $dest);
					$out[$file][$label] = $msgstr;
					$out[$file]['_translator'] = $translator;
				}
			}

			$msgid = null;
			$msgstr = '';
			$dests = array();
		}

		// meta line
		if ($line[0] == '#') {
			$value = trim(substr($line, 2));
			if ($line[1] == ':')
				$dests[] = str_replace('en_US', $language, $value);
		}
		else if (strpos($line, 'msgid') === 0) {
			$msgid = gettext_decode(substr($line, 6));

			if (!empty($msgid))
				$is_header = false;
		}
		else if (strpos($line, 'msgstr') === 0) {
			$msgstr = gettext_decode(substr($line, 7));
		}
		else if ($msgid && $line[0] == '"') {
			$msgstr .= gettext_decode($line);
		}
		else if ($msgid !== null && $line[0] == '"') {
			$msgid .= gettext_decode($line);
		}
	}

	if ($msgid && $dests) {
		foreach ($dests as $dest) {
			list($file, $label) = explode(':', $dest);
			$out[$file][$label] = $msgstr;
			$out[$file]['_translator'] = $translator;
		}
	}

	return $language ? $out : array();
}


function gettext_decode($str)
{
	return stripslashes(trim($str, '"'));
}

/**
 * Translate two-chars language codes to our internally used language identifiers
 */
function expand_langcode($lang)
{
	static $rcube_language_aliases, $rcube_languages;

	if (!$rcube_language_aliases)
		include(INSTALL_PATH . 'program/localization/index.inc');

	if ($rcube_language_aliases[$lang])
		return $rcube_language_aliases[$lang];
	else if (strlen($lang) == 2 && !isset($rcube_languages[$lang]))
		return strtolower($lang) . '_' . strtoupper($lang);
	else
		return $lang;
}


?>