@@ -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 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | * |
16 | 16 | * @return bool |
17 | 17 | */ |
18 | - public static function toFile(Translations $translations, $file, array $options = []); |
|
18 | + public static function toFile( Translations $translations, $file, array $options = [ ] ); |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Generates a string with the translations ready to save in a file. |
@@ -25,5 +25,5 @@ discard block |
||
25 | 25 | * |
26 | 26 | * @return string |
27 | 27 | */ |
28 | - public static function toString(Translations $translations, array $options = []); |
|
28 | + public static function toString( Translations $translations, array $options = [ ] ); |
|
29 | 29 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use Gettext\Translations; |
6 | 6 | |
7 | -interface GeneratorInterface |
|
8 | -{ |
|
7 | +interface GeneratorInterface { |
|
9 | 8 | /** |
10 | 9 | * Saves the translations in a file. |
11 | 10 | * |
@@ -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 | } |
@@ -19,14 +19,14 @@ |
||
19 | 19 | /** |
20 | 20 | * {@inheritdoc} |
21 | 21 | */ |
22 | - public static function toString(Translations $translations, array $options = []) |
|
22 | + public static function toString( Translations $translations, array $options = [ ] ) |
|
23 | 23 | { |
24 | 24 | $options += static::$options; |
25 | 25 | |
26 | 26 | return YamlDumper::dump( |
27 | - static::toArray($translations, $options['includeHeaders']), |
|
28 | - $options['inline'], |
|
29 | - $options['indent'] |
|
27 | + static::toArray( $translations, $options[ 'includeHeaders' ] ), |
|
28 | + $options[ 'inline' ], |
|
29 | + $options[ 'indent' ] |
|
30 | 30 | ); |
31 | 31 | } |
32 | 32 | } |
@@ -6,8 +6,7 @@ discard block |
||
6 | 6 | use Gettext\Utils\DictionaryTrait; |
7 | 7 | use Symfony\Component\Yaml\Yaml as YamlDumper; |
8 | 8 | |
9 | -class YamlDictionary extends Generator implements GeneratorInterface |
|
10 | -{ |
|
9 | +class YamlDictionary extends Generator implements GeneratorInterface { |
|
11 | 10 | use DictionaryTrait; |
12 | 11 | |
13 | 12 | public static $options = [ |
@@ -19,8 +18,7 @@ discard block |
||
19 | 18 | /** |
20 | 19 | * {@inheritdoc} |
21 | 20 | */ |
22 | - public static function toString(Translations $translations, array $options = []) |
|
23 | - { |
|
21 | + public static function toString(Translations $translations, array $options = []) { |
|
24 | 22 | $options += static::$options; |
25 | 23 | |
26 | 24 | return YamlDumper::dump( |
@@ -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 | } |
@@ -24,32 +24,32 @@ |
||
24 | 24 | /** |
25 | 25 | * {@parentDoc}. |
26 | 26 | */ |
27 | - public static function toString(Translations $translations, array $options = []) |
|
27 | + public static function toString( Translations $translations, array $options = [ ] ) |
|
28 | 28 | { |
29 | 29 | $options += static::$options; |
30 | - $handle = fopen('php://memory', 'w'); |
|
30 | + $handle = fopen( 'php://memory', 'w' ); |
|
31 | 31 | |
32 | - if ($options['includeHeaders']) { |
|
33 | - static::fputcsv($handle, ['', '', static::generateHeaders($translations)], $options); |
|
32 | + if ( $options[ 'includeHeaders' ] ) { |
|
33 | + static::fputcsv( $handle, [ '', '', static::generateHeaders( $translations ) ], $options ); |
|
34 | 34 | } |
35 | 35 | |
36 | - foreach ($translations as $translation) { |
|
37 | - if ($translation->isDisabled()) { |
|
36 | + foreach ( $translations as $translation ) { |
|
37 | + if ( $translation->isDisabled() ) { |
|
38 | 38 | continue; |
39 | 39 | } |
40 | 40 | |
41 | - $line = [$translation->getContext(), $translation->getOriginal(), $translation->getTranslation()]; |
|
41 | + $line = [ $translation->getContext(), $translation->getOriginal(), $translation->getTranslation() ]; |
|
42 | 42 | |
43 | - if ($translation->hasPluralTranslations(true)) { |
|
44 | - $line = array_merge($line, $translation->getPluralTranslations()); |
|
43 | + if ( $translation->hasPluralTranslations( true ) ) { |
|
44 | + $line = array_merge( $line, $translation->getPluralTranslations() ); |
|
45 | 45 | } |
46 | 46 | |
47 | - static::fputcsv($handle, $line, $options); |
|
47 | + static::fputcsv( $handle, $line, $options ); |
|
48 | 48 | } |
49 | 49 | |
50 | - rewind($handle); |
|
51 | - $csv = stream_get_contents($handle); |
|
52 | - fclose($handle); |
|
50 | + rewind( $handle ); |
|
51 | + $csv = stream_get_contents( $handle ); |
|
52 | + fclose( $handle ); |
|
53 | 53 | |
54 | 54 | return $csv; |
55 | 55 | } |
@@ -9,8 +9,7 @@ discard block |
||
9 | 9 | /** |
10 | 10 | * Class to export translations to csv. |
11 | 11 | */ |
12 | -class Csv extends Generator implements GeneratorInterface |
|
13 | -{ |
|
12 | +class Csv extends Generator implements GeneratorInterface { |
|
14 | 13 | use HeadersGeneratorTrait; |
15 | 14 | use CsvTrait; |
16 | 15 | |
@@ -24,8 +23,7 @@ discard block |
||
24 | 23 | /** |
25 | 24 | * {@parentDoc}. |
26 | 25 | */ |
27 | - public static function toString(Translations $translations, array $options = []) |
|
28 | - { |
|
26 | + public static function toString(Translations $translations, array $options = []) { |
|
29 | 27 | $options += static::$options; |
30 | 28 | $handle = fopen('php://memory', 'w'); |
31 | 29 |
@@ -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 | } |
@@ -17,10 +17,10 @@ |
||
17 | 17 | /** |
18 | 18 | * {@parentDoc}. |
19 | 19 | */ |
20 | - public static function toString(Translations $translations, array $options = []) |
|
20 | + public static function toString( Translations $translations, array $options = [ ] ) |
|
21 | 21 | { |
22 | 22 | $options += static::$options; |
23 | 23 | |
24 | - return json_encode(static::toArray($translations, $options['includeHeaders']), $options['json']); |
|
24 | + return json_encode( static::toArray( $translations, $options[ 'includeHeaders' ] ), $options[ 'json' ] ); |
|
25 | 25 | } |
26 | 26 | } |
@@ -5,8 +5,7 @@ discard block |
||
5 | 5 | use Gettext\Translations; |
6 | 6 | use Gettext\Utils\DictionaryTrait; |
7 | 7 | |
8 | -class JsonDictionary extends Generator implements GeneratorInterface |
|
9 | -{ |
|
8 | +class JsonDictionary extends Generator implements GeneratorInterface { |
|
10 | 9 | use DictionaryTrait; |
11 | 10 | |
12 | 11 | public static $options = [ |
@@ -17,8 +16,7 @@ discard block |
||
17 | 16 | /** |
18 | 17 | * {@parentDoc}. |
19 | 18 | */ |
20 | - public static function toString(Translations $translations, array $options = []) |
|
21 | - { |
|
19 | + public static function toString(Translations $translations, array $options = []) { |
|
22 | 20 | $options += static::$options; |
23 | 21 | |
24 | 22 | return json_encode(static::toArray($translations, $options['includeHeaders']), $options['json']); |
@@ -7,134 +7,134 @@ |
||
7 | 7 | |
8 | 8 | class Mo extends Generator implements GeneratorInterface |
9 | 9 | { |
10 | - use HeadersGeneratorTrait; |
|
11 | - |
|
12 | - public static $options = [ |
|
13 | - 'includeHeaders' => true, |
|
14 | - ]; |
|
15 | - |
|
16 | - /** |
|
17 | - * {@parentDoc}. |
|
18 | - */ |
|
19 | - public static function toString(Translations $translations, array $options = []) |
|
20 | - { |
|
21 | - $options += static::$options; |
|
22 | - $messages = []; |
|
23 | - |
|
24 | - if ($options['includeHeaders']) { |
|
25 | - $messages[''] = static::generateHeaders($translations); |
|
26 | - } |
|
27 | - |
|
28 | - foreach ($translations as $translation) { |
|
29 | - if (!$translation->hasTranslation() || $translation->isDisabled()) { |
|
30 | - continue; |
|
31 | - } |
|
32 | - |
|
33 | - if ($translation->hasContext()) { |
|
34 | - $originalString = $translation->getContext()."\x04".$translation->getOriginal(); |
|
35 | - } else { |
|
36 | - $originalString = $translation->getOriginal(); |
|
37 | - } |
|
38 | - |
|
39 | - $messages[$originalString] = $translation; |
|
40 | - } |
|
41 | - |
|
42 | - ksort($messages); |
|
43 | - $numEntries = count($messages); |
|
44 | - $originalsTable = ''; |
|
45 | - $translationsTable = ''; |
|
46 | - $originalsIndex = []; |
|
47 | - $translationsIndex = []; |
|
48 | - $pluralForm = $translations->getPluralForms(); |
|
49 | - $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
50 | - |
|
51 | - foreach ($messages as $originalString => $translation) { |
|
52 | - if (is_string($translation)) { |
|
53 | - // Headers |
|
54 | - $translationString = $translation; |
|
55 | - } else { |
|
56 | - /* @var $translation \Gettext\Translation */ |
|
57 | - if ($translation->hasPlural() && $translation->hasPluralTranslations(true)) { |
|
58 | - $originalString .= "\x00".$translation->getPlural(); |
|
59 | - $translationString = $translation->getTranslation(); |
|
60 | - $translationString .= "\x00".implode("\x00", $translation->getPluralTranslations($pluralSize)); |
|
61 | - } else { |
|
62 | - $translationString = $translation->getTranslation(); |
|
63 | - } |
|
64 | - } |
|
65 | - |
|
66 | - $originalsIndex[] = [ |
|
67 | - 'relativeOffset' => strlen($originalsTable), |
|
68 | - 'length' => strlen($originalString) |
|
69 | - ]; |
|
70 | - $originalsTable .= $originalString."\x00"; |
|
71 | - $translationsIndex[] = [ |
|
72 | - 'relativeOffset' => strlen($translationsTable), |
|
73 | - 'length' => strlen($translationString) |
|
74 | - ]; |
|
75 | - $translationsTable .= $translationString."\x00"; |
|
76 | - } |
|
77 | - |
|
78 | - // Offset of table with the original strings index: right after the header (which is 7 words) |
|
79 | - $originalsIndexOffset = 7 * 4; |
|
80 | - |
|
81 | - // Size of table with the original strings index |
|
82 | - $originalsIndexSize = $numEntries * (4 + 4); |
|
83 | - |
|
84 | - // Offset of table with the translation strings index: right after the original strings index table |
|
85 | - $translationsIndexOffset = $originalsIndexOffset + $originalsIndexSize; |
|
86 | - |
|
87 | - // Size of table with the translation strings index |
|
88 | - $translationsIndexSize = $numEntries * (4 + 4); |
|
89 | - |
|
90 | - // Hashing table starts after the header and after the index table |
|
91 | - $originalsStringsOffset = $translationsIndexOffset + $translationsIndexSize; |
|
92 | - |
|
93 | - // Translations start after the keys |
|
94 | - $translationsStringsOffset = $originalsStringsOffset + strlen($originalsTable); |
|
95 | - |
|
96 | - // Let's generate the .mo file binary data |
|
97 | - $mo = ''; |
|
98 | - |
|
99 | - // Magic number |
|
100 | - $mo .= pack('L', 0x950412de); |
|
101 | - |
|
102 | - // File format revision |
|
103 | - $mo .= pack('L', 0); |
|
104 | - |
|
105 | - // Number of strings |
|
106 | - $mo .= pack('L', $numEntries); |
|
107 | - |
|
108 | - // Offset of table with original strings |
|
109 | - $mo .= pack('L', $originalsIndexOffset); |
|
110 | - |
|
111 | - // Offset of table with translation strings |
|
112 | - $mo .= pack('L', $translationsIndexOffset); |
|
113 | - |
|
114 | - // Size of hashing table: we don't use it. |
|
115 | - $mo .= pack('L', 0); |
|
116 | - |
|
117 | - // Offset of hashing table: it would start right after the translations index table |
|
118 | - $mo .= pack('L', $translationsIndexOffset + $translationsIndexSize); |
|
119 | - |
|
120 | - // Write the lengths & offsets of the original strings |
|
121 | - foreach ($originalsIndex as $info) { |
|
122 | - $mo .= pack('L', $info['length']); |
|
123 | - $mo .= pack('L', $originalsStringsOffset + $info['relativeOffset']); |
|
124 | - } |
|
125 | - |
|
126 | - // Write the lengths & offsets of the translated strings |
|
127 | - foreach ($translationsIndex as $info) { |
|
128 | - $mo .= pack('L', $info['length']); |
|
129 | - $mo .= pack('L', $translationsStringsOffset + $info['relativeOffset']); |
|
130 | - } |
|
131 | - |
|
132 | - // Write original strings |
|
133 | - $mo .= $originalsTable; |
|
134 | - |
|
135 | - // Write translation strings |
|
136 | - $mo .= $translationsTable; |
|
137 | - |
|
138 | - return $mo; |
|
139 | - } |
|
10 | + use HeadersGeneratorTrait; |
|
11 | + |
|
12 | + public static $options = [ |
|
13 | + 'includeHeaders' => true, |
|
14 | + ]; |
|
15 | + |
|
16 | + /** |
|
17 | + * {@parentDoc}. |
|
18 | + */ |
|
19 | + public static function toString(Translations $translations, array $options = []) |
|
20 | + { |
|
21 | + $options += static::$options; |
|
22 | + $messages = []; |
|
23 | + |
|
24 | + if ($options['includeHeaders']) { |
|
25 | + $messages[''] = static::generateHeaders($translations); |
|
26 | + } |
|
27 | + |
|
28 | + foreach ($translations as $translation) { |
|
29 | + if (!$translation->hasTranslation() || $translation->isDisabled()) { |
|
30 | + continue; |
|
31 | + } |
|
32 | + |
|
33 | + if ($translation->hasContext()) { |
|
34 | + $originalString = $translation->getContext()."\x04".$translation->getOriginal(); |
|
35 | + } else { |
|
36 | + $originalString = $translation->getOriginal(); |
|
37 | + } |
|
38 | + |
|
39 | + $messages[$originalString] = $translation; |
|
40 | + } |
|
41 | + |
|
42 | + ksort($messages); |
|
43 | + $numEntries = count($messages); |
|
44 | + $originalsTable = ''; |
|
45 | + $translationsTable = ''; |
|
46 | + $originalsIndex = []; |
|
47 | + $translationsIndex = []; |
|
48 | + $pluralForm = $translations->getPluralForms(); |
|
49 | + $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
50 | + |
|
51 | + foreach ($messages as $originalString => $translation) { |
|
52 | + if (is_string($translation)) { |
|
53 | + // Headers |
|
54 | + $translationString = $translation; |
|
55 | + } else { |
|
56 | + /* @var $translation \Gettext\Translation */ |
|
57 | + if ($translation->hasPlural() && $translation->hasPluralTranslations(true)) { |
|
58 | + $originalString .= "\x00".$translation->getPlural(); |
|
59 | + $translationString = $translation->getTranslation(); |
|
60 | + $translationString .= "\x00".implode("\x00", $translation->getPluralTranslations($pluralSize)); |
|
61 | + } else { |
|
62 | + $translationString = $translation->getTranslation(); |
|
63 | + } |
|
64 | + } |
|
65 | + |
|
66 | + $originalsIndex[] = [ |
|
67 | + 'relativeOffset' => strlen($originalsTable), |
|
68 | + 'length' => strlen($originalString) |
|
69 | + ]; |
|
70 | + $originalsTable .= $originalString."\x00"; |
|
71 | + $translationsIndex[] = [ |
|
72 | + 'relativeOffset' => strlen($translationsTable), |
|
73 | + 'length' => strlen($translationString) |
|
74 | + ]; |
|
75 | + $translationsTable .= $translationString."\x00"; |
|
76 | + } |
|
77 | + |
|
78 | + // Offset of table with the original strings index: right after the header (which is 7 words) |
|
79 | + $originalsIndexOffset = 7 * 4; |
|
80 | + |
|
81 | + // Size of table with the original strings index |
|
82 | + $originalsIndexSize = $numEntries * (4 + 4); |
|
83 | + |
|
84 | + // Offset of table with the translation strings index: right after the original strings index table |
|
85 | + $translationsIndexOffset = $originalsIndexOffset + $originalsIndexSize; |
|
86 | + |
|
87 | + // Size of table with the translation strings index |
|
88 | + $translationsIndexSize = $numEntries * (4 + 4); |
|
89 | + |
|
90 | + // Hashing table starts after the header and after the index table |
|
91 | + $originalsStringsOffset = $translationsIndexOffset + $translationsIndexSize; |
|
92 | + |
|
93 | + // Translations start after the keys |
|
94 | + $translationsStringsOffset = $originalsStringsOffset + strlen($originalsTable); |
|
95 | + |
|
96 | + // Let's generate the .mo file binary data |
|
97 | + $mo = ''; |
|
98 | + |
|
99 | + // Magic number |
|
100 | + $mo .= pack('L', 0x950412de); |
|
101 | + |
|
102 | + // File format revision |
|
103 | + $mo .= pack('L', 0); |
|
104 | + |
|
105 | + // Number of strings |
|
106 | + $mo .= pack('L', $numEntries); |
|
107 | + |
|
108 | + // Offset of table with original strings |
|
109 | + $mo .= pack('L', $originalsIndexOffset); |
|
110 | + |
|
111 | + // Offset of table with translation strings |
|
112 | + $mo .= pack('L', $translationsIndexOffset); |
|
113 | + |
|
114 | + // Size of hashing table: we don't use it. |
|
115 | + $mo .= pack('L', 0); |
|
116 | + |
|
117 | + // Offset of hashing table: it would start right after the translations index table |
|
118 | + $mo .= pack('L', $translationsIndexOffset + $translationsIndexSize); |
|
119 | + |
|
120 | + // Write the lengths & offsets of the original strings |
|
121 | + foreach ($originalsIndex as $info) { |
|
122 | + $mo .= pack('L', $info['length']); |
|
123 | + $mo .= pack('L', $originalsStringsOffset + $info['relativeOffset']); |
|
124 | + } |
|
125 | + |
|
126 | + // Write the lengths & offsets of the translated strings |
|
127 | + foreach ($translationsIndex as $info) { |
|
128 | + $mo .= pack('L', $info['length']); |
|
129 | + $mo .= pack('L', $translationsStringsOffset + $info['relativeOffset']); |
|
130 | + } |
|
131 | + |
|
132 | + // Write original strings |
|
133 | + $mo .= $originalsTable; |
|
134 | + |
|
135 | + // Write translation strings |
|
136 | + $mo .= $translationsTable; |
|
137 | + |
|
138 | + return $mo; |
|
139 | + } |
|
140 | 140 | } |
@@ -16,117 +16,117 @@ |
||
16 | 16 | /** |
17 | 17 | * {@parentDoc}. |
18 | 18 | */ |
19 | - public static function toString(Translations $translations, array $options = []) |
|
19 | + public static function toString( Translations $translations, array $options = [ ] ) |
|
20 | 20 | { |
21 | 21 | $options += static::$options; |
22 | - $messages = []; |
|
22 | + $messages = [ ]; |
|
23 | 23 | |
24 | - if ($options['includeHeaders']) { |
|
25 | - $messages[''] = static::generateHeaders($translations); |
|
24 | + if ( $options[ 'includeHeaders' ] ) { |
|
25 | + $messages[ '' ] = static::generateHeaders( $translations ); |
|
26 | 26 | } |
27 | 27 | |
28 | - foreach ($translations as $translation) { |
|
29 | - if (!$translation->hasTranslation() || $translation->isDisabled()) { |
|
28 | + foreach ( $translations as $translation ) { |
|
29 | + if ( ! $translation->hasTranslation() || $translation->isDisabled() ) { |
|
30 | 30 | continue; |
31 | 31 | } |
32 | 32 | |
33 | - if ($translation->hasContext()) { |
|
34 | - $originalString = $translation->getContext()."\x04".$translation->getOriginal(); |
|
33 | + if ( $translation->hasContext() ) { |
|
34 | + $originalString = $translation->getContext() . "\x04" . $translation->getOriginal(); |
|
35 | 35 | } else { |
36 | 36 | $originalString = $translation->getOriginal(); |
37 | 37 | } |
38 | 38 | |
39 | - $messages[$originalString] = $translation; |
|
39 | + $messages[ $originalString ] = $translation; |
|
40 | 40 | } |
41 | 41 | |
42 | - ksort($messages); |
|
43 | - $numEntries = count($messages); |
|
42 | + ksort( $messages ); |
|
43 | + $numEntries = count( $messages ); |
|
44 | 44 | $originalsTable = ''; |
45 | 45 | $translationsTable = ''; |
46 | - $originalsIndex = []; |
|
47 | - $translationsIndex = []; |
|
46 | + $originalsIndex = [ ]; |
|
47 | + $translationsIndex = [ ]; |
|
48 | 48 | $pluralForm = $translations->getPluralForms(); |
49 | - $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
49 | + $pluralSize = is_array( $pluralForm ) ? ( $pluralForm[ 0 ] - 1 ) : null; |
|
50 | 50 | |
51 | - foreach ($messages as $originalString => $translation) { |
|
52 | - if (is_string($translation)) { |
|
51 | + foreach ( $messages as $originalString => $translation ) { |
|
52 | + if ( is_string( $translation ) ) { |
|
53 | 53 | // Headers |
54 | 54 | $translationString = $translation; |
55 | 55 | } else { |
56 | 56 | /* @var $translation \Gettext\Translation */ |
57 | - if ($translation->hasPlural() && $translation->hasPluralTranslations(true)) { |
|
58 | - $originalString .= "\x00".$translation->getPlural(); |
|
57 | + if ( $translation->hasPlural() && $translation->hasPluralTranslations( true ) ) { |
|
58 | + $originalString .= "\x00" . $translation->getPlural(); |
|
59 | 59 | $translationString = $translation->getTranslation(); |
60 | - $translationString .= "\x00".implode("\x00", $translation->getPluralTranslations($pluralSize)); |
|
60 | + $translationString .= "\x00" . implode( "\x00", $translation->getPluralTranslations( $pluralSize ) ); |
|
61 | 61 | } else { |
62 | 62 | $translationString = $translation->getTranslation(); |
63 | 63 | } |
64 | 64 | } |
65 | 65 | |
66 | - $originalsIndex[] = [ |
|
67 | - 'relativeOffset' => strlen($originalsTable), |
|
68 | - 'length' => strlen($originalString) |
|
66 | + $originalsIndex[ ] = [ |
|
67 | + 'relativeOffset' => strlen( $originalsTable ), |
|
68 | + 'length' => strlen( $originalString ) |
|
69 | 69 | ]; |
70 | - $originalsTable .= $originalString."\x00"; |
|
71 | - $translationsIndex[] = [ |
|
72 | - 'relativeOffset' => strlen($translationsTable), |
|
73 | - 'length' => strlen($translationString) |
|
70 | + $originalsTable .= $originalString . "\x00"; |
|
71 | + $translationsIndex[ ] = [ |
|
72 | + 'relativeOffset' => strlen( $translationsTable ), |
|
73 | + 'length' => strlen( $translationString ) |
|
74 | 74 | ]; |
75 | - $translationsTable .= $translationString."\x00"; |
|
75 | + $translationsTable .= $translationString . "\x00"; |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | // Offset of table with the original strings index: right after the header (which is 7 words) |
79 | 79 | $originalsIndexOffset = 7 * 4; |
80 | 80 | |
81 | 81 | // Size of table with the original strings index |
82 | - $originalsIndexSize = $numEntries * (4 + 4); |
|
82 | + $originalsIndexSize = $numEntries * ( 4 + 4 ); |
|
83 | 83 | |
84 | 84 | // Offset of table with the translation strings index: right after the original strings index table |
85 | 85 | $translationsIndexOffset = $originalsIndexOffset + $originalsIndexSize; |
86 | 86 | |
87 | 87 | // Size of table with the translation strings index |
88 | - $translationsIndexSize = $numEntries * (4 + 4); |
|
88 | + $translationsIndexSize = $numEntries * ( 4 + 4 ); |
|
89 | 89 | |
90 | 90 | // Hashing table starts after the header and after the index table |
91 | 91 | $originalsStringsOffset = $translationsIndexOffset + $translationsIndexSize; |
92 | 92 | |
93 | 93 | // Translations start after the keys |
94 | - $translationsStringsOffset = $originalsStringsOffset + strlen($originalsTable); |
|
94 | + $translationsStringsOffset = $originalsStringsOffset + strlen( $originalsTable ); |
|
95 | 95 | |
96 | 96 | // Let's generate the .mo file binary data |
97 | 97 | $mo = ''; |
98 | 98 | |
99 | 99 | // Magic number |
100 | - $mo .= pack('L', 0x950412de); |
|
100 | + $mo .= pack( 'L', 0x950412de ); |
|
101 | 101 | |
102 | 102 | // File format revision |
103 | - $mo .= pack('L', 0); |
|
103 | + $mo .= pack( 'L', 0 ); |
|
104 | 104 | |
105 | 105 | // Number of strings |
106 | - $mo .= pack('L', $numEntries); |
|
106 | + $mo .= pack( 'L', $numEntries ); |
|
107 | 107 | |
108 | 108 | // Offset of table with original strings |
109 | - $mo .= pack('L', $originalsIndexOffset); |
|
109 | + $mo .= pack( 'L', $originalsIndexOffset ); |
|
110 | 110 | |
111 | 111 | // Offset of table with translation strings |
112 | - $mo .= pack('L', $translationsIndexOffset); |
|
112 | + $mo .= pack( 'L', $translationsIndexOffset ); |
|
113 | 113 | |
114 | 114 | // Size of hashing table: we don't use it. |
115 | - $mo .= pack('L', 0); |
|
115 | + $mo .= pack( 'L', 0 ); |
|
116 | 116 | |
117 | 117 | // Offset of hashing table: it would start right after the translations index table |
118 | - $mo .= pack('L', $translationsIndexOffset + $translationsIndexSize); |
|
118 | + $mo .= pack( 'L', $translationsIndexOffset + $translationsIndexSize ); |
|
119 | 119 | |
120 | 120 | // Write the lengths & offsets of the original strings |
121 | - foreach ($originalsIndex as $info) { |
|
122 | - $mo .= pack('L', $info['length']); |
|
123 | - $mo .= pack('L', $originalsStringsOffset + $info['relativeOffset']); |
|
121 | + foreach ( $originalsIndex as $info ) { |
|
122 | + $mo .= pack( 'L', $info[ 'length' ] ); |
|
123 | + $mo .= pack( 'L', $originalsStringsOffset + $info[ 'relativeOffset' ] ); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | // Write the lengths & offsets of the translated strings |
127 | - foreach ($translationsIndex as $info) { |
|
128 | - $mo .= pack('L', $info['length']); |
|
129 | - $mo .= pack('L', $translationsStringsOffset + $info['relativeOffset']); |
|
127 | + foreach ( $translationsIndex as $info ) { |
|
128 | + $mo .= pack( 'L', $info[ 'length' ] ); |
|
129 | + $mo .= pack( 'L', $translationsStringsOffset + $info[ 'relativeOffset' ] ); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | // Write original strings |
@@ -5,8 +5,7 @@ discard block |
||
5 | 5 | use Gettext\Translations; |
6 | 6 | use Gettext\Utils\HeadersGeneratorTrait; |
7 | 7 | |
8 | -class Mo extends Generator implements GeneratorInterface |
|
9 | -{ |
|
8 | +class Mo extends Generator implements GeneratorInterface { |
|
10 | 9 | use HeadersGeneratorTrait; |
11 | 10 | |
12 | 11 | public static $options = [ |
@@ -16,8 +15,7 @@ discard block |
||
16 | 15 | /** |
17 | 16 | * {@parentDoc}. |
18 | 17 | */ |
19 | - public static function toString(Translations $translations, array $options = []) |
|
20 | - { |
|
18 | + public static function toString(Translations $translations, array $options = []) { |
|
21 | 19 | $options += static::$options; |
22 | 20 | $messages = []; |
23 | 21 |
@@ -6,61 +6,61 @@ |
||
6 | 6 | |
7 | 7 | class Jed extends Generator implements GeneratorInterface |
8 | 8 | { |
9 | - public static $options = [ |
|
10 | - 'json' => 0, |
|
11 | - ]; |
|
9 | + public static $options = [ |
|
10 | + 'json' => 0, |
|
11 | + ]; |
|
12 | 12 | |
13 | - /** |
|
14 | - * {@parentDoc}. |
|
15 | - */ |
|
16 | - public static function toString(Translations $translations, array $options = []) |
|
17 | - { |
|
18 | - $domain = $translations->getDomain() ?: 'messages'; |
|
19 | - $options += static::$options; |
|
13 | + /** |
|
14 | + * {@parentDoc}. |
|
15 | + */ |
|
16 | + public static function toString(Translations $translations, array $options = []) |
|
17 | + { |
|
18 | + $domain = $translations->getDomain() ?: 'messages'; |
|
19 | + $options += static::$options; |
|
20 | 20 | |
21 | - return json_encode([ |
|
22 | - $domain => [ |
|
23 | - '' => [ |
|
24 | - 'domain' => $domain, |
|
25 | - 'lang' => $translations->getLanguage() ?: 'en', |
|
26 | - 'plural-forms' => $translations->getHeader('Plural-Forms') ?: 'nplurals=2; plural=(n != 1);', |
|
27 | - ], |
|
28 | - ] + static::buildMessages($translations), |
|
29 | - ], $options['json']); |
|
30 | - } |
|
21 | + return json_encode([ |
|
22 | + $domain => [ |
|
23 | + '' => [ |
|
24 | + 'domain' => $domain, |
|
25 | + 'lang' => $translations->getLanguage() ?: 'en', |
|
26 | + 'plural-forms' => $translations->getHeader('Plural-Forms') ?: 'nplurals=2; plural=(n != 1);', |
|
27 | + ], |
|
28 | + ] + static::buildMessages($translations), |
|
29 | + ], $options['json']); |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Generates an array with all translations. |
|
34 | - * |
|
35 | - * @param Translations $translations |
|
36 | - * |
|
37 | - * @return array |
|
38 | - */ |
|
39 | - protected static function buildMessages(Translations $translations) |
|
40 | - { |
|
41 | - $pluralForm = $translations->getPluralForms(); |
|
42 | - $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
43 | - $messages = []; |
|
44 | - $context_glue = '\u0004'; |
|
32 | + /** |
|
33 | + * Generates an array with all translations. |
|
34 | + * |
|
35 | + * @param Translations $translations |
|
36 | + * |
|
37 | + * @return array |
|
38 | + */ |
|
39 | + protected static function buildMessages(Translations $translations) |
|
40 | + { |
|
41 | + $pluralForm = $translations->getPluralForms(); |
|
42 | + $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
43 | + $messages = []; |
|
44 | + $context_glue = '\u0004'; |
|
45 | 45 | |
46 | - foreach ($translations as $translation) { |
|
47 | - if ($translation->isDisabled()) { |
|
48 | - continue; |
|
49 | - } |
|
46 | + foreach ($translations as $translation) { |
|
47 | + if ($translation->isDisabled()) { |
|
48 | + continue; |
|
49 | + } |
|
50 | 50 | |
51 | - $key = ($translation->hasContext() ? $translation->getContext().$context_glue : '') |
|
52 | - .$translation->getOriginal(); |
|
51 | + $key = ($translation->hasContext() ? $translation->getContext().$context_glue : '') |
|
52 | + .$translation->getOriginal(); |
|
53 | 53 | |
54 | - if ($translation->hasPluralTranslations(true)) { |
|
55 | - $message = $translation->getPluralTranslations($pluralSize); |
|
56 | - array_unshift($message, $translation->getTranslation()); |
|
57 | - } else { |
|
58 | - $message = [$translation->getTranslation()]; |
|
59 | - } |
|
54 | + if ($translation->hasPluralTranslations(true)) { |
|
55 | + $message = $translation->getPluralTranslations($pluralSize); |
|
56 | + array_unshift($message, $translation->getTranslation()); |
|
57 | + } else { |
|
58 | + $message = [$translation->getTranslation()]; |
|
59 | + } |
|
60 | 60 | |
61 | - $messages[$key] = $message; |
|
62 | - } |
|
61 | + $messages[$key] = $message; |
|
62 | + } |
|
63 | 63 | |
64 | - return $messages; |
|
65 | - } |
|
64 | + return $messages; |
|
65 | + } |
|
66 | 66 | } |
@@ -13,20 +13,20 @@ discard block |
||
13 | 13 | /** |
14 | 14 | * {@parentDoc}. |
15 | 15 | */ |
16 | - public static function toString(Translations $translations, array $options = []) |
|
16 | + public static function toString( Translations $translations, array $options = [ ] ) |
|
17 | 17 | { |
18 | 18 | $domain = $translations->getDomain() ?: 'messages'; |
19 | 19 | $options += static::$options; |
20 | 20 | |
21 | - return json_encode([ |
|
21 | + return json_encode( [ |
|
22 | 22 | $domain => [ |
23 | 23 | '' => [ |
24 | 24 | 'domain' => $domain, |
25 | 25 | 'lang' => $translations->getLanguage() ?: 'en', |
26 | - 'plural-forms' => $translations->getHeader('Plural-Forms') ?: 'nplurals=2; plural=(n != 1);', |
|
26 | + 'plural-forms' => $translations->getHeader( 'Plural-Forms' ) ?: 'nplurals=2; plural=(n != 1);', |
|
27 | 27 | ], |
28 | - ] + static::buildMessages($translations), |
|
29 | - ], $options['json']); |
|
28 | + ] + static::buildMessages( $translations ), |
|
29 | + ], $options[ 'json' ] ); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | /** |
@@ -36,29 +36,29 @@ discard block |
||
36 | 36 | * |
37 | 37 | * @return array |
38 | 38 | */ |
39 | - protected static function buildMessages(Translations $translations) |
|
39 | + protected static function buildMessages( Translations $translations ) |
|
40 | 40 | { |
41 | 41 | $pluralForm = $translations->getPluralForms(); |
42 | - $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
43 | - $messages = []; |
|
42 | + $pluralSize = is_array( $pluralForm ) ? ( $pluralForm[ 0 ] - 1 ) : null; |
|
43 | + $messages = [ ]; |
|
44 | 44 | $context_glue = '\u0004'; |
45 | 45 | |
46 | - foreach ($translations as $translation) { |
|
47 | - if ($translation->isDisabled()) { |
|
46 | + foreach ( $translations as $translation ) { |
|
47 | + if ( $translation->isDisabled() ) { |
|
48 | 48 | continue; |
49 | 49 | } |
50 | 50 | |
51 | - $key = ($translation->hasContext() ? $translation->getContext().$context_glue : '') |
|
51 | + $key = ( $translation->hasContext() ? $translation->getContext() . $context_glue : '' ) |
|
52 | 52 | .$translation->getOriginal(); |
53 | 53 | |
54 | - if ($translation->hasPluralTranslations(true)) { |
|
55 | - $message = $translation->getPluralTranslations($pluralSize); |
|
56 | - array_unshift($message, $translation->getTranslation()); |
|
54 | + if ( $translation->hasPluralTranslations( true ) ) { |
|
55 | + $message = $translation->getPluralTranslations( $pluralSize ); |
|
56 | + array_unshift( $message, $translation->getTranslation() ); |
|
57 | 57 | } else { |
58 | - $message = [$translation->getTranslation()]; |
|
58 | + $message = [ $translation->getTranslation() ]; |
|
59 | 59 | } |
60 | 60 | |
61 | - $messages[$key] = $message; |
|
61 | + $messages[ $key ] = $message; |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | return $messages; |
@@ -4,8 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Gettext\Translations; |
6 | 6 | |
7 | -class Jed extends Generator implements GeneratorInterface |
|
8 | -{ |
|
7 | +class Jed extends Generator implements GeneratorInterface { |
|
9 | 8 | public static $options = [ |
10 | 9 | 'json' => 0, |
11 | 10 | ]; |
@@ -13,8 +12,7 @@ discard block |
||
13 | 12 | /** |
14 | 13 | * {@parentDoc}. |
15 | 14 | */ |
16 | - public static function toString(Translations $translations, array $options = []) |
|
17 | - { |
|
15 | + public static function toString(Translations $translations, array $options = []) { |
|
18 | 16 | $domain = $translations->getDomain() ?: 'messages'; |
19 | 17 | $options += static::$options; |
20 | 18 | |
@@ -36,8 +34,7 @@ discard block |
||
36 | 34 | * |
37 | 35 | * @return array |
38 | 36 | */ |
39 | - protected static function buildMessages(Translations $translations) |
|
40 | - { |
|
37 | + protected static function buildMessages(Translations $translations) { |
|
41 | 38 | $pluralForm = $translations->getPluralForms(); |
42 | 39 | $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
43 | 40 | $messages = []; |
@@ -6,140 +6,140 @@ |
||
6 | 6 | |
7 | 7 | class Po extends Generator implements GeneratorInterface |
8 | 8 | { |
9 | - public static $options = [ |
|
10 | - 'noLocation' => false, |
|
11 | - ]; |
|
12 | - |
|
13 | - /** |
|
14 | - * {@parentDoc}. |
|
15 | - */ |
|
16 | - public static function toString(Translations $translations, array $options = []) |
|
17 | - { |
|
18 | - $options += static::$options; |
|
19 | - |
|
20 | - $pluralForm = $translations->getPluralForms(); |
|
21 | - $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
22 | - $lines = ['msgid ""', 'msgstr ""']; |
|
23 | - |
|
24 | - foreach ($translations->getHeaders() as $name => $value) { |
|
25 | - $lines[] = sprintf('"%s: %s\\n"', $name, $value); |
|
26 | - } |
|
27 | - |
|
28 | - $lines[] = ''; |
|
29 | - |
|
30 | - //Translations |
|
31 | - foreach ($translations as $translation) { |
|
32 | - if ($translation->hasComments()) { |
|
33 | - foreach ($translation->getComments() as $comment) { |
|
34 | - $lines[] = '# '.$comment; |
|
35 | - } |
|
36 | - } |
|
37 | - |
|
38 | - if ($translation->hasExtractedComments()) { |
|
39 | - foreach ($translation->getExtractedComments() as $comment) { |
|
40 | - $lines[] = '#. '.$comment; |
|
41 | - } |
|
42 | - } |
|
43 | - |
|
44 | - if (!$options['noLocation'] && $translation->hasReferences()) { |
|
45 | - foreach ($translation->getReferences() as $reference) { |
|
46 | - $lines[] = '#: '.$reference[0].(!is_null($reference[1]) ? ':'.$reference[1] : null); |
|
47 | - } |
|
48 | - } |
|
49 | - |
|
50 | - if ($translation->hasFlags()) { |
|
51 | - $lines[] = '#, '.implode(',', $translation->getFlags()); |
|
52 | - } |
|
53 | - |
|
54 | - $prefix = $translation->isDisabled() ? '#~ ' : ''; |
|
55 | - |
|
56 | - if ($translation->hasContext()) { |
|
57 | - $lines[] = $prefix.'msgctxt '.static::convertString($translation->getContext()); |
|
58 | - } |
|
59 | - |
|
60 | - static::addLines($lines, $prefix.'msgid', $translation->getOriginal()); |
|
61 | - |
|
62 | - if ($translation->hasPlural()) { |
|
63 | - static::addLines($lines, $prefix.'msgid_plural', $translation->getPlural()); |
|
64 | - static::addLines($lines, $prefix.'msgstr[0]', $translation->getTranslation()); |
|
65 | - |
|
66 | - foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { |
|
67 | - static::addLines($lines, $prefix.'msgstr['.($k + 1).']', $v); |
|
68 | - } |
|
69 | - } else { |
|
70 | - static::addLines($lines, $prefix.'msgstr', $translation->getTranslation()); |
|
71 | - } |
|
72 | - |
|
73 | - $lines[] = ''; |
|
74 | - } |
|
75 | - |
|
76 | - return implode("\n", $lines); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Escapes and adds double quotes to a string. |
|
81 | - * |
|
82 | - * @param string $string |
|
83 | - * |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - protected static function multilineQuote($string) |
|
87 | - { |
|
88 | - $lines = explode("\n", $string); |
|
89 | - $last = count($lines) - 1; |
|
90 | - |
|
91 | - foreach ($lines as $k => $line) { |
|
92 | - if ($k === $last) { |
|
93 | - $lines[$k] = static::convertString($line); |
|
94 | - } else { |
|
95 | - $lines[$k] = static::convertString($line."\n"); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - return $lines; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Add one or more lines depending whether the string is multiline or not. |
|
104 | - * |
|
105 | - * @param array &$lines |
|
106 | - * @param string $name |
|
107 | - * @param string $value |
|
108 | - */ |
|
109 | - protected static function addLines(array &$lines, $name, $value) |
|
110 | - { |
|
111 | - $newLines = static::multilineQuote($value); |
|
112 | - |
|
113 | - if (count($newLines) === 1) { |
|
114 | - $lines[] = $name.' '.$newLines[0]; |
|
115 | - } else { |
|
116 | - $lines[] = $name.' ""'; |
|
117 | - |
|
118 | - foreach ($newLines as $line) { |
|
119 | - $lines[] = $line; |
|
120 | - } |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Convert a string to its PO representation. |
|
126 | - * |
|
127 | - * @param string $value |
|
128 | - * |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public static function convertString($value) |
|
132 | - { |
|
133 | - return '"'.strtr( |
|
134 | - $value, |
|
135 | - [ |
|
136 | - "\x00" => '', |
|
137 | - '\\' => '\\\\', |
|
138 | - "\t" => '\t', |
|
139 | - "\r" => '\r', |
|
140 | - "\n" => '\n', |
|
141 | - '"' => '\\"', |
|
142 | - ] |
|
143 | - ).'"'; |
|
144 | - } |
|
9 | + public static $options = [ |
|
10 | + 'noLocation' => false, |
|
11 | + ]; |
|
12 | + |
|
13 | + /** |
|
14 | + * {@parentDoc}. |
|
15 | + */ |
|
16 | + public static function toString(Translations $translations, array $options = []) |
|
17 | + { |
|
18 | + $options += static::$options; |
|
19 | + |
|
20 | + $pluralForm = $translations->getPluralForms(); |
|
21 | + $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
22 | + $lines = ['msgid ""', 'msgstr ""']; |
|
23 | + |
|
24 | + foreach ($translations->getHeaders() as $name => $value) { |
|
25 | + $lines[] = sprintf('"%s: %s\\n"', $name, $value); |
|
26 | + } |
|
27 | + |
|
28 | + $lines[] = ''; |
|
29 | + |
|
30 | + //Translations |
|
31 | + foreach ($translations as $translation) { |
|
32 | + if ($translation->hasComments()) { |
|
33 | + foreach ($translation->getComments() as $comment) { |
|
34 | + $lines[] = '# '.$comment; |
|
35 | + } |
|
36 | + } |
|
37 | + |
|
38 | + if ($translation->hasExtractedComments()) { |
|
39 | + foreach ($translation->getExtractedComments() as $comment) { |
|
40 | + $lines[] = '#. '.$comment; |
|
41 | + } |
|
42 | + } |
|
43 | + |
|
44 | + if (!$options['noLocation'] && $translation->hasReferences()) { |
|
45 | + foreach ($translation->getReferences() as $reference) { |
|
46 | + $lines[] = '#: '.$reference[0].(!is_null($reference[1]) ? ':'.$reference[1] : null); |
|
47 | + } |
|
48 | + } |
|
49 | + |
|
50 | + if ($translation->hasFlags()) { |
|
51 | + $lines[] = '#, '.implode(',', $translation->getFlags()); |
|
52 | + } |
|
53 | + |
|
54 | + $prefix = $translation->isDisabled() ? '#~ ' : ''; |
|
55 | + |
|
56 | + if ($translation->hasContext()) { |
|
57 | + $lines[] = $prefix.'msgctxt '.static::convertString($translation->getContext()); |
|
58 | + } |
|
59 | + |
|
60 | + static::addLines($lines, $prefix.'msgid', $translation->getOriginal()); |
|
61 | + |
|
62 | + if ($translation->hasPlural()) { |
|
63 | + static::addLines($lines, $prefix.'msgid_plural', $translation->getPlural()); |
|
64 | + static::addLines($lines, $prefix.'msgstr[0]', $translation->getTranslation()); |
|
65 | + |
|
66 | + foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { |
|
67 | + static::addLines($lines, $prefix.'msgstr['.($k + 1).']', $v); |
|
68 | + } |
|
69 | + } else { |
|
70 | + static::addLines($lines, $prefix.'msgstr', $translation->getTranslation()); |
|
71 | + } |
|
72 | + |
|
73 | + $lines[] = ''; |
|
74 | + } |
|
75 | + |
|
76 | + return implode("\n", $lines); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Escapes and adds double quotes to a string. |
|
81 | + * |
|
82 | + * @param string $string |
|
83 | + * |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + protected static function multilineQuote($string) |
|
87 | + { |
|
88 | + $lines = explode("\n", $string); |
|
89 | + $last = count($lines) - 1; |
|
90 | + |
|
91 | + foreach ($lines as $k => $line) { |
|
92 | + if ($k === $last) { |
|
93 | + $lines[$k] = static::convertString($line); |
|
94 | + } else { |
|
95 | + $lines[$k] = static::convertString($line."\n"); |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + return $lines; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Add one or more lines depending whether the string is multiline or not. |
|
104 | + * |
|
105 | + * @param array &$lines |
|
106 | + * @param string $name |
|
107 | + * @param string $value |
|
108 | + */ |
|
109 | + protected static function addLines(array &$lines, $name, $value) |
|
110 | + { |
|
111 | + $newLines = static::multilineQuote($value); |
|
112 | + |
|
113 | + if (count($newLines) === 1) { |
|
114 | + $lines[] = $name.' '.$newLines[0]; |
|
115 | + } else { |
|
116 | + $lines[] = $name.' ""'; |
|
117 | + |
|
118 | + foreach ($newLines as $line) { |
|
119 | + $lines[] = $line; |
|
120 | + } |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Convert a string to its PO representation. |
|
126 | + * |
|
127 | + * @param string $value |
|
128 | + * |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public static function convertString($value) |
|
132 | + { |
|
133 | + return '"'.strtr( |
|
134 | + $value, |
|
135 | + [ |
|
136 | + "\x00" => '', |
|
137 | + '\\' => '\\\\', |
|
138 | + "\t" => '\t', |
|
139 | + "\r" => '\r', |
|
140 | + "\n" => '\n', |
|
141 | + '"' => '\\"', |
|
142 | + ] |
|
143 | + ).'"'; |
|
144 | + } |
|
145 | 145 | } |
@@ -13,67 +13,67 @@ discard block |
||
13 | 13 | /** |
14 | 14 | * {@parentDoc}. |
15 | 15 | */ |
16 | - public static function toString(Translations $translations, array $options = []) |
|
16 | + public static function toString( Translations $translations, array $options = [ ] ) |
|
17 | 17 | { |
18 | 18 | $options += static::$options; |
19 | 19 | |
20 | 20 | $pluralForm = $translations->getPluralForms(); |
21 | - $pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null; |
|
22 | - $lines = ['msgid ""', 'msgstr ""']; |
|
21 | + $pluralSize = is_array( $pluralForm ) ? ( $pluralForm[ 0 ] - 1 ) : null; |
|
22 | + $lines = [ 'msgid ""', 'msgstr ""' ]; |
|
23 | 23 | |
24 | - foreach ($translations->getHeaders() as $name => $value) { |
|
25 | - $lines[] = sprintf('"%s: %s\\n"', $name, $value); |
|
24 | + foreach ( $translations->getHeaders() as $name => $value ) { |
|
25 | + $lines[ ] = sprintf( '"%s: %s\\n"', $name, $value ); |
|
26 | 26 | } |
27 | 27 | |
28 | - $lines[] = ''; |
|
28 | + $lines[ ] = ''; |
|
29 | 29 | |
30 | 30 | //Translations |
31 | - foreach ($translations as $translation) { |
|
32 | - if ($translation->hasComments()) { |
|
33 | - foreach ($translation->getComments() as $comment) { |
|
34 | - $lines[] = '# '.$comment; |
|
31 | + foreach ( $translations as $translation ) { |
|
32 | + if ( $translation->hasComments() ) { |
|
33 | + foreach ( $translation->getComments() as $comment ) { |
|
34 | + $lines[ ] = '# ' . $comment; |
|
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
38 | - if ($translation->hasExtractedComments()) { |
|
39 | - foreach ($translation->getExtractedComments() as $comment) { |
|
40 | - $lines[] = '#. '.$comment; |
|
38 | + if ( $translation->hasExtractedComments() ) { |
|
39 | + foreach ( $translation->getExtractedComments() as $comment ) { |
|
40 | + $lines[ ] = '#. ' . $comment; |
|
41 | 41 | } |
42 | 42 | } |
43 | 43 | |
44 | - if (!$options['noLocation'] && $translation->hasReferences()) { |
|
45 | - foreach ($translation->getReferences() as $reference) { |
|
46 | - $lines[] = '#: '.$reference[0].(!is_null($reference[1]) ? ':'.$reference[1] : null); |
|
44 | + if ( ! $options[ 'noLocation' ] && $translation->hasReferences() ) { |
|
45 | + foreach ( $translation->getReferences() as $reference ) { |
|
46 | + $lines[ ] = '#: ' . $reference[ 0 ] . ( ! is_null( $reference[ 1 ] ) ? ':' . $reference[ 1 ] : null ); |
|
47 | 47 | } |
48 | 48 | } |
49 | 49 | |
50 | - if ($translation->hasFlags()) { |
|
51 | - $lines[] = '#, '.implode(',', $translation->getFlags()); |
|
50 | + if ( $translation->hasFlags() ) { |
|
51 | + $lines[ ] = '#, ' . implode( ',', $translation->getFlags() ); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | $prefix = $translation->isDisabled() ? '#~ ' : ''; |
55 | 55 | |
56 | - if ($translation->hasContext()) { |
|
57 | - $lines[] = $prefix.'msgctxt '.static::convertString($translation->getContext()); |
|
56 | + if ( $translation->hasContext() ) { |
|
57 | + $lines[ ] = $prefix . 'msgctxt ' . static::convertString( $translation->getContext() ); |
|
58 | 58 | } |
59 | 59 | |
60 | - static::addLines($lines, $prefix.'msgid', $translation->getOriginal()); |
|
60 | + static::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() ); |
|
61 | 61 | |
62 | - if ($translation->hasPlural()) { |
|
63 | - static::addLines($lines, $prefix.'msgid_plural', $translation->getPlural()); |
|
64 | - static::addLines($lines, $prefix.'msgstr[0]', $translation->getTranslation()); |
|
62 | + if ( $translation->hasPlural() ) { |
|
63 | + static::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() ); |
|
64 | + static::addLines( $lines, $prefix . 'msgstr[0]', $translation->getTranslation() ); |
|
65 | 65 | |
66 | - foreach ($translation->getPluralTranslations($pluralSize) as $k => $v) { |
|
67 | - static::addLines($lines, $prefix.'msgstr['.($k + 1).']', $v); |
|
66 | + foreach ( $translation->getPluralTranslations( $pluralSize ) as $k => $v ) { |
|
67 | + static::addLines( $lines, $prefix . 'msgstr[' . ( $k + 1 ) . ']', $v ); |
|
68 | 68 | } |
69 | 69 | } else { |
70 | - static::addLines($lines, $prefix.'msgstr', $translation->getTranslation()); |
|
70 | + static::addLines( $lines, $prefix . 'msgstr', $translation->getTranslation() ); |
|
71 | 71 | } |
72 | 72 | |
73 | - $lines[] = ''; |
|
73 | + $lines[ ] = ''; |
|
74 | 74 | } |
75 | 75 | |
76 | - return implode("\n", $lines); |
|
76 | + return implode( "\n", $lines ); |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | /** |
@@ -83,16 +83,16 @@ discard block |
||
83 | 83 | * |
84 | 84 | * @return string |
85 | 85 | */ |
86 | - protected static function multilineQuote($string) |
|
86 | + protected static function multilineQuote( $string ) |
|
87 | 87 | { |
88 | - $lines = explode("\n", $string); |
|
89 | - $last = count($lines) - 1; |
|
88 | + $lines = explode( "\n", $string ); |
|
89 | + $last = count( $lines ) - 1; |
|
90 | 90 | |
91 | - foreach ($lines as $k => $line) { |
|
92 | - if ($k === $last) { |
|
93 | - $lines[$k] = static::convertString($line); |
|
91 | + foreach ( $lines as $k => $line ) { |
|
92 | + if ( $k === $last ) { |
|
93 | + $lines[ $k ] = static::convertString( $line ); |
|
94 | 94 | } else { |
95 | - $lines[$k] = static::convertString($line."\n"); |
|
95 | + $lines[ $k ] = static::convertString( $line . "\n" ); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | |
@@ -106,17 +106,17 @@ discard block |
||
106 | 106 | * @param string $name |
107 | 107 | * @param string $value |
108 | 108 | */ |
109 | - protected static function addLines(array &$lines, $name, $value) |
|
109 | + protected static function addLines( array &$lines, $name, $value ) |
|
110 | 110 | { |
111 | - $newLines = static::multilineQuote($value); |
|
111 | + $newLines = static::multilineQuote( $value ); |
|
112 | 112 | |
113 | - if (count($newLines) === 1) { |
|
114 | - $lines[] = $name.' '.$newLines[0]; |
|
113 | + if ( count( $newLines ) === 1 ) { |
|
114 | + $lines[ ] = $name . ' ' . $newLines[ 0 ]; |
|
115 | 115 | } else { |
116 | - $lines[] = $name.' ""'; |
|
116 | + $lines[ ] = $name . ' ""'; |
|
117 | 117 | |
118 | - foreach ($newLines as $line) { |
|
119 | - $lines[] = $line; |
|
118 | + foreach ( $newLines as $line ) { |
|
119 | + $lines[ ] = $line; |
|
120 | 120 | } |
121 | 121 | } |
122 | 122 | } |
@@ -128,9 +128,9 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @return string |
130 | 130 | */ |
131 | - public static function convertString($value) |
|
131 | + public static function convertString( $value ) |
|
132 | 132 | { |
133 | - return '"'.strtr( |
|
133 | + return '"' . strtr( |
|
134 | 134 | $value, |
135 | 135 | [ |
136 | 136 | "\x00" => '', |
@@ -140,6 +140,6 @@ discard block |
||
140 | 140 | "\n" => '\n', |
141 | 141 | '"' => '\\"', |
142 | 142 | ] |
143 | - ).'"'; |
|
143 | + ) . '"'; |
|
144 | 144 | } |
145 | 145 | } |
@@ -4,8 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Gettext\Translations; |
6 | 6 | |
7 | -class Po extends Generator implements GeneratorInterface |
|
8 | -{ |
|
7 | +class Po extends Generator implements GeneratorInterface { |
|
9 | 8 | public static $options = [ |
10 | 9 | 'noLocation' => false, |
11 | 10 | ]; |
@@ -13,8 +12,7 @@ discard block |
||
13 | 12 | /** |
14 | 13 | * {@parentDoc}. |
15 | 14 | */ |
16 | - public static function toString(Translations $translations, array $options = []) |
|
17 | - { |
|
15 | + public static function toString(Translations $translations, array $options = []) { |
|
18 | 16 | $options += static::$options; |
19 | 17 | |
20 | 18 | $pluralForm = $translations->getPluralForms(); |
@@ -83,8 +81,7 @@ discard block |
||
83 | 81 | * |
84 | 82 | * @return string |
85 | 83 | */ |
86 | - protected static function multilineQuote($string) |
|
87 | - { |
|
84 | + protected static function multilineQuote($string) { |
|
88 | 85 | $lines = explode("\n", $string); |
89 | 86 | $last = count($lines) - 1; |
90 | 87 | |
@@ -106,8 +103,7 @@ discard block |
||
106 | 103 | * @param string $name |
107 | 104 | * @param string $value |
108 | 105 | */ |
109 | - protected static function addLines(array &$lines, $name, $value) |
|
110 | - { |
|
106 | + protected static function addLines(array &$lines, $name, $value) { |
|
111 | 107 | $newLines = static::multilineQuote($value); |
112 | 108 | |
113 | 109 | if (count($newLines) === 1) { |
@@ -128,8 +124,7 @@ discard block |
||
128 | 124 | * |
129 | 125 | * @return string |
130 | 126 | */ |
131 | - public static function convertString($value) |
|
132 | - { |
|
127 | + public static function convertString($value) { |
|
133 | 128 | return '"'.strtr( |
134 | 129 | $value, |
135 | 130 | [ |
@@ -7,20 +7,20 @@ |
||
7 | 7 | |
8 | 8 | class Json extends Generator implements GeneratorInterface |
9 | 9 | { |
10 | - use MultidimensionalArrayTrait; |
|
10 | + use MultidimensionalArrayTrait; |
|
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 | - * {@inheritdoc} |
|
19 | - */ |
|
20 | - public static function toString(Translations $translations, array $options = []) |
|
21 | - { |
|
22 | - $options += static::$options; |
|
17 | + /** |
|
18 | + * {@inheritdoc} |
|
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'], true), $options['json']); |
|
25 | - } |
|
24 | + return json_encode(static::toArray($translations, $options['includeHeaders'], true), $options['json']); |
|
25 | + } |
|
26 | 26 | } |
@@ -17,10 +17,10 @@ |
||
17 | 17 | /** |
18 | 18 | * {@inheritdoc} |
19 | 19 | */ |
20 | - public static function toString(Translations $translations, array $options = []) |
|
20 | + public static function toString( Translations $translations, array $options = [ ] ) |
|
21 | 21 | { |
22 | 22 | $options += static::$options; |
23 | 23 | |
24 | - return json_encode(static::toArray($translations, $options['includeHeaders'], true), $options['json']); |
|
24 | + return json_encode( static::toArray( $translations, $options[ 'includeHeaders' ], true ), $options[ 'json' ] ); |
|
25 | 25 | } |
26 | 26 | } |
@@ -5,8 +5,7 @@ discard block |
||
5 | 5 | use Gettext\Translations; |
6 | 6 | use Gettext\Utils\MultidimensionalArrayTrait; |
7 | 7 | |
8 | -class Json extends Generator implements GeneratorInterface |
|
9 | -{ |
|
8 | +class Json extends Generator implements GeneratorInterface { |
|
10 | 9 | use MultidimensionalArrayTrait; |
11 | 10 | |
12 | 11 | public static $options = [ |
@@ -17,8 +16,7 @@ discard block |
||
17 | 16 | /** |
18 | 17 | * {@inheritdoc} |
19 | 18 | */ |
20 | - public static function toString(Translations $translations, array $options = []) |
|
21 | - { |
|
19 | + public static function toString(Translations $translations, array $options = []) { |
|
22 | 20 | $options += static::$options; |
23 | 21 | |
24 | 22 | return json_encode(static::toArray($translations, $options['includeHeaders'], true), $options['json']); |
@@ -6,17 +6,17 @@ |
||
6 | 6 | |
7 | 7 | abstract class Generator implements GeneratorInterface |
8 | 8 | { |
9 | - /** |
|
10 | - * {@inheritdoc} |
|
11 | - */ |
|
12 | - public static function toFile(Translations $translations, $file, array $options = []) |
|
13 | - { |
|
14 | - $content = static::toString($translations, $options); |
|
9 | + /** |
|
10 | + * {@inheritdoc} |
|
11 | + */ |
|
12 | + public static function toFile(Translations $translations, $file, array $options = []) |
|
13 | + { |
|
14 | + $content = static::toString($translations, $options); |
|
15 | 15 | |
16 | - if (file_put_contents($file, $content) === false) { |
|
17 | - return false; |
|
18 | - } |
|
16 | + if (file_put_contents($file, $content) === false) { |
|
17 | + return false; |
|
18 | + } |
|
19 | 19 | |
20 | - return true; |
|
21 | - } |
|
20 | + return true; |
|
21 | + } |
|
22 | 22 | } |
@@ -9,11 +9,11 @@ |
||
9 | 9 | /** |
10 | 10 | * {@inheritdoc} |
11 | 11 | */ |
12 | - public static function toFile(Translations $translations, $file, array $options = []) |
|
12 | + public static function toFile( Translations $translations, $file, array $options = [ ] ) |
|
13 | 13 | { |
14 | - $content = static::toString($translations, $options); |
|
14 | + $content = static::toString( $translations, $options ); |
|
15 | 15 | |
16 | - if (file_put_contents($file, $content) === false) { |
|
16 | + if ( file_put_contents( $file, $content ) === false ) { |
|
17 | 17 | return false; |
18 | 18 | } |
19 | 19 |
@@ -4,13 +4,11 @@ |
||
4 | 4 | |
5 | 5 | use Gettext\Translations; |
6 | 6 | |
7 | -abstract class Generator implements GeneratorInterface |
|
8 | -{ |
|
7 | +abstract class Generator implements GeneratorInterface { |
|
9 | 8 | /** |
10 | 9 | * {@inheritdoc} |
11 | 10 | */ |
12 | - public static function toFile(Translations $translations, $file, array $options = []) |
|
13 | - { |
|
11 | + public static function toFile(Translations $translations, $file, array $options = []) { |
|
14 | 12 | $content = static::toString($translations, $options); |
15 | 13 | |
16 | 14 | if (file_put_contents($file, $content) === false) { |