1 | <?php |
||
7 | class Po extends Generator implements GeneratorInterface |
||
8 | { |
||
9 | /** |
||
10 | * {@parentDoc}. |
||
11 | */ |
||
12 | public static function toString(Translations $translations, array $options = []) |
||
13 | { |
||
14 | $pluralForm = $translations->getPluralForms(); |
||
15 | $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
||
16 | $lines = ['msgid ""', 'msgstr ""']; |
||
17 | |||
18 | foreach ($translations->getHeaders() as $name => $value) { |
||
19 | $lines[] = sprintf('"%s: %s\\n"', $name, $value); |
||
20 | } |
||
21 | |||
22 | $lines[] = ''; |
||
23 | |||
24 | //Translations |
||
25 | foreach ($translations as $translation) { |
||
26 | if ($translation->hasComments()) { |
||
27 | foreach ($translation->getComments() as $comment) { |
||
28 | $lines[] = '# '.$comment; |
||
29 | } |
||
30 | } |
||
31 | |||
32 | if ($translation->hasExtractedComments()) { |
||
33 | foreach ($translation->getExtractedComments() as $comment) { |
||
34 | $lines[] = '#. '.$comment; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | if ($translation->hasReferences()) { |
||
39 | foreach ($translation->getReferences() as $reference) { |
||
40 | $lines[] = '#: '.$reference[0].(!is_null($reference[1]) ? ':'.$reference[1] : null); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | if ($translation->hasFlags()) { |
||
45 | $lines[] = '#, '.implode(',', $translation->getFlags()); |
||
46 | } |
||
47 | |||
48 | if ($translation->hasContext()) { |
||
49 | $lines[] = 'msgctxt '.self::convertString($translation->getContext()); |
||
50 | } |
||
51 | |||
52 | self::addLines($lines, 'msgid', $translation->getOriginal()); |
||
53 | |||
54 | if ($translation->hasPlural()) { |
||
55 | self::addLines($lines, 'msgid_plural', $translation->getPlural()); |
||
56 | self::addLines($lines, 'msgstr[0]', $translation->getTranslation()); |
||
57 | |||
58 | foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { |
||
59 | self::addLines($lines, 'msgstr['.($k + 1).']', $v); |
||
60 | } |
||
61 | } else { |
||
62 | self::addLines($lines, 'msgstr', $translation->getTranslation()); |
||
63 | } |
||
64 | |||
65 | $lines[] = ''; |
||
66 | } |
||
67 | |||
68 | return implode("\n", $lines); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Escapes and adds double quotes to a string. |
||
73 | * |
||
74 | * @param string $string |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | private static function multilineQuote($string) |
||
93 | |||
94 | /** |
||
95 | * Add one or more lines depending whether the string is multiline or not. |
||
96 | * |
||
97 | * @param array &$lines |
||
98 | * @param string $name |
||
99 | * @param string $value |
||
100 | */ |
||
101 | private static function addLines(array &$lines, $name, $value) |
||
115 | |||
116 | /** |
||
117 | * Convert a string to its PO representation. |
||
118 | * |
||
119 | * @param string $value |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public static function convertString($value) |
||
136 | } |
||
137 |