-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoHeader.php
587 lines (532 loc) · 16.7 KB
/
PoHeader.php
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<?php
namespace Drupal\Component\Gettext;
/**
* Gettext PO header handler.
*
* Possible Gettext PO header elements are explained in
* http://www.gnu.org/software/gettext/manual/gettext.html#Header-Entry,
* but we only support a subset of these directly.
*
* Example header:
*
* "Project-Id-Version: Drupal core (7.11)\n"
* "POT-Creation-Date: 2012-02-12 22:59+0000\n"
* "PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\n"
* "Language-Team: Catalan\n"
* "MIME-Version: 1.0\n"
* "Content-Type: text/plain; charset=utf-8\n"
* "Content-Transfer-Encoding: 8bit\n"
* "Plural-Forms: nplurals=2; plural=(n>1);\n"
*/
class PoHeader {
/**
* Language code.
*
* @var string
*/
protected $langcode;
/**
* Formula for the plural form.
*
* @var string
*/
protected $pluralForms;
/**
* Author(s) of the file.
*
* @var string[]
*/
protected $authors;
/**
* Date the po file got created.
*
* @var string
*/
protected $poDate;
/**
* Human readable language name.
*
* @var string
*/
protected $languageName;
/**
* Name of the project the translation belongs to.
*
* @var string
*/
protected $projectName;
/**
* Constructor, creates a PoHeader with default values.
*
* @param string $langcode
* Language code.
*/
public function __construct($langcode = NULL) {
$this->langcode = $langcode;
// Ignore errors when run during site installation before
// date_default_timezone_set() is called.
$this->poDate = @date("Y-m-d H:iO");
$this->pluralForms = 'nplurals=2; plural=(n > 1);';
}
/**
* Gets the plural form.
*
* @return string
* Plural form component from the header, for example:
* 'nplurals=2; plural=(n > 1);'.
*/
public function getPluralForms() {
return $this->pluralForms;
}
/**
* Set the human readable language name.
*
* @param string $languageName
* Human readable language name.
*/
public function setLanguageName($languageName) {
$this->languageName = $languageName;
}
/**
* Gets the human readable language name.
*
* @return string
* The human readable language name.
*/
public function getLanguageName() {
return $this->languageName;
}
/**
* Set the project name.
*
* @param string $projectName
* Human readable project name.
*/
public function setProjectName($projectName) {
$this->projectName = $projectName;
}
/**
* Gets the project name.
*
* @return string
* The human readable project name.
*/
public function getProjectName() {
return $this->projectName;
}
/**
* Populate internal values from a string.
*
* @param string $header
* Full header string with key-value pairs.
*/
public function setFromString($header) {
// Get an array of all header values for processing.
$values = $this->parseHeader($header);
// There is only one value relevant for our header implementation when
// reading, and that is the plural formula.
if (!empty($values['Plural-Forms'])) {
$this->pluralForms = $values['Plural-Forms'];
}
}
/**
* Generate a Gettext PO formatted header string based on data set earlier.
*/
public function __toString() {
$output = '';
$isTemplate = empty($this->languageName);
$output .= '# ' . ($isTemplate ? 'LANGUAGE' : $this->languageName) . ' translation of ' . ($isTemplate ? 'PROJECT' : $this->projectName) . "\n";
if (!empty($this->authors)) {
$output .= '# Generated by ' . implode("\n# ", $this->authors) . "\n";
}
$output .= "#\n";
// Add the actual header information.
$output .= "msgid \"\"\n";
$output .= "msgstr \"\"\n";
$output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
$output .= "\"POT-Creation-Date: " . $this->poDate . "\\n\"\n";
$output .= "\"PO-Revision-Date: " . $this->poDate . "\\n\"\n";
$output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
$output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
$output .= "\"MIME-Version: 1.0\\n\"\n";
$output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
$output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
$output .= "\"Plural-Forms: " . $this->pluralForms . "\\n\"\n";
$output .= "\n";
return $output;
}
/**
* Parses a Plural-Forms entry from a Gettext Portable Object file header.
*
* @param string $plural_forms
* The Plural-Forms entry value.
*
* @return array|bool
* An indexed array of parsed plural formula data. Containing:
* - 'nplurals': The number of plural forms defined by the plural formula.
* - 'plurals': Array of plural positions keyed by plural value.
* False when there is no plural string.
*
* @throws \Exception
*/
public function parsePluralForms($plural_forms) {
$plurals = [];
// First, delete all whitespace.
$plural_forms = strtr($plural_forms, [" " => "", "\t" => ""]);
// Select the parts that define nplurals and plural.
$nplurals = strstr($plural_forms, "nplurals=");
if (strpos($nplurals, ";")) {
// We want the string from the 10th char, because "nplurals=" length is 9.
$nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9);
}
else {
return FALSE;
}
$plural = strstr($plural_forms, "plural=");
if (strpos($plural, ";")) {
// We want the string from the 8th char, because "plural=" length is 7.
$plural = substr($plural, 7, strpos($plural, ";") - 7);
}
else {
return FALSE;
}
// If the number of plurals is zero, we return a default result.
if ($nplurals == 0) {
return [$nplurals, ['default' => 0]];
}
// Calculate possible plural positions of different plural values. All known
// plural formula's are repetitive above 100.
// For data compression we store the last position the array value
// changes and store it as default.
$element_stack = $this->parseArithmetic($plural);
if ($element_stack !== FALSE) {
for ($i = 0; $i <= 199; $i++) {
$plurals[$i] = $this->evaluatePlural($element_stack, $i);
}
$default = $plurals[$i - 1];
$plurals = array_filter($plurals, function ($value) use ($default) {
return ($value != $default);
});
$plurals['default'] = $default;
return [$nplurals, $plurals];
}
else {
throw new \Exception('The plural formula could not be parsed.');
}
}
/**
* Parses a Gettext Portable Object file header.
*
* @param string $header
* A string containing the complete header.
*
* @return array
* An associative array of key-value pairs.
*/
private function parseHeader($header) {
$header_parsed = [];
$lines = array_map('trim', explode("\n", $header));
foreach ($lines as $line) {
if ($line) {
[$tag, $contents] = explode(":", $line, 2);
$header_parsed[trim($tag)] = trim($contents);
}
}
return $header_parsed;
}
/**
* Parses and sanitizes an arithmetic formula into a plural element stack.
*
* While parsing, we ensure, that the operators have the right
* precedence and associativity.
*
* @param string $string
* A string containing the arithmetic formula.
*
* @return array|bool
* A stack of values and operations to be evaluated. False if the formula
* could not be parsed.
*/
private function parseArithmetic($string) {
// Operator precedence table.
$precedence = ["(" => -1, ")" => -1, "?" => 1, ":" => 1, "||" => 3, "&&" => 4, "==" => 5, "!=" => 5, "<" => 6, ">" => 6, "<=" => 6, ">=" => 6, "+" => 7, "-" => 7, "*" => 8, "/" => 8, "%" => 8];
// Right associativity.
$right_associativity = ["?" => 1, ":" => 1];
$tokens = $this->tokenizeFormula($string);
// Parse by converting into infix notation then back into postfix
// Operator stack - holds math operators and symbols.
$operator_stack = [];
// Element Stack - holds data to be operated on.
$element_stack = [];
foreach ($tokens as $token) {
$current_token = $token;
// Numbers and the $n variable are simply pushed into $element_stack.
if (is_numeric($token)) {
$element_stack[] = $current_token;
}
elseif ($current_token == "n") {
$element_stack[] = '$n';
}
elseif ($current_token == "(") {
$operator_stack[] = $current_token;
}
elseif ($current_token == ")") {
$top_op = array_pop($operator_stack);
while (isset($top_op) && ($top_op != "(")) {
$element_stack[] = $top_op;
$top_op = array_pop($operator_stack);
}
}
elseif (!empty($precedence[$current_token])) {
// If it's an operator, then pop from $operator_stack into
// $element_stack until the precedence in $operator_stack is less
// than current, then push into $operator_stack.
$top_op = array_pop($operator_stack);
while (isset($top_op) && ($precedence[$top_op] >= $precedence[$current_token]) && !(($precedence[$top_op] == $precedence[$current_token]) && !empty($right_associativity[$top_op]) && !empty($right_associativity[$current_token]))) {
$element_stack[] = $top_op;
$top_op = array_pop($operator_stack);
}
if ($top_op) {
// Return element to top.
$operator_stack[] = $top_op;
}
// Parentheses are not needed.
$operator_stack[] = $current_token;
}
else {
return FALSE;
}
}
// Flush operator stack.
$top_op = array_pop($operator_stack);
while ($top_op != NULL) {
$element_stack[] = $top_op;
$top_op = array_pop($operator_stack);
}
$return = $element_stack;
// Now validate stack.
$previous_size = count($element_stack) + 1;
while (count($element_stack) < $previous_size) {
$previous_size = count($element_stack);
for ($i = 2; $i < count($element_stack); $i++) {
$op = $element_stack[$i];
if (!empty($precedence[$op])) {
if ($op == ":") {
$f = $element_stack[$i - 2] . "):" . $element_stack[$i - 1] . ")";
}
elseif ($op == "?") {
$f = "(" . $element_stack[$i - 2] . "?(" . $element_stack[$i - 1];
}
else {
$f = "(" . $element_stack[$i - 2] . $op . $element_stack[$i - 1] . ")";
}
array_splice($element_stack, $i - 2, 3, $f);
break;
}
}
}
// If only one element is left, the number of operators is appropriate.
return count($element_stack) == 1 ? $return : FALSE;
}
/**
* Tokenize the formula.
*
* @param string $formula
* A string containing the arithmetic formula.
*
* @return array
* List of arithmetic tokens identified in the formula.
*/
private function tokenizeFormula($formula) {
$formula = str_replace(" ", "", $formula);
$tokens = [];
for ($i = 0; $i < strlen($formula); $i++) {
if (is_numeric($formula[$i])) {
$num = $formula[$i];
$j = $i + 1;
while ($j < strlen($formula) && is_numeric($formula[$j])) {
$num .= $formula[$j];
$j++;
}
$i = $j - 1;
$tokens[] = $num;
}
elseif ($pos = strpos(" =<>!&|", $formula[$i])) {
$next = $formula[$i + 1];
switch ($pos) {
case 1:
case 2:
case 3:
case 4:
if ($next == '=') {
$tokens[] = $formula[$i] . '=';
$i++;
}
else {
$tokens[] = $formula[$i];
}
break;
case 5:
if ($next == '&') {
$tokens[] = '&&';
$i++;
}
else {
$tokens[] = $formula[$i];
}
break;
case 6:
if ($next == '|') {
$tokens[] = '||';
$i++;
}
else {
$tokens[] = $formula[$i];
}
break;
}
}
else {
$tokens[] = $formula[$i];
}
}
return $tokens;
}
/**
* Evaluate the plural element stack using a plural value.
*
* Using an element stack, which represents a plural formula, we calculate
* which plural string should be used for a given plural value.
*
* An example of plural formula parting and evaluation:
* Plural formula: 'n!=1'
* This formula is parsed by parseArithmetic() to a stack (array) of elements:
* @code
* [
* 0 => '$n',
* 1 => '1',
* 2 => '!=',
* ];
* @endcode
* The evaluatePlural() method evaluates the $element_stack using the plural
* value $n. Before the actual evaluation, the '$n' in the array is replaced
* by the value of $n.
* For example: $n = 2 results in:
* @code
* [
* 0 => '2',
* 1 => '1',
* 2 => '!=',
* ]
* @endcode
* The stack is processed until only one element is (the result) is left. In
* every iteration the top elements of the stack, up until the first operator,
* are evaluated. After evaluation the arguments and the operator itself are
* removed and replaced by the evaluation result. This is typically 2
* arguments and 1 element for the operator.
* Because the operator is '!=' the example stack is evaluated as:
* $f = (int) 2 != 1;
* The resulting stack is:
* @code
* [
* 0 => 1,
* ]
* @endcode
* With only one element left in the stack (the final result) the loop is
* terminated and the result is returned.
*
* @param array $element_stack
* Array of plural formula values and operators create by parseArithmetic().
* @param int $n
* The @count number for which we are determining the right plural position.
*
* @return int
* Number of the plural string to be used for the given plural value.
*
* @see parseArithmetic()
*
* @throws \Exception
*/
protected function evaluatePlural($element_stack, $n) {
$count = count($element_stack);
$limit = $count;
// Replace the '$n' value in the formula by the plural value.
for ($i = 0; $i < $count; $i++) {
if ($element_stack[$i] === '$n') {
$element_stack[$i] = $n;
}
}
// We process the stack until only one element is (the result) is left.
// We limit the number of evaluation cycles to prevent an endless loop in
// case the stack contains an error.
while (isset($element_stack[1])) {
for ($i = 2; $i < $count; $i++) {
// There's no point in checking non-symbols. Also, switch(TRUE) would
// match any case and so it would break.
if (is_bool($element_stack[$i]) || is_numeric($element_stack[$i])) {
continue;
}
$f = NULL;
$length = 3;
$delta = 2;
switch ($element_stack[$i]) {
case '==':
$f = $element_stack[$i - 2] == $element_stack[$i - 1];
break;
case '!=':
$f = $element_stack[$i - 2] != $element_stack[$i - 1];
break;
case '<=':
$f = $element_stack[$i - 2] <= $element_stack[$i - 1];
break;
case '>=':
$f = $element_stack[$i - 2] >= $element_stack[$i - 1];
break;
case '<':
$f = $element_stack[$i - 2] < $element_stack[$i - 1];
break;
case '>':
$f = $element_stack[$i - 2] > $element_stack[$i - 1];
break;
case '+':
$f = $element_stack[$i - 2] + $element_stack[$i - 1];
break;
case '-':
$f = $element_stack[$i - 2] - $element_stack[$i - 1];
break;
case '*':
$f = $element_stack[$i - 2] * $element_stack[$i - 1];
break;
case '/':
$f = $element_stack[$i - 2] / $element_stack[$i - 1];
break;
case '%':
$f = $element_stack[$i - 2] % $element_stack[$i - 1];
break;
case '&&':
$f = $element_stack[$i - 2] && $element_stack[$i - 1];
break;
case '||':
$f = $element_stack[$i - 2] || $element_stack[$i - 1];
break;
case ':':
$f = $element_stack[$i - 3] ? $element_stack[$i - 2] : $element_stack[$i - 1];
// This operator has 3 preceding elements, instead of the default 2.
$length = 5;
$delta = 3;
break;
}
// If the element is an operator we remove the processed elements and
// store the result.
if (isset($f)) {
array_splice($element_stack, $i - $delta, $length, $f);
break;
}
}
}
if (!$limit) {
throw new \Exception('The plural formula could not be evaluated.');
}
return (int) $element_stack[0];
}
}