@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -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 | } |
@@ -8,25 +8,25 @@ |
||
8 | 8 | |
9 | 9 | class Yaml extends Generator implements GeneratorInterface |
10 | 10 | { |
11 | - use MultidimensionalArrayTrait; |
|
11 | + use MultidimensionalArrayTrait; |
|
12 | 12 | |
13 | - public static $options = [ |
|
14 | - 'includeHeaders' => false, |
|
15 | - 'indent' => 2, |
|
16 | - 'inline' => 4, |
|
17 | - ]; |
|
13 | + public static $options = [ |
|
14 | + 'includeHeaders' => false, |
|
15 | + 'indent' => 2, |
|
16 | + 'inline' => 4, |
|
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 | } |
@@ -4,36 +4,36 @@ |
||
4 | 4 | |
5 | 5 | abstract class BaseTranslator implements TranslatorInterface |
6 | 6 | { |
7 | - /** @var TranslatorInterface */ |
|
8 | - public static $current; |
|
9 | - |
|
10 | - /** |
|
11 | - * @see TranslatorInterface |
|
12 | - */ |
|
13 | - public function noop($original) |
|
14 | - { |
|
15 | - return $original; |
|
16 | - } |
|
17 | - |
|
18 | - /** |
|
19 | - * @see TranslatorInterface |
|
20 | - */ |
|
21 | - public function register() |
|
22 | - { |
|
23 | - $previous = static::$current; |
|
24 | - |
|
25 | - static::$current = $this; |
|
26 | - |
|
27 | - static::includeFunctions(); |
|
28 | - |
|
29 | - return $previous; |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Include the gettext functions |
|
34 | - */ |
|
35 | - public static function includeFunctions() |
|
36 | - { |
|
37 | - include_once __DIR__.'/translator_functions.php'; |
|
38 | - } |
|
7 | + /** @var TranslatorInterface */ |
|
8 | + public static $current; |
|
9 | + |
|
10 | + /** |
|
11 | + * @see TranslatorInterface |
|
12 | + */ |
|
13 | + public function noop($original) |
|
14 | + { |
|
15 | + return $original; |
|
16 | + } |
|
17 | + |
|
18 | + /** |
|
19 | + * @see TranslatorInterface |
|
20 | + */ |
|
21 | + public function register() |
|
22 | + { |
|
23 | + $previous = static::$current; |
|
24 | + |
|
25 | + static::$current = $this; |
|
26 | + |
|
27 | + static::includeFunctions(); |
|
28 | + |
|
29 | + return $previous; |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Include the gettext functions |
|
34 | + */ |
|
35 | + public static function includeFunctions() |
|
36 | + { |
|
37 | + include_once __DIR__.'/translator_functions.php'; |
|
38 | + } |
|
39 | 39 | } |
@@ -4,158 +4,158 @@ |
||
4 | 4 | |
5 | 5 | class GettextTranslator extends BaseTranslator implements TranslatorInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Constructor. Detects the current language using the environment variables. |
|
9 | - * |
|
10 | - * @param string $language |
|
11 | - */ |
|
12 | - public function __construct($language = null) |
|
13 | - { |
|
14 | - if (!function_exists('gettext')) { |
|
15 | - throw new \RuntimeException('This class require the gettext extension for PHP'); |
|
16 | - } |
|
17 | - |
|
18 | - //detects the language environment respecting the priority order |
|
19 | - //http://php.net/manual/en/function.gettext.php#114062 |
|
20 | - if (empty($language)) { |
|
21 | - $language = getenv('LANGUAGE') ?: getenv('LC_ALL') ?: getenv('LC_MESSAGES') ?: getenv('LANG'); |
|
22 | - } |
|
23 | - |
|
24 | - if (!empty($language)) { |
|
25 | - $this->setLanguage($language); |
|
26 | - } |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Define the current locale. |
|
31 | - * |
|
32 | - * @param string $language |
|
33 | - * @param int|null $category |
|
34 | - * |
|
35 | - * @return self |
|
36 | - */ |
|
37 | - public function setLanguage($language, $category = null) |
|
38 | - { |
|
39 | - if ($category === null) { |
|
40 | - $category = defined('LC_MESSAGES') ? LC_MESSAGES : LC_ALL; |
|
41 | - } |
|
42 | - |
|
43 | - setlocale($category, $language); |
|
44 | - putenv('LANGUAGE='.$language); |
|
45 | - |
|
46 | - return $this; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Loads a gettext domain. |
|
51 | - * |
|
52 | - * @param string $domain |
|
53 | - * @param string $path |
|
54 | - * @param bool $default |
|
55 | - * |
|
56 | - * @return self |
|
57 | - */ |
|
58 | - public function loadDomain($domain, $path = null, $default = true) |
|
59 | - { |
|
60 | - bindtextdomain($domain, $path); |
|
61 | - bind_textdomain_codeset($domain, 'UTF-8'); |
|
62 | - |
|
63 | - if ($default) { |
|
64 | - textdomain($domain); |
|
65 | - } |
|
66 | - |
|
67 | - return $this; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @see TranslatorInterface |
|
72 | - * |
|
73 | - * {@inheritdoc} |
|
74 | - */ |
|
75 | - public function gettext($original) |
|
76 | - { |
|
77 | - return gettext($original); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @see TranslatorInterface |
|
82 | - * |
|
83 | - * {@inheritdoc} |
|
84 | - */ |
|
85 | - public function ngettext($original, $plural, $value) |
|
86 | - { |
|
87 | - return ngettext($original, $plural, $value); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @see TranslatorInterface |
|
92 | - * |
|
93 | - * {@inheritdoc} |
|
94 | - */ |
|
95 | - public function dngettext($domain, $original, $plural, $value) |
|
96 | - { |
|
97 | - return dngettext($domain, $original, $plural, $value); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @see TranslatorInterface |
|
102 | - * |
|
103 | - * {@inheritdoc} |
|
104 | - */ |
|
105 | - public function npgettext($context, $original, $plural, $value) |
|
106 | - { |
|
107 | - $message = $context."\x04".$original; |
|
108 | - $translation = ngettext($message, $plural, $value); |
|
109 | - |
|
110 | - return ($translation === $message) ? $original : $translation; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @see TranslatorInterface |
|
115 | - * |
|
116 | - * {@inheritdoc} |
|
117 | - */ |
|
118 | - public function pgettext($context, $original) |
|
119 | - { |
|
120 | - $message = $context."\x04".$original; |
|
121 | - $translation = gettext($message); |
|
122 | - |
|
123 | - return ($translation === $message) ? $original : $translation; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @see TranslatorInterface |
|
128 | - * |
|
129 | - * {@inheritdoc} |
|
130 | - */ |
|
131 | - public function dgettext($domain, $original) |
|
132 | - { |
|
133 | - return dgettext($domain, $original); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * @see TranslatorInterface |
|
138 | - * |
|
139 | - * {@inheritdoc} |
|
140 | - */ |
|
141 | - public function dpgettext($domain, $context, $original) |
|
142 | - { |
|
143 | - $message = $context."\x04".$original; |
|
144 | - $translation = dgettext($domain, $message); |
|
145 | - |
|
146 | - return ($translation === $message) ? $original : $translation; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @see TranslatorInterface |
|
151 | - * |
|
152 | - * {@inheritdoc} |
|
153 | - */ |
|
154 | - public function dnpgettext($domain, $context, $original, $plural, $value) |
|
155 | - { |
|
156 | - $message = $context."\x04".$original; |
|
157 | - $translation = dngettext($domain, $message, $plural, $value); |
|
158 | - |
|
159 | - return ($translation === $message) ? $original : $translation; |
|
160 | - } |
|
7 | + /** |
|
8 | + * Constructor. Detects the current language using the environment variables. |
|
9 | + * |
|
10 | + * @param string $language |
|
11 | + */ |
|
12 | + public function __construct($language = null) |
|
13 | + { |
|
14 | + if (!function_exists('gettext')) { |
|
15 | + throw new \RuntimeException('This class require the gettext extension for PHP'); |
|
16 | + } |
|
17 | + |
|
18 | + //detects the language environment respecting the priority order |
|
19 | + //http://php.net/manual/en/function.gettext.php#114062 |
|
20 | + if (empty($language)) { |
|
21 | + $language = getenv('LANGUAGE') ?: getenv('LC_ALL') ?: getenv('LC_MESSAGES') ?: getenv('LANG'); |
|
22 | + } |
|
23 | + |
|
24 | + if (!empty($language)) { |
|
25 | + $this->setLanguage($language); |
|
26 | + } |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Define the current locale. |
|
31 | + * |
|
32 | + * @param string $language |
|
33 | + * @param int|null $category |
|
34 | + * |
|
35 | + * @return self |
|
36 | + */ |
|
37 | + public function setLanguage($language, $category = null) |
|
38 | + { |
|
39 | + if ($category === null) { |
|
40 | + $category = defined('LC_MESSAGES') ? LC_MESSAGES : LC_ALL; |
|
41 | + } |
|
42 | + |
|
43 | + setlocale($category, $language); |
|
44 | + putenv('LANGUAGE='.$language); |
|
45 | + |
|
46 | + return $this; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Loads a gettext domain. |
|
51 | + * |
|
52 | + * @param string $domain |
|
53 | + * @param string $path |
|
54 | + * @param bool $default |
|
55 | + * |
|
56 | + * @return self |
|
57 | + */ |
|
58 | + public function loadDomain($domain, $path = null, $default = true) |
|
59 | + { |
|
60 | + bindtextdomain($domain, $path); |
|
61 | + bind_textdomain_codeset($domain, 'UTF-8'); |
|
62 | + |
|
63 | + if ($default) { |
|
64 | + textdomain($domain); |
|
65 | + } |
|
66 | + |
|
67 | + return $this; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @see TranslatorInterface |
|
72 | + * |
|
73 | + * {@inheritdoc} |
|
74 | + */ |
|
75 | + public function gettext($original) |
|
76 | + { |
|
77 | + return gettext($original); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @see TranslatorInterface |
|
82 | + * |
|
83 | + * {@inheritdoc} |
|
84 | + */ |
|
85 | + public function ngettext($original, $plural, $value) |
|
86 | + { |
|
87 | + return ngettext($original, $plural, $value); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @see TranslatorInterface |
|
92 | + * |
|
93 | + * {@inheritdoc} |
|
94 | + */ |
|
95 | + public function dngettext($domain, $original, $plural, $value) |
|
96 | + { |
|
97 | + return dngettext($domain, $original, $plural, $value); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @see TranslatorInterface |
|
102 | + * |
|
103 | + * {@inheritdoc} |
|
104 | + */ |
|
105 | + public function npgettext($context, $original, $plural, $value) |
|
106 | + { |
|
107 | + $message = $context."\x04".$original; |
|
108 | + $translation = ngettext($message, $plural, $value); |
|
109 | + |
|
110 | + return ($translation === $message) ? $original : $translation; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @see TranslatorInterface |
|
115 | + * |
|
116 | + * {@inheritdoc} |
|
117 | + */ |
|
118 | + public function pgettext($context, $original) |
|
119 | + { |
|
120 | + $message = $context."\x04".$original; |
|
121 | + $translation = gettext($message); |
|
122 | + |
|
123 | + return ($translation === $message) ? $original : $translation; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @see TranslatorInterface |
|
128 | + * |
|
129 | + * {@inheritdoc} |
|
130 | + */ |
|
131 | + public function dgettext($domain, $original) |
|
132 | + { |
|
133 | + return dgettext($domain, $original); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * @see TranslatorInterface |
|
138 | + * |
|
139 | + * {@inheritdoc} |
|
140 | + */ |
|
141 | + public function dpgettext($domain, $context, $original) |
|
142 | + { |
|
143 | + $message = $context."\x04".$original; |
|
144 | + $translation = dgettext($domain, $message); |
|
145 | + |
|
146 | + return ($translation === $message) ? $original : $translation; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @see TranslatorInterface |
|
151 | + * |
|
152 | + * {@inheritdoc} |
|
153 | + */ |
|
154 | + public function dnpgettext($domain, $context, $original, $plural, $value) |
|
155 | + { |
|
156 | + $message = $context."\x04".$original; |
|
157 | + $translation = dngettext($domain, $message, $plural, $value); |
|
158 | + |
|
159 | + return ($translation === $message) ? $original : $translation; |
|
160 | + } |
|
161 | 161 | } |
@@ -9,21 +9,21 @@ |
||
9 | 9 | */ |
10 | 10 | trait HeadersGeneratorTrait |
11 | 11 | { |
12 | - /** |
|
13 | - * Returns the headers as a string. |
|
14 | - * |
|
15 | - * @param Translations $translations |
|
16 | - * |
|
17 | - * @return string |
|
18 | - */ |
|
19 | - protected static function generateHeaders(Translations $translations) |
|
20 | - { |
|
21 | - $headers = ''; |
|
12 | + /** |
|
13 | + * Returns the headers as a string. |
|
14 | + * |
|
15 | + * @param Translations $translations |
|
16 | + * |
|
17 | + * @return string |
|
18 | + */ |
|
19 | + protected static function generateHeaders(Translations $translations) |
|
20 | + { |
|
21 | + $headers = ''; |
|
22 | 22 | |
23 | - foreach ($translations->getHeaders() as $name => $value) { |
|
24 | - $headers .= sprintf("%s: %s\n", $name, $value); |
|
25 | - } |
|
23 | + foreach ($translations->getHeaders() as $name => $value) { |
|
24 | + $headers .= sprintf("%s: %s\n", $name, $value); |
|
25 | + } |
|
26 | 26 | |
27 | - return $headers; |
|
28 | - } |
|
27 | + return $headers; |
|
28 | + } |
|
29 | 29 | } |
@@ -9,51 +9,51 @@ |
||
9 | 9 | */ |
10 | 10 | trait DictionaryTrait |
11 | 11 | { |
12 | - use HeadersGeneratorTrait; |
|
13 | - use HeadersExtractorTrait; |
|
14 | - |
|
15 | - /** |
|
16 | - * Returns a plain dictionary with the format [original => translation]. |
|
17 | - * |
|
18 | - * @param Translations $translations |
|
19 | - * @param bool $includeHeaders |
|
20 | - * |
|
21 | - * @return array |
|
22 | - */ |
|
23 | - protected static function toArray(Translations $translations, $includeHeaders) |
|
24 | - { |
|
25 | - $messages = []; |
|
26 | - |
|
27 | - if ($includeHeaders) { |
|
28 | - $messages[''] = static::generateHeaders($translations); |
|
29 | - } |
|
30 | - |
|
31 | - foreach ($translations as $translation) { |
|
32 | - if ($translation->isDisabled()) { |
|
33 | - continue; |
|
34 | - } |
|
35 | - |
|
36 | - $messages[$translation->getOriginal()] = $translation->getTranslation(); |
|
37 | - } |
|
38 | - |
|
39 | - return $messages; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Extract the entries from a dictionary. |
|
44 | - * |
|
45 | - * @param array $messages |
|
46 | - * @param Translations $translations |
|
47 | - */ |
|
48 | - protected static function fromArray(array $messages, Translations $translations) |
|
49 | - { |
|
50 | - foreach ($messages as $original => $translation) { |
|
51 | - if ($original === '') { |
|
52 | - static::extractHeaders($translation, $translations); |
|
53 | - continue; |
|
54 | - } |
|
55 | - |
|
56 | - $translations->insert(null, $original)->setTranslation($translation); |
|
57 | - } |
|
58 | - } |
|
12 | + use HeadersGeneratorTrait; |
|
13 | + use HeadersExtractorTrait; |
|
14 | + |
|
15 | + /** |
|
16 | + * Returns a plain dictionary with the format [original => translation]. |
|
17 | + * |
|
18 | + * @param Translations $translations |
|
19 | + * @param bool $includeHeaders |
|
20 | + * |
|
21 | + * @return array |
|
22 | + */ |
|
23 | + protected static function toArray(Translations $translations, $includeHeaders) |
|
24 | + { |
|
25 | + $messages = []; |
|
26 | + |
|
27 | + if ($includeHeaders) { |
|
28 | + $messages[''] = static::generateHeaders($translations); |
|
29 | + } |
|
30 | + |
|
31 | + foreach ($translations as $translation) { |
|
32 | + if ($translation->isDisabled()) { |
|
33 | + continue; |
|
34 | + } |
|
35 | + |
|
36 | + $messages[$translation->getOriginal()] = $translation->getTranslation(); |
|
37 | + } |
|
38 | + |
|
39 | + return $messages; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Extract the entries from a dictionary. |
|
44 | + * |
|
45 | + * @param array $messages |
|
46 | + * @param Translations $translations |
|
47 | + */ |
|
48 | + protected static function fromArray(array $messages, Translations $translations) |
|
49 | + { |
|
50 | + foreach ($messages as $original => $translation) { |
|
51 | + if ($original === '') { |
|
52 | + static::extractHeaders($translation, $translations); |
|
53 | + continue; |
|
54 | + } |
|
55 | + |
|
56 | + $translations->insert(null, $original)->setTranslation($translation); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | } |