-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgettext_php_to_po.php
More file actions
325 lines (301 loc) · 8.67 KB
/
gettext_php_to_po.php
File metadata and controls
325 lines (301 loc) · 8.67 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/***
* This script scans all PHP files for gettext() / _() function calls and
* generates .po files for multiple languages. You can specify the target
* languages for translation by changing the TARGET_LANGS define below.
*
* This script uses the AWS Translate API for translation. This API requires
* AWS authentication. See https://github.com/cilogon/aws-cli-setup
* for how to authenticate with your UIUC account, then log in to AWS Ohio
* (us-east-2). You must have an active AWS authn session.
*/
// A list of target languages for tranlation.
// NOTE: 'en' (English) must ALWAYS be a target.
// 've' is the upside-down English version (optional).
define('TARGET_LANGS', array('en_US', 've_ZA', 'fr_FR', 'de_DE'));
if ((!defined('TARGET_LANGS')) || empty(TARGET_LANGS)) {
echo "Please add at least one language to the TARGET_LANGS array.\n";
exit;
}
// Ensure that this script runs only from the command line
if (strlen($_SERVER['DOCUMENT_ROOT']) > 0) {
exit;
}
// Run this script only from the script directory
if (__DIR__ != getcwd()) {
echo 'Please run this script from the ' . __DIR__ . ' directory.' . "\n";
exit;
}
// Check if the service-lib repo exists at the same dir level
if (!file_exists('../service-lib/composer.json')) {
echo "Please run the following commands to check out the service-lib";
echo "repository at the same directory level as this repository.";
echo " pushd ..";
echo " git clone git@github.com:cilogon/service-lib.git";
echo " popd";
echo "Then run this script again.";
exit;
}
// Run 'composer update' to pull in library dependencies
$output = null;
if (
(exec('composer -V', $output, $result_code) === false) ||
($result_code != 0)
) {
echo "Unable to find 'composer' command.\n";
echo "Please see https://getcomposer.org/doc/00-intro.md for installation.\n";
exit;
}
exec('composer update');
// Create symlink to the cloned service-lib
if (!is_dir('vendor/cilogon')) {
mkdir('vendor/cilogon', 0755, true);
}
chdir('vendor/cilogon');
if (!is_link('service-lib')) {
system('rm -rf service-lib');
symlink('../../../service-lib', 'service-lib');
}
chdir('../..');
// Setup done! Now run the actual program to generate the translation files
require_once __DIR__ . '/vendor/autoload.php';
use Gettext\Scanner\PhpScanner;
use Gettext\Translations;
use Gettext\Loader\PoLoader;
use Gettext\Generator\PoGenerator;
use Gettext\Generator\MoGenerator;
use Aws\Exception\AwsException;
// Create a new AWS TranslateClient - requires AWS authentication
$awsclient = new Aws\Translate\TranslateClient([
'profile' => 'us-east-2',
'region' => 'us-east-2',
'version' => '2017-07-01'
]);
// Scan all php files for gettext() / _() function calls
$phpScanner = new PhpScanner(
Translations::create('cilogon')
);
$phpScanner->setDefaultDomain('cilogon');
foreach (glob('./{*,*/*,*/*/*,*/*/*/*,*/*/*/*/*,*/*/*/*/*/*}.php', GLOB_BRACE) as $file) {
// Skip non-cilogon vendor libraries
if (
(preg_match('%\./vendor/%', $file)) &&
(!preg_match('%\./vendor/cilogon/%', $file))
) {
continue;
}
$phpScanner->scanFile($file);
}
list('cilogon' => $translations) = $phpScanner->getTranslations();
// Loop through the TARGET_LANGS array, creating .po files for each
foreach (TARGET_LANGS as $lang) {
echo "Translating to '$lang'\n";
$translatedString = '';
foreach ($translations as $translation) {
if ($lang == 'en_US') { // For English, just copy the source string
$translatedString = $translation->getOriginal();
} elseif ($lang == 've_ZA') { // For upside-down, flip the source string
$translatedString = flipString($translation->getOriginal());
} else { // Use AWS Translate API for all other languages
try {
$result = $awsclient->translateText([
'SourceLanguageCode' => 'en',
'TargetLanguageCode' => substr($lang, 0, 2),
'Text' => $translation->getOriginal(),
]);
$translatedString = $result->get('TranslatedText');
} catch (AwsException $e) {
echo $e->getMessage() . "\n";
exit;
}
}
$translation->translate($translatedString);
}
$poGenerator = new PoGenerator();
if (!is_dir('locale/' . $lang . '/LC_MESSAGES')) {
mkdir('locale/' . $lang . '/LC_MESSAGES', 0755, true);
}
$poGenerator->generateFile($translations, 'locale/' . $lang . '/LC_MESSAGES/cilogon.po');
}
echo "Done!\n";
/***
* This function takes a string and rotates it 180 degrees so it looks like
* it has been flipped upside-down.
*/
function flipString($strtoflip)
{
$retstr = '';
for ($i = strlen($strtoflip)-1; $i >= 0; $i--) {
$retstr .= flipChar(substr($strtoflip, $i, 1));
}
return $retstr;
}
/***
* This function takes a single character and rotates it 180 degrees to it
* looks like it has been flipped upside-down. The following characters are
* used for the flip:
*
* Ɐ ꓭ Ɔ ꓷ Ǝ Ⅎ ꓨ H I ſ ꓘ ꓶ Ā N O Ԁ Ꝺ ꓤ S ꓕ ꓵ ꓥ M X ⅄ Z ɐ ā ɔ Ă ǝ ɟ ƃ ɥ ı̣ ɾ̣ ʞ ן ɯ ă o d b ɹ s ʇ n ʌ ʍ x ʎ z W q p u ' ¡ ⅋ ‾
*/
function flipChar($chartoflip)
{
$flipTable = array(
'a' => 'ɐ',
'b' => 'q',
'c' => 'ɔ',
'd' => 'p',
'e' => 'ǝ',
'f' => 'ɟ',
'g' => 'ƃ',
'h' => 'ɥ',
'i' => 'ᴉ',
'j' => 'ɾ',
'k' => 'ʞ',
'l' => 'ן',
'm' => 'ɯ',
'n' => 'u',
'p' => 'd',
'q' => 'b',
'r' => 'ɹ',
't' => 'ʇ',
'u' => 'n',
'v' => 'ʌ',
'w' => 'ʍ',
'y' => 'ʎ',
'A' => '∀',
'B' => 'ꓭ',
'C' => 'Ɔ',
'D' => 'ꓷ',
'E' => 'Ǝ',
'F' => 'Ⅎ',
'G' => 'ꓨ',
'H' => 'H',
'I' => 'I',
'J' => 'ſ',
'K' => 'ꓘ',
'L' => 'ꓶ',
'M' => 'W',
'N' => 'N',
'P' => 'Ԁ',
'Q' => 'Ꝺ',
'R' => 'ꓤ',
'T' => 'ꓕ',
'U' => 'ꓵ',
'V' => 'Λ',
'W' => 'M',
'Y' => '⅄',
'1' => 'Ɩ',
'2' => 'ᄅ',
'3' => 'Ɛ',
'4' => 'ㄣ',
'5' => 'ϛ',
'6' => '9',
'7' => 'ㄥ',
'8' => '8',
'9' => '6',
'0' => '0',
'.' => '˙',
',' => '\'',
'\'' => ',',
'"' => '˶',
'`' => ',',
'?' => '¿',
'!' => '¡',
'[' => ']',
']' => '[',
'(' => ')',
')' => '(',
'{' => '}',
'}' => '{',
'<' => '>',
'>' => '<',
'&' => '⅋',
'_' => '‾',
'∴' => '∵',
'⁅' => '⁆'
);
$flipTableFlipped = array(
'ɐ' => 'a',
'q' => 'b',
'ɔ' => 'c',
'p' => 'd',
'ǝ' => 'e',
'ɟ' => 'f',
'ƃ' => 'g',
'ɥ' => 'h',
'ᴉ' => 'i',
'ɾ' => 'j',
'ʞ' => 'k',
'ן' => 'l',
'ɯ' => 'm',
'u' => 'n',
'd' => 'p',
'b' => 'q',
'ɹ' => 'r',
'ʇ' => 't',
'n' => 'u',
'ʌ' => 'v',
'ʍ' => 'w',
'ʎ' => 'y',
'∀' => 'A',
'B' => 'ꓭ',
'Ɔ' => 'C',
'ꓷ' => 'D',
'Ǝ' => 'E',
'Ⅎ' => 'F',
'ꓨ' => 'G',
'H' => 'H',
'I' => 'I',
'ſ' => 'J',
'ꓘ' => 'K',
'ꓶ' => 'L',
'W' => 'M',
'N' => 'N',
'Ԁ' => 'P',
'Ꝺ' => 'Q',
'ꓤ' => 'R',
'ꓕ' => 'T',
'ꓵ' => 'U',
'Λ' => 'V',
'M' => 'W',
'⅄' => 'Y',
'Ɩ' => '1',
'ᄅ' => '2',
'Ɛ' => '3',
'ㄣ' => '4',
'ϛ' => '5',
'9' => '6',
'ㄥ' => '7',
'8' => '8',
'6' => '9',
'0' => '0',
'˙' => '.',
'\'' => ',',
',' => '\'',
'˶' => '"',
',' => '`',
'¿' => '?',
'¡' => '!',
']' => '[',
'[' => ']',
')' => '(',
'(' => ')',
'}' => '{',
'{' => '}',
'>' => '<',
'<' => '>',
'⅋' => '&',
'‾' => '_',
'∵' => '∴',
'⁆' => '⁅'
);
if (array_key_exists($chartoflip, $flipTable)) {
return $flipTable[$chartoflip];
} elseif (array_key_exists($chartoflip, $flipTableFlipped)) {
return $flipTableFlipped[$chartoflip];
} elseif (array_key_exists(strtolower($chartoflip), $flipTable)) {
return $flipTable[strtolower($chartoflip)];
} else {
return $chartoflip;
}
}