@@ -7,106 +7,106 @@ |
||
7 | 7 | */ |
8 | 8 | interface TranslatorInterface |
9 | 9 | { |
10 | - /** |
|
11 | - * Register this translator as global, to use with the gettext functions __(), p__(), etc. |
|
12 | - * Returns the previous translator if exists. |
|
13 | - * |
|
14 | - * @return TranslatorInterface|null |
|
15 | - */ |
|
16 | - public function register(); |
|
10 | + /** |
|
11 | + * Register this translator as global, to use with the gettext functions __(), p__(), etc. |
|
12 | + * Returns the previous translator if exists. |
|
13 | + * |
|
14 | + * @return TranslatorInterface|null |
|
15 | + */ |
|
16 | + public function register(); |
|
17 | 17 | |
18 | - /** |
|
19 | - * Noop, marks the string for translation but returns it unchanged. |
|
20 | - * |
|
21 | - * @param string $original |
|
22 | - * |
|
23 | - * @return string |
|
24 | - */ |
|
25 | - public function noop($original); |
|
18 | + /** |
|
19 | + * Noop, marks the string for translation but returns it unchanged. |
|
20 | + * |
|
21 | + * @param string $original |
|
22 | + * |
|
23 | + * @return string |
|
24 | + */ |
|
25 | + public function noop($original); |
|
26 | 26 | |
27 | - /** |
|
28 | - * Gets a translation using the original string. |
|
29 | - * |
|
30 | - * @param string $original |
|
31 | - * |
|
32 | - * @return string |
|
33 | - */ |
|
34 | - public function gettext($original); |
|
27 | + /** |
|
28 | + * Gets a translation using the original string. |
|
29 | + * |
|
30 | + * @param string $original |
|
31 | + * |
|
32 | + * @return string |
|
33 | + */ |
|
34 | + public function gettext($original); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Gets a translation checking the plural form. |
|
38 | - * |
|
39 | - * @param string $original |
|
40 | - * @param string $plural |
|
41 | - * @param string $value |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function ngettext($original, $plural, $value); |
|
36 | + /** |
|
37 | + * Gets a translation checking the plural form. |
|
38 | + * |
|
39 | + * @param string $original |
|
40 | + * @param string $plural |
|
41 | + * @param string $value |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function ngettext($original, $plural, $value); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Gets a translation checking the domain and the plural form. |
|
49 | - * |
|
50 | - * @param string $domain |
|
51 | - * @param string $original |
|
52 | - * @param string $plural |
|
53 | - * @param string $value |
|
54 | - * |
|
55 | - * @return string |
|
56 | - */ |
|
57 | - public function dngettext($domain, $original, $plural, $value); |
|
47 | + /** |
|
48 | + * Gets a translation checking the domain and the plural form. |
|
49 | + * |
|
50 | + * @param string $domain |
|
51 | + * @param string $original |
|
52 | + * @param string $plural |
|
53 | + * @param string $value |
|
54 | + * |
|
55 | + * @return string |
|
56 | + */ |
|
57 | + public function dngettext($domain, $original, $plural, $value); |
|
58 | 58 | |
59 | - /** |
|
60 | - * Gets a translation checking the context and the plural form. |
|
61 | - * |
|
62 | - * @param string $context |
|
63 | - * @param string $original |
|
64 | - * @param string $plural |
|
65 | - * @param string $value |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function npgettext($context, $original, $plural, $value); |
|
59 | + /** |
|
60 | + * Gets a translation checking the context and the plural form. |
|
61 | + * |
|
62 | + * @param string $context |
|
63 | + * @param string $original |
|
64 | + * @param string $plural |
|
65 | + * @param string $value |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function npgettext($context, $original, $plural, $value); |
|
70 | 70 | |
71 | - /** |
|
72 | - * Gets a translation checking the context. |
|
73 | - * |
|
74 | - * @param string $context |
|
75 | - * @param string $original |
|
76 | - * |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public function pgettext($context, $original); |
|
71 | + /** |
|
72 | + * Gets a translation checking the context. |
|
73 | + * |
|
74 | + * @param string $context |
|
75 | + * @param string $original |
|
76 | + * |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public function pgettext($context, $original); |
|
80 | 80 | |
81 | - /** |
|
82 | - * Gets a translation checking the domain. |
|
83 | - * |
|
84 | - * @param string $domain |
|
85 | - * @param string $original |
|
86 | - * |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - public function dgettext($domain, $original); |
|
81 | + /** |
|
82 | + * Gets a translation checking the domain. |
|
83 | + * |
|
84 | + * @param string $domain |
|
85 | + * @param string $original |
|
86 | + * |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + public function dgettext($domain, $original); |
|
90 | 90 | |
91 | - /** |
|
92 | - * Gets a translation checking the domain and context. |
|
93 | - * |
|
94 | - * @param string $domain |
|
95 | - * @param string $context |
|
96 | - * @param string $original |
|
97 | - * |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - public function dpgettext($domain, $context, $original); |
|
91 | + /** |
|
92 | + * Gets a translation checking the domain and context. |
|
93 | + * |
|
94 | + * @param string $domain |
|
95 | + * @param string $context |
|
96 | + * @param string $original |
|
97 | + * |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + public function dpgettext($domain, $context, $original); |
|
101 | 101 | |
102 | - /** |
|
103 | - * Gets a translation checking the domain, the context and the plural form. |
|
104 | - * |
|
105 | - * @param string $domain |
|
106 | - * @param string $context |
|
107 | - * @param string $original |
|
108 | - * @param string $plural |
|
109 | - * @param string $value |
|
110 | - */ |
|
111 | - public function dnpgettext($domain, $context, $original, $plural, $value); |
|
102 | + /** |
|
103 | + * Gets a translation checking the domain, the context and the plural form. |
|
104 | + * |
|
105 | + * @param string $domain |
|
106 | + * @param string $context |
|
107 | + * @param string $original |
|
108 | + * @param string $plural |
|
109 | + * @param string $value |
|
110 | + */ |
|
111 | + public function dnpgettext($domain, $context, $original, $plural, $value); |
|
112 | 112 | } |
@@ -11,15 +11,15 @@ discard block |
||
11 | 11 | */ |
12 | 12 | function __($original) |
13 | 13 | { |
14 | - $text = BaseTranslator::$current->gettext($original); |
|
14 | + $text = BaseTranslator::$current->gettext($original); |
|
15 | 15 | |
16 | - if (func_num_args() === 1) { |
|
17 | - return $text; |
|
18 | - } |
|
16 | + if (func_num_args() === 1) { |
|
17 | + return $text; |
|
18 | + } |
|
19 | 19 | |
20 | - $args = array_slice(func_get_args(), 1); |
|
20 | + $args = array_slice(func_get_args(), 1); |
|
21 | 21 | |
22 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
22 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | */ |
32 | 32 | function noop__($original) |
33 | 33 | { |
34 | - return $original; |
|
34 | + return $original; |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | /** |
@@ -45,15 +45,15 @@ discard block |
||
45 | 45 | */ |
46 | 46 | function n__($original, $plural, $value) |
47 | 47 | { |
48 | - $text = BaseTranslator::$current->ngettext($original, $plural, $value); |
|
48 | + $text = BaseTranslator::$current->ngettext($original, $plural, $value); |
|
49 | 49 | |
50 | - if (func_num_args() === 3) { |
|
51 | - return $text; |
|
52 | - } |
|
50 | + if (func_num_args() === 3) { |
|
51 | + return $text; |
|
52 | + } |
|
53 | 53 | |
54 | - $args = array_slice(func_get_args(), 3); |
|
54 | + $args = array_slice(func_get_args(), 3); |
|
55 | 55 | |
56 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
56 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | /** |
@@ -66,15 +66,15 @@ discard block |
||
66 | 66 | */ |
67 | 67 | function p__($context, $original) |
68 | 68 | { |
69 | - $text = BaseTranslator::$current->pgettext($context, $original); |
|
69 | + $text = BaseTranslator::$current->pgettext($context, $original); |
|
70 | 70 | |
71 | - if (func_num_args() === 2) { |
|
72 | - return $text; |
|
73 | - } |
|
71 | + if (func_num_args() === 2) { |
|
72 | + return $text; |
|
73 | + } |
|
74 | 74 | |
75 | - $args = array_slice(func_get_args(), 2); |
|
75 | + $args = array_slice(func_get_args(), 2); |
|
76 | 76 | |
77 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
77 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
@@ -87,15 +87,15 @@ discard block |
||
87 | 87 | */ |
88 | 88 | function d__($domain, $original) |
89 | 89 | { |
90 | - $text = BaseTranslator::$current->dgettext($domain, $original); |
|
90 | + $text = BaseTranslator::$current->dgettext($domain, $original); |
|
91 | 91 | |
92 | - if (func_num_args() === 2) { |
|
93 | - return $text; |
|
94 | - } |
|
92 | + if (func_num_args() === 2) { |
|
93 | + return $text; |
|
94 | + } |
|
95 | 95 | |
96 | - $args = array_slice(func_get_args(), 2); |
|
96 | + $args = array_slice(func_get_args(), 2); |
|
97 | 97 | |
98 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
98 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -109,15 +109,15 @@ discard block |
||
109 | 109 | */ |
110 | 110 | function dp__($domain, $context, $original) |
111 | 111 | { |
112 | - $text = BaseTranslator::$current->dpgettext($domain, $context, $original); |
|
112 | + $text = BaseTranslator::$current->dpgettext($domain, $context, $original); |
|
113 | 113 | |
114 | - if (func_num_args() === 3) { |
|
115 | - return $text; |
|
116 | - } |
|
114 | + if (func_num_args() === 3) { |
|
115 | + return $text; |
|
116 | + } |
|
117 | 117 | |
118 | - $args = array_slice(func_get_args(), 3); |
|
118 | + $args = array_slice(func_get_args(), 3); |
|
119 | 119 | |
120 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
120 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -132,15 +132,15 @@ discard block |
||
132 | 132 | */ |
133 | 133 | function dn__($domain, $original, $plural, $value) |
134 | 134 | { |
135 | - $text = BaseTranslator::$current->dngettext($domain, $original, $plural, $value); |
|
135 | + $text = BaseTranslator::$current->dngettext($domain, $original, $plural, $value); |
|
136 | 136 | |
137 | - if (func_num_args() === 4) { |
|
138 | - return $text; |
|
139 | - } |
|
137 | + if (func_num_args() === 4) { |
|
138 | + return $text; |
|
139 | + } |
|
140 | 140 | |
141 | - $args = array_slice(func_get_args(), 4); |
|
141 | + $args = array_slice(func_get_args(), 4); |
|
142 | 142 | |
143 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
143 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
@@ -155,15 +155,15 @@ discard block |
||
155 | 155 | */ |
156 | 156 | function np__($context, $original, $plural, $value) |
157 | 157 | { |
158 | - $text = BaseTranslator::$current->npgettext($context, $original, $plural, $value); |
|
158 | + $text = BaseTranslator::$current->npgettext($context, $original, $plural, $value); |
|
159 | 159 | |
160 | - if (func_num_args() === 4) { |
|
161 | - return $text; |
|
162 | - } |
|
160 | + if (func_num_args() === 4) { |
|
161 | + return $text; |
|
162 | + } |
|
163 | 163 | |
164 | - $args = array_slice(func_get_args(), 4); |
|
164 | + $args = array_slice(func_get_args(), 4); |
|
165 | 165 | |
166 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
166 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -179,13 +179,13 @@ discard block |
||
179 | 179 | */ |
180 | 180 | function dnp__($domain, $context, $original, $plural, $value) |
181 | 181 | { |
182 | - $text = BaseTranslator::$current->dnpgettext($domain, $context, $original, $plural, $value); |
|
182 | + $text = BaseTranslator::$current->dnpgettext($domain, $context, $original, $plural, $value); |
|
183 | 183 | |
184 | - if (func_num_args() === 5) { |
|
185 | - return $text; |
|
186 | - } |
|
184 | + if (func_num_args() === 5) { |
|
185 | + return $text; |
|
186 | + } |
|
187 | 187 | |
188 | - $args = array_slice(func_get_args(), 5); |
|
188 | + $args = array_slice(func_get_args(), 5); |
|
189 | 189 | |
190 | - return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
190 | + return is_array($args[0]) ? strtr($text, $args[0]) : vsprintf($text, $args); |
|
191 | 191 | } |
@@ -7,34 +7,34 @@ |
||
7 | 7 | |
8 | 8 | class PhpArray extends Generator implements GeneratorInterface |
9 | 9 | { |
10 | - use MultidimensionalArrayTrait; |
|
11 | - |
|
12 | - public static $options = [ |
|
13 | - 'includeHeaders' => true, |
|
14 | - ]; |
|
15 | - |
|
16 | - /** |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public static function toString(Translations $translations, array $options = []) |
|
20 | - { |
|
21 | - $array = static::generate($translations, $options); |
|
22 | - |
|
23 | - return '<?php return '.var_export($array, true).';'; |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Generates an array with the translations. |
|
28 | - * |
|
29 | - * @param Translations $translations |
|
30 | - * @param array $options |
|
31 | - * |
|
32 | - * @return array |
|
33 | - */ |
|
34 | - public static function generate(Translations $translations, array $options = []) |
|
35 | - { |
|
36 | - $options += static::$options; |
|
37 | - |
|
38 | - return static::toArray($translations, $options['includeHeaders'], true); |
|
39 | - } |
|
10 | + use MultidimensionalArrayTrait; |
|
11 | + |
|
12 | + public static $options = [ |
|
13 | + 'includeHeaders' => true, |
|
14 | + ]; |
|
15 | + |
|
16 | + /** |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public static function toString(Translations $translations, array $options = []) |
|
20 | + { |
|
21 | + $array = static::generate($translations, $options); |
|
22 | + |
|
23 | + return '<?php return '.var_export($array, true).';'; |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Generates an array with the translations. |
|
28 | + * |
|
29 | + * @param Translations $translations |
|
30 | + * @param array $options |
|
31 | + * |
|
32 | + * @return array |
|
33 | + */ |
|
34 | + public static function generate(Translations $translations, array $options = []) |
|
35 | + { |
|
36 | + $options += static::$options; |
|
37 | + |
|
38 | + return static::toArray($translations, $options['includeHeaders'], true); |
|
39 | + } |
|
40 | 40 | } |
@@ -8,32 +8,32 @@ |
||
8 | 8 | |
9 | 9 | class CsvDictionary extends Generator implements GeneratorInterface |
10 | 10 | { |
11 | - use DictionaryTrait; |
|
12 | - use CsvTrait; |
|
13 | - |
|
14 | - public static $options = [ |
|
15 | - 'includeHeaders' => false, |
|
16 | - 'delimiter' => ",", |
|
17 | - 'enclosure' => '"', |
|
18 | - 'escape_char' => "\\" |
|
19 | - ]; |
|
20 | - |
|
21 | - /** |
|
22 | - * {@parentDoc}. |
|
23 | - */ |
|
24 | - public static function toString(Translations $translations, array $options = []) |
|
25 | - { |
|
26 | - $options += static::$options; |
|
27 | - $handle = fopen('php://memory', 'w'); |
|
28 | - |
|
29 | - foreach (static::toArray($translations, $options['includeHeaders']) as $original => $translation) { |
|
30 | - static::fputcsv($handle, [$original, $translation], $options); |
|
31 | - } |
|
32 | - |
|
33 | - rewind($handle); |
|
34 | - $csv = stream_get_contents($handle); |
|
35 | - fclose($handle); |
|
36 | - |
|
37 | - return $csv; |
|
38 | - } |
|
11 | + use DictionaryTrait; |
|
12 | + use CsvTrait; |
|
13 | + |
|
14 | + public static $options = [ |
|
15 | + 'includeHeaders' => false, |
|
16 | + 'delimiter' => ",", |
|
17 | + 'enclosure' => '"', |
|
18 | + 'escape_char' => "\\" |
|
19 | + ]; |
|
20 | + |
|
21 | + /** |
|
22 | + * {@parentDoc}. |
|
23 | + */ |
|
24 | + public static function toString(Translations $translations, array $options = []) |
|
25 | + { |
|
26 | + $options += static::$options; |
|
27 | + $handle = fopen('php://memory', 'w'); |
|
28 | + |
|
29 | + foreach (static::toArray($translations, $options['includeHeaders']) as $original => $translation) { |
|
30 | + static::fputcsv($handle, [$original, $translation], $options); |
|
31 | + } |
|
32 | + |
|
33 | + rewind($handle); |
|
34 | + $csv = stream_get_contents($handle); |
|
35 | + fclose($handle); |
|
36 | + |
|
37 | + return $csv; |
|
38 | + } |
|
39 | 39 | } |
@@ -8,115 +8,115 @@ |
||
8 | 8 | |
9 | 9 | class Xliff extends Generator implements GeneratorInterface |
10 | 10 | { |
11 | - const UNIT_ID_REGEXP = '/^XLIFF_UNIT_ID: (.*)$/'; |
|
12 | - |
|
13 | - /** |
|
14 | - * {@inheritdoc} |
|
15 | - */ |
|
16 | - public static function toString(Translations $translations, array $options = []) |
|
17 | - { |
|
18 | - $dom = new DOMDocument('1.0', 'utf-8'); |
|
19 | - $dom->formatOutput = true; |
|
20 | - $xliff = $dom->appendChild($dom->createElement('xliff')); |
|
21 | - $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); |
|
22 | - $xliff->setAttribute('version', '2.0'); |
|
23 | - $xliff->setAttribute('srcLang', $translations->getLanguage()); |
|
24 | - $xliff->setAttribute('trgLang', $translations->getLanguage()); |
|
25 | - $file = $xliff->appendChild($dom->createElement('file')); |
|
26 | - $file->setAttribute('id', $translations->getDomain().'.'.$translations->getLanguage()); |
|
27 | - |
|
28 | - //Save headers as notes |
|
29 | - $notes = $dom->createElement('notes'); |
|
30 | - |
|
31 | - foreach ($translations->getHeaders() as $name => $value) { |
|
32 | - $notes->appendChild(static::createTextNode($dom, 'note', $value))->setAttribute('id', $name); |
|
33 | - } |
|
34 | - |
|
35 | - if ($notes->hasChildNodes()) { |
|
36 | - $file->appendChild($notes); |
|
37 | - } |
|
38 | - |
|
39 | - foreach ($translations as $translation) { |
|
40 | - //Find an XLIFF unit ID, if one is available; otherwise generate |
|
41 | - $unitId = static::getUnitID($translation)?:md5($translation->getContext().$translation->getOriginal()); |
|
42 | - |
|
43 | - $unit = $dom->createElement('unit'); |
|
44 | - $unit->setAttribute('id', $unitId); |
|
45 | - |
|
46 | - //Save comments as notes |
|
47 | - $notes = $dom->createElement('notes'); |
|
48 | - |
|
49 | - $notes->appendChild(static::createTextNode($dom, 'note', $translation->getContext())) |
|
50 | - ->setAttribute('category', 'context'); |
|
51 | - |
|
52 | - foreach ($translation->getComments() as $comment) { |
|
53 | - //Skip XLIFF unit ID comments. |
|
54 | - if (preg_match(static::UNIT_ID_REGEXP, $comment)) { |
|
55 | - continue; |
|
56 | - } |
|
57 | - |
|
58 | - $notes->appendChild(static::createTextNode($dom, 'note', $comment)) |
|
59 | - ->setAttribute('category', 'comment'); |
|
60 | - } |
|
61 | - |
|
62 | - foreach ($translation->getExtractedComments() as $comment) { |
|
63 | - $notes->appendChild(static::createTextNode($dom, 'note', $comment)) |
|
64 | - ->setAttribute('category', 'extracted-comment'); |
|
65 | - } |
|
66 | - |
|
67 | - foreach ($translation->getFlags() as $flag) { |
|
68 | - $notes->appendChild(static::createTextNode($dom, 'note', $flag)) |
|
69 | - ->setAttribute('category', 'flag'); |
|
70 | - } |
|
71 | - |
|
72 | - foreach ($translation->getReferences() as $reference) { |
|
73 | - $notes->appendChild(static::createTextNode($dom, 'note', $reference[0].':'.$reference[1])) |
|
74 | - ->setAttribute('category', 'reference'); |
|
75 | - } |
|
76 | - |
|
77 | - $unit->appendChild($notes); |
|
78 | - |
|
79 | - $segment = $unit->appendChild($dom->createElement('segment')); |
|
80 | - $segment->appendChild(static::createTextNode($dom, 'source', $translation->getOriginal())); |
|
81 | - $segment->appendChild(static::createTextNode($dom, 'target', $translation->getTranslation())); |
|
82 | - |
|
83 | - foreach ($translation->getPluralTranslations() as $plural) { |
|
84 | - if ($plural !== '') { |
|
85 | - $segment->appendChild(static::createTextNode($dom, 'target', $plural)); |
|
86 | - } |
|
87 | - } |
|
88 | - |
|
89 | - $file->appendChild($unit); |
|
90 | - } |
|
91 | - |
|
92 | - return $dom->saveXML(); |
|
93 | - } |
|
94 | - |
|
95 | - protected static function createTextNode(DOMDocument $dom, $name, $string) |
|
96 | - { |
|
97 | - $node = $dom->createElement($name); |
|
98 | - $text = (preg_match('/[&<>]/', $string) === 1) |
|
99 | - ? $dom->createCDATASection($string) |
|
100 | - : $dom->createTextNode($string); |
|
101 | - $node->appendChild($text); |
|
102 | - |
|
103 | - return $node; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Gets the translation's unit ID, if one is available. |
|
108 | - * |
|
109 | - * @param Translation $translation |
|
110 | - * |
|
111 | - * @return string|null |
|
112 | - */ |
|
113 | - public static function getUnitID(Translation $translation) |
|
114 | - { |
|
115 | - foreach ($translation->getComments() as $comment) { |
|
116 | - if (preg_match(static::UNIT_ID_REGEXP, $comment, $matches)) { |
|
117 | - return $matches[1]; |
|
118 | - } |
|
119 | - } |
|
120 | - return null; |
|
121 | - } |
|
11 | + const UNIT_ID_REGEXP = '/^XLIFF_UNIT_ID: (.*)$/'; |
|
12 | + |
|
13 | + /** |
|
14 | + * {@inheritdoc} |
|
15 | + */ |
|
16 | + public static function toString(Translations $translations, array $options = []) |
|
17 | + { |
|
18 | + $dom = new DOMDocument('1.0', 'utf-8'); |
|
19 | + $dom->formatOutput = true; |
|
20 | + $xliff = $dom->appendChild($dom->createElement('xliff')); |
|
21 | + $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:2.0'); |
|
22 | + $xliff->setAttribute('version', '2.0'); |
|
23 | + $xliff->setAttribute('srcLang', $translations->getLanguage()); |
|
24 | + $xliff->setAttribute('trgLang', $translations->getLanguage()); |
|
25 | + $file = $xliff->appendChild($dom->createElement('file')); |
|
26 | + $file->setAttribute('id', $translations->getDomain().'.'.$translations->getLanguage()); |
|
27 | + |
|
28 | + //Save headers as notes |
|
29 | + $notes = $dom->createElement('notes'); |
|
30 | + |
|
31 | + foreach ($translations->getHeaders() as $name => $value) { |
|
32 | + $notes->appendChild(static::createTextNode($dom, 'note', $value))->setAttribute('id', $name); |
|
33 | + } |
|
34 | + |
|
35 | + if ($notes->hasChildNodes()) { |
|
36 | + $file->appendChild($notes); |
|
37 | + } |
|
38 | + |
|
39 | + foreach ($translations as $translation) { |
|
40 | + //Find an XLIFF unit ID, if one is available; otherwise generate |
|
41 | + $unitId = static::getUnitID($translation)?:md5($translation->getContext().$translation->getOriginal()); |
|
42 | + |
|
43 | + $unit = $dom->createElement('unit'); |
|
44 | + $unit->setAttribute('id', $unitId); |
|
45 | + |
|
46 | + //Save comments as notes |
|
47 | + $notes = $dom->createElement('notes'); |
|
48 | + |
|
49 | + $notes->appendChild(static::createTextNode($dom, 'note', $translation->getContext())) |
|
50 | + ->setAttribute('category', 'context'); |
|
51 | + |
|
52 | + foreach ($translation->getComments() as $comment) { |
|
53 | + //Skip XLIFF unit ID comments. |
|
54 | + if (preg_match(static::UNIT_ID_REGEXP, $comment)) { |
|
55 | + continue; |
|
56 | + } |
|
57 | + |
|
58 | + $notes->appendChild(static::createTextNode($dom, 'note', $comment)) |
|
59 | + ->setAttribute('category', 'comment'); |
|
60 | + } |
|
61 | + |
|
62 | + foreach ($translation->getExtractedComments() as $comment) { |
|
63 | + $notes->appendChild(static::createTextNode($dom, 'note', $comment)) |
|
64 | + ->setAttribute('category', 'extracted-comment'); |
|
65 | + } |
|
66 | + |
|
67 | + foreach ($translation->getFlags() as $flag) { |
|
68 | + $notes->appendChild(static::createTextNode($dom, 'note', $flag)) |
|
69 | + ->setAttribute('category', 'flag'); |
|
70 | + } |
|
71 | + |
|
72 | + foreach ($translation->getReferences() as $reference) { |
|
73 | + $notes->appendChild(static::createTextNode($dom, 'note', $reference[0].':'.$reference[1])) |
|
74 | + ->setAttribute('category', 'reference'); |
|
75 | + } |
|
76 | + |
|
77 | + $unit->appendChild($notes); |
|
78 | + |
|
79 | + $segment = $unit->appendChild($dom->createElement('segment')); |
|
80 | + $segment->appendChild(static::createTextNode($dom, 'source', $translation->getOriginal())); |
|
81 | + $segment->appendChild(static::createTextNode($dom, 'target', $translation->getTranslation())); |
|
82 | + |
|
83 | + foreach ($translation->getPluralTranslations() as $plural) { |
|
84 | + if ($plural !== '') { |
|
85 | + $segment->appendChild(static::createTextNode($dom, 'target', $plural)); |
|
86 | + } |
|
87 | + } |
|
88 | + |
|
89 | + $file->appendChild($unit); |
|
90 | + } |
|
91 | + |
|
92 | + return $dom->saveXML(); |
|
93 | + } |
|
94 | + |
|
95 | + protected static function createTextNode(DOMDocument $dom, $name, $string) |
|
96 | + { |
|
97 | + $node = $dom->createElement($name); |
|
98 | + $text = (preg_match('/[&<>]/', $string) === 1) |
|
99 | + ? $dom->createCDATASection($string) |
|
100 | + : $dom->createTextNode($string); |
|
101 | + $node->appendChild($text); |
|
102 | + |
|
103 | + return $node; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Gets the translation's unit ID, if one is available. |
|
108 | + * |
|
109 | + * @param Translation $translation |
|
110 | + * |
|
111 | + * @return string|null |
|
112 | + */ |
|
113 | + public static function getUnitID(Translation $translation) |
|
114 | + { |
|
115 | + foreach ($translation->getComments() as $comment) { |
|
116 | + if (preg_match(static::UNIT_ID_REGEXP, $comment, $matches)) { |
|
117 | + return $matches[1]; |
|
118 | + } |
|
119 | + } |
|
120 | + return null; |
|
121 | + } |
|
122 | 122 | } |
@@ -6,24 +6,24 @@ |
||
6 | 6 | |
7 | 7 | interface GeneratorInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * Saves the translations in a file. |
|
11 | - * |
|
12 | - * @param Translations $translations |
|
13 | - * @param string $file |
|
14 | - * @param array $options |
|
15 | - * |
|
16 | - * @return bool |
|
17 | - */ |
|
18 | - public static function toFile(Translations $translations, $file, array $options = []); |
|
9 | + /** |
|
10 | + * Saves the translations in a file. |
|
11 | + * |
|
12 | + * @param Translations $translations |
|
13 | + * @param string $file |
|
14 | + * @param array $options |
|
15 | + * |
|
16 | + * @return bool |
|
17 | + */ |
|
18 | + public static function toFile(Translations $translations, $file, array $options = []); |
|
19 | 19 | |
20 | - /** |
|
21 | - * Generates a string with the translations ready to save in a file. |
|
22 | - * |
|
23 | - * @param Translations $translations |
|
24 | - * @param array $options |
|
25 | - * |
|
26 | - * @return string |
|
27 | - */ |
|
28 | - public static function toString(Translations $translations, array $options = []); |
|
20 | + /** |
|
21 | + * Generates a string with the translations ready to save in a file. |
|
22 | + * |
|
23 | + * @param Translations $translations |
|
24 | + * @param array $options |
|
25 | + * |
|
26 | + * @return string |
|
27 | + */ |
|
28 | + public static function toString(Translations $translations, array $options = []); |
|
29 | 29 | } |
@@ -8,25 +8,25 @@ |
||
8 | 8 | |
9 | 9 | class YamlDictionary extends Generator implements GeneratorInterface |
10 | 10 | { |
11 | - use DictionaryTrait; |
|
11 | + use DictionaryTrait; |
|
12 | 12 | |
13 | - public static $options = [ |
|
14 | - 'includeHeaders' => false, |
|
15 | - 'indent' => 2, |
|
16 | - 'inline' => 3, |
|
17 | - ]; |
|
13 | + public static $options = [ |
|
14 | + 'includeHeaders' => false, |
|
15 | + 'indent' => 2, |
|
16 | + 'inline' => 3, |
|
17 | + ]; |
|
18 | 18 | |
19 | - /** |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - public static function toString(Translations $translations, array $options = []) |
|
23 | - { |
|
24 | - $options += static::$options; |
|
19 | + /** |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + public static function toString(Translations $translations, array $options = []) |
|
23 | + { |
|
24 | + $options += static::$options; |
|
25 | 25 | |
26 | - return YamlDumper::dump( |
|
27 | - static::toArray($translations, $options['includeHeaders']), |
|
28 | - $options['inline'], |
|
29 | - $options['indent'] |
|
30 | - ); |
|
31 | - } |
|
26 | + return YamlDumper::dump( |
|
27 | + static::toArray($translations, $options['includeHeaders']), |
|
28 | + $options['inline'], |
|
29 | + $options['indent'] |
|
30 | + ); |
|
31 | + } |
|
32 | 32 | } |
@@ -11,46 +11,46 @@ |
||
11 | 11 | */ |
12 | 12 | class Csv extends Generator implements GeneratorInterface |
13 | 13 | { |
14 | - use HeadersGeneratorTrait; |
|
15 | - use CsvTrait; |
|
16 | - |
|
17 | - public static $options = [ |
|
18 | - 'includeHeaders' => false, |
|
19 | - 'delimiter' => ",", |
|
20 | - 'enclosure' => '"', |
|
21 | - 'escape_char' => "\\" |
|
22 | - ]; |
|
23 | - |
|
24 | - /** |
|
25 | - * {@parentDoc}. |
|
26 | - */ |
|
27 | - public static function toString(Translations $translations, array $options = []) |
|
28 | - { |
|
29 | - $options += static::$options; |
|
30 | - $handle = fopen('php://memory', 'w'); |
|
31 | - |
|
32 | - if ($options['includeHeaders']) { |
|
33 | - static::fputcsv($handle, ['', '', static::generateHeaders($translations)], $options); |
|
34 | - } |
|
35 | - |
|
36 | - foreach ($translations as $translation) { |
|
37 | - if ($translation->isDisabled()) { |
|
38 | - continue; |
|
39 | - } |
|
40 | - |
|
41 | - $line = [$translation->getContext(), $translation->getOriginal(), $translation->getTranslation()]; |
|
42 | - |
|
43 | - if ($translation->hasPluralTranslations(true)) { |
|
44 | - $line = array_merge($line, $translation->getPluralTranslations()); |
|
45 | - } |
|
46 | - |
|
47 | - static::fputcsv($handle, $line, $options); |
|
48 | - } |
|
49 | - |
|
50 | - rewind($handle); |
|
51 | - $csv = stream_get_contents($handle); |
|
52 | - fclose($handle); |
|
53 | - |
|
54 | - return $csv; |
|
55 | - } |
|
14 | + use HeadersGeneratorTrait; |
|
15 | + use CsvTrait; |
|
16 | + |
|
17 | + public static $options = [ |
|
18 | + 'includeHeaders' => false, |
|
19 | + 'delimiter' => ",", |
|
20 | + 'enclosure' => '"', |
|
21 | + 'escape_char' => "\\" |
|
22 | + ]; |
|
23 | + |
|
24 | + /** |
|
25 | + * {@parentDoc}. |
|
26 | + */ |
|
27 | + public static function toString(Translations $translations, array $options = []) |
|
28 | + { |
|
29 | + $options += static::$options; |
|
30 | + $handle = fopen('php://memory', 'w'); |
|
31 | + |
|
32 | + if ($options['includeHeaders']) { |
|
33 | + static::fputcsv($handle, ['', '', static::generateHeaders($translations)], $options); |
|
34 | + } |
|
35 | + |
|
36 | + foreach ($translations as $translation) { |
|
37 | + if ($translation->isDisabled()) { |
|
38 | + continue; |
|
39 | + } |
|
40 | + |
|
41 | + $line = [$translation->getContext(), $translation->getOriginal(), $translation->getTranslation()]; |
|
42 | + |
|
43 | + if ($translation->hasPluralTranslations(true)) { |
|
44 | + $line = array_merge($line, $translation->getPluralTranslations()); |
|
45 | + } |
|
46 | + |
|
47 | + static::fputcsv($handle, $line, $options); |
|
48 | + } |
|
49 | + |
|
50 | + rewind($handle); |
|
51 | + $csv = stream_get_contents($handle); |
|
52 | + fclose($handle); |
|
53 | + |
|
54 | + return $csv; |
|
55 | + } |
|
56 | 56 | } |
@@ -7,20 +7,20 @@ |
||
7 | 7 | |
8 | 8 | class JsonDictionary extends Generator implements GeneratorInterface |
9 | 9 | { |
10 | - use DictionaryTrait; |
|
10 | + use DictionaryTrait; |
|
11 | 11 | |
12 | - public static $options = [ |
|
13 | - 'json' => 0, |
|
14 | - 'includeHeaders' => false, |
|
15 | - ]; |
|
12 | + public static $options = [ |
|
13 | + 'json' => 0, |
|
14 | + 'includeHeaders' => false, |
|
15 | + ]; |
|
16 | 16 | |
17 | - /** |
|
18 | - * {@parentDoc}. |
|
19 | - */ |
|
20 | - public static function toString(Translations $translations, array $options = []) |
|
21 | - { |
|
22 | - $options += static::$options; |
|
17 | + /** |
|
18 | + * {@parentDoc}. |
|
19 | + */ |
|
20 | + public static function toString(Translations $translations, array $options = []) |
|
21 | + { |
|
22 | + $options += static::$options; |
|
23 | 23 | |
24 | - return json_encode(static::toArray($translations, $options['includeHeaders']), $options['json']); |
|
25 | - } |
|
24 | + return json_encode(static::toArray($translations, $options['includeHeaders']), $options['json']); |
|
25 | + } |
|
26 | 26 | } |