|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gettext\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Gettext\Translations; |
|
6
|
|
|
|
|
7
|
|
|
final class PoGenerator extends Generator |
|
8
|
|
|
{ |
|
9
|
|
|
public function generateString(Translations $translations): string |
|
10
|
|
|
{ |
|
11
|
|
|
$pluralForm = $translations->getHeaders()->getPluralForm(); |
|
12
|
|
|
$pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
13
|
|
|
|
|
14
|
|
|
//Headers |
|
15
|
|
|
$lines = ['msgid ""', 'msgstr ""']; |
|
16
|
|
|
|
|
17
|
|
|
foreach ($translations->getHeaders() as $name => $value) { |
|
18
|
|
|
$lines[] = sprintf('"%s: %s\\n"', $name, $value); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
$lines[] = ''; |
|
22
|
|
|
|
|
23
|
|
|
//Translations |
|
24
|
|
|
foreach ($translations as $translation) { |
|
25
|
|
|
foreach ($translation->getComments() as $comment) { |
|
26
|
|
|
$lines[] = sprintf('# %s', $comment); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
foreach ($translation->getExtractedComments() as $comment) { |
|
30
|
|
|
$lines[] = sprintf('#. %s', $comment); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
foreach ($translation->getReferences() as $filename => $lineNumbers) { |
|
34
|
|
|
if (empty($lineNumbers)) { |
|
35
|
|
|
$lines[] = sprintf('#: %s', $filename); |
|
36
|
|
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
foreach ($lineNumbers as $number) { |
|
40
|
|
|
$lines[] = sprintf('#: %s:%d', $filename, $number); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (count($translation->getFlags())) { |
|
45
|
|
|
$lines[] = sprintf('#, %s', implode(',', $translation->getFlags()->toArray())); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$prefix = $translation->isDisabled() ? '#~ ' : ''; |
|
49
|
|
|
|
|
50
|
|
|
if ($context = $translation->getContext()) { |
|
51
|
|
|
$lines[] = sprintf('%smsgctxt %s', $prefix, self::encode($context)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
self::appendLines($lines, $prefix, 'msgid', $translation->getOriginal()); |
|
55
|
|
|
|
|
56
|
|
|
if ($plural = $translation->getPlural()) { |
|
57
|
|
|
self::appendLines($lines, $prefix, 'msgid_plural', $plural); |
|
58
|
|
|
self::appendLines($lines, $prefix, 'msgstr[0]', $translation->getTranslation() ?: ''); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { |
|
61
|
|
|
self::appendLines($lines, $prefix, sprintf('msgstr[%d]', $k + 1), $v); |
|
62
|
|
|
} |
|
63
|
|
|
} else { |
|
64
|
|
|
self::appendLines($lines, $prefix, 'msgstr', $translation->getTranslation() ?: ''); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$lines[] = ''; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return implode("\n", $lines); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Add one or more lines depending whether the string is multiline or not. |
|
75
|
|
|
*/ |
|
76
|
|
|
private static function appendLines(array &$lines, string $prefix, string $name, string $value): void |
|
77
|
|
|
{ |
|
78
|
|
|
$newLines = explode("\n", $value); |
|
79
|
|
|
$total = count($newLines); |
|
80
|
|
|
|
|
81
|
|
|
if ($total === 1) { |
|
82
|
|
|
$lines[] = sprintf('%s%s %s', $prefix, $name, self::encode($newLines[0])); |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$lines[] = sprintf('%s%s ""', $prefix, $name); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($newLines as $k => $line) { |
|
89
|
|
|
if ($k === $total - 1) { |
|
90
|
|
|
$line .= "\n"; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$lines[] = self::encode($line); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Convert a string to its PO representation. |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function encode(string $value): string |
|
101
|
|
|
{ |
|
102
|
|
|
return '"'.strtr( |
|
103
|
|
|
$value, |
|
104
|
|
|
[ |
|
105
|
|
|
"\x00" => '', |
|
106
|
|
|
'\\' => '\\\\', |
|
107
|
|
|
"\t" => '\t', |
|
108
|
|
|
"\r" => '\r', |
|
109
|
|
|
"\n" => '\n', |
|
110
|
|
|
'"' => '\\"', |
|
111
|
|
|
] |
|
112
|
|
|
).'"'; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|