@@ -6,265 +6,265 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Translator extends BaseTranslator implements TranslatorInterface |
| 8 | 8 | { |
| 9 | - protected $domain; |
|
| 10 | - protected $dictionary = []; |
|
| 11 | - protected $plurals = []; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * Loads translation from a Translations instance, a file on an array. |
|
| 15 | - * |
|
| 16 | - * @param Translations|string|array $translations |
|
| 17 | - * |
|
| 18 | - * @return static |
|
| 19 | - */ |
|
| 20 | - public function loadTranslations($translations) |
|
| 21 | - { |
|
| 22 | - if (is_object($translations) && $translations instanceof Translations) { |
|
| 23 | - $translations = PhpArray::generate($translations, ['includeHeaders' => false]); |
|
| 24 | - } elseif (is_string($translations) && is_file($translations)) { |
|
| 25 | - $translations = include $translations; |
|
| 26 | - } elseif (!is_array($translations)) { |
|
| 27 | - throw new \InvalidArgumentException( |
|
| 28 | - 'Invalid Translator: only arrays, files or instance of Translations are allowed' |
|
| 29 | - ); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - $this->addTranslations($translations); |
|
| 33 | - |
|
| 34 | - return $this; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Set the default domain. |
|
| 39 | - * |
|
| 40 | - * @param string $domain |
|
| 41 | - * |
|
| 42 | - * @return static |
|
| 43 | - */ |
|
| 44 | - public function defaultDomain($domain) |
|
| 45 | - { |
|
| 46 | - $this->domain = $domain; |
|
| 47 | - |
|
| 48 | - return $this; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @see TranslatorInterface |
|
| 53 | - * |
|
| 54 | - * {@inheritdoc} |
|
| 55 | - */ |
|
| 56 | - public function gettext($original) |
|
| 57 | - { |
|
| 58 | - return $this->dpgettext($this->domain, null, $original); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @see TranslatorInterface |
|
| 63 | - * |
|
| 64 | - * {@inheritdoc} |
|
| 65 | - */ |
|
| 66 | - public function ngettext($original, $plural, $value) |
|
| 67 | - { |
|
| 68 | - return $this->dnpgettext($this->domain, null, $original, $plural, $value); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @see TranslatorInterface |
|
| 73 | - * |
|
| 74 | - * {@inheritdoc} |
|
| 75 | - */ |
|
| 76 | - public function dngettext($domain, $original, $plural, $value) |
|
| 77 | - { |
|
| 78 | - return $this->dnpgettext($domain, null, $original, $plural, $value); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @see TranslatorInterface |
|
| 83 | - * |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - public function npgettext($context, $original, $plural, $value) |
|
| 87 | - { |
|
| 88 | - return $this->dnpgettext($this->domain, $context, $original, $plural, $value); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @see TranslatorInterface |
|
| 93 | - * |
|
| 94 | - * {@inheritdoc} |
|
| 95 | - */ |
|
| 96 | - public function pgettext($context, $original) |
|
| 97 | - { |
|
| 98 | - return $this->dpgettext($this->domain, $context, $original); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @see TranslatorInterface |
|
| 103 | - * |
|
| 104 | - * {@inheritdoc} |
|
| 105 | - */ |
|
| 106 | - public function dgettext($domain, $original) |
|
| 107 | - { |
|
| 108 | - return $this->dpgettext($domain, null, $original); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @see TranslatorInterface |
|
| 113 | - * |
|
| 114 | - * {@inheritdoc} |
|
| 115 | - */ |
|
| 116 | - public function dpgettext($domain, $context, $original) |
|
| 117 | - { |
|
| 118 | - $translation = $this->getTranslation($domain, $context, $original); |
|
| 119 | - |
|
| 120 | - if (isset($translation[0]) && $translation[0] !== '') { |
|
| 121 | - return $translation[0]; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $original; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @see TranslatorInterface |
|
| 129 | - * |
|
| 130 | - * {@inheritdoc} |
|
| 131 | - */ |
|
| 132 | - public function dnpgettext($domain, $context, $original, $plural, $value) |
|
| 133 | - { |
|
| 134 | - $translation = $this->getTranslation($domain, $context, $original); |
|
| 135 | - $key = $this->getPluralIndex($domain, $value, $translation === false); |
|
| 136 | - |
|
| 137 | - if (isset($translation[$key]) && $translation[$key] !== '') { |
|
| 138 | - return $translation[$key]; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return ($key === 0) ? $original : $plural; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Set new translations to the dictionary. |
|
| 146 | - * |
|
| 147 | - * @param array $translations |
|
| 148 | - */ |
|
| 149 | - protected function addTranslations(array $translations) |
|
| 150 | - { |
|
| 151 | - $domain = isset($translations['domain']) ? $translations['domain'] : ''; |
|
| 152 | - |
|
| 153 | - //Set the first domain loaded as default domain |
|
| 154 | - if ($this->domain === null) { |
|
| 155 | - $this->domain = $domain; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - if (isset($this->dictionary[$domain])) { |
|
| 159 | - $this->dictionary[$domain] = array_replace_recursive($this->dictionary[$domain], $translations['messages']); |
|
| 160 | - |
|
| 161 | - return; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - if (!empty($translations['plural-forms'])) { |
|
| 165 | - list($count, $code) = array_map('trim', explode(';', $translations['plural-forms'], 2)); |
|
| 166 | - |
|
| 167 | - // extract just the expression turn 'n' into a php variable '$n'. |
|
| 168 | - // Slap on a return keyword and semicolon at the end. |
|
| 169 | - $this->plurals[$domain] = [ |
|
| 170 | - 'count' => (int) str_replace('nplurals=', '', $count), |
|
| 171 | - 'code' => str_replace('plural=', 'return ', str_replace('n', '$n', $code)).';', |
|
| 172 | - ]; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $this->dictionary[$domain] = $translations['messages']; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Search and returns a translation. |
|
| 180 | - * |
|
| 181 | - * @param string $domain |
|
| 182 | - * @param string $context |
|
| 183 | - * @param string $original |
|
| 184 | - * |
|
| 185 | - * @return string|false |
|
| 186 | - */ |
|
| 187 | - protected function getTranslation($domain, $context, $original) |
|
| 188 | - { |
|
| 189 | - return isset($this->dictionary[$domain][$context][$original]) |
|
| 190 | - ? $this->dictionary[$domain][$context][$original] |
|
| 191 | - : false; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Executes the plural decision code given the number to decide which |
|
| 196 | - * plural version to take. |
|
| 197 | - * |
|
| 198 | - * @param string $domain |
|
| 199 | - * @param string $n |
|
| 200 | - * @param bool $fallback set to true to get fallback plural index |
|
| 201 | - * |
|
| 202 | - * @return int |
|
| 203 | - */ |
|
| 204 | - protected function getPluralIndex($domain, $n, $fallback) |
|
| 205 | - { |
|
| 206 | - //Not loaded domain or translation, use a fallback |
|
| 207 | - if (!isset($this->plurals[$domain]) || $fallback === true) { |
|
| 208 | - return $n == 1 ? 0 : 1; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if (!isset($this->plurals[$domain]['function'])) { |
|
| 212 | - $code = static::fixTerseIfs($this->plurals[$domain]['code']); |
|
| 213 | - $this->plurals[$domain]['function'] = eval("return function (\$n) { $code };"); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if ($this->plurals[$domain]['count'] <= 2) { |
|
| 217 | - return call_user_func($this->plurals[$domain]['function'], $n) ? 1 : 0; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return call_user_func($this->plurals[$domain]['function'], $n); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * This function will recursively wrap failure states in brackets if they contain a nested terse if. |
|
| 225 | - * |
|
| 226 | - * This because PHP can not handle nested terse if's unless they are wrapped in brackets. |
|
| 227 | - * |
|
| 228 | - * This code probably only works for the gettext plural decision codes. |
|
| 229 | - * |
|
| 230 | - * return ($n==1 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2); |
|
| 231 | - * becomes |
|
| 232 | - * return ($n==1 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2)); |
|
| 233 | - * |
|
| 234 | - * @param string $code the terse if string |
|
| 235 | - * @param bool $inner If inner is true we wrap it in brackets |
|
| 236 | - * |
|
| 237 | - * @return string A formatted terse If that PHP can work with. |
|
| 238 | - */ |
|
| 239 | - private static function fixTerseIfs($code, $inner = false) |
|
| 240 | - { |
|
| 241 | - /* |
|
| 9 | + protected $domain; |
|
| 10 | + protected $dictionary = []; |
|
| 11 | + protected $plurals = []; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * Loads translation from a Translations instance, a file on an array. |
|
| 15 | + * |
|
| 16 | + * @param Translations|string|array $translations |
|
| 17 | + * |
|
| 18 | + * @return static |
|
| 19 | + */ |
|
| 20 | + public function loadTranslations($translations) |
|
| 21 | + { |
|
| 22 | + if (is_object($translations) && $translations instanceof Translations) { |
|
| 23 | + $translations = PhpArray::generate($translations, ['includeHeaders' => false]); |
|
| 24 | + } elseif (is_string($translations) && is_file($translations)) { |
|
| 25 | + $translations = include $translations; |
|
| 26 | + } elseif (!is_array($translations)) { |
|
| 27 | + throw new \InvalidArgumentException( |
|
| 28 | + 'Invalid Translator: only arrays, files or instance of Translations are allowed' |
|
| 29 | + ); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + $this->addTranslations($translations); |
|
| 33 | + |
|
| 34 | + return $this; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Set the default domain. |
|
| 39 | + * |
|
| 40 | + * @param string $domain |
|
| 41 | + * |
|
| 42 | + * @return static |
|
| 43 | + */ |
|
| 44 | + public function defaultDomain($domain) |
|
| 45 | + { |
|
| 46 | + $this->domain = $domain; |
|
| 47 | + |
|
| 48 | + return $this; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @see TranslatorInterface |
|
| 53 | + * |
|
| 54 | + * {@inheritdoc} |
|
| 55 | + */ |
|
| 56 | + public function gettext($original) |
|
| 57 | + { |
|
| 58 | + return $this->dpgettext($this->domain, null, $original); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @see TranslatorInterface |
|
| 63 | + * |
|
| 64 | + * {@inheritdoc} |
|
| 65 | + */ |
|
| 66 | + public function ngettext($original, $plural, $value) |
|
| 67 | + { |
|
| 68 | + return $this->dnpgettext($this->domain, null, $original, $plural, $value); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @see TranslatorInterface |
|
| 73 | + * |
|
| 74 | + * {@inheritdoc} |
|
| 75 | + */ |
|
| 76 | + public function dngettext($domain, $original, $plural, $value) |
|
| 77 | + { |
|
| 78 | + return $this->dnpgettext($domain, null, $original, $plural, $value); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @see TranslatorInterface |
|
| 83 | + * |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + public function npgettext($context, $original, $plural, $value) |
|
| 87 | + { |
|
| 88 | + return $this->dnpgettext($this->domain, $context, $original, $plural, $value); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @see TranslatorInterface |
|
| 93 | + * |
|
| 94 | + * {@inheritdoc} |
|
| 95 | + */ |
|
| 96 | + public function pgettext($context, $original) |
|
| 97 | + { |
|
| 98 | + return $this->dpgettext($this->domain, $context, $original); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @see TranslatorInterface |
|
| 103 | + * |
|
| 104 | + * {@inheritdoc} |
|
| 105 | + */ |
|
| 106 | + public function dgettext($domain, $original) |
|
| 107 | + { |
|
| 108 | + return $this->dpgettext($domain, null, $original); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @see TranslatorInterface |
|
| 113 | + * |
|
| 114 | + * {@inheritdoc} |
|
| 115 | + */ |
|
| 116 | + public function dpgettext($domain, $context, $original) |
|
| 117 | + { |
|
| 118 | + $translation = $this->getTranslation($domain, $context, $original); |
|
| 119 | + |
|
| 120 | + if (isset($translation[0]) && $translation[0] !== '') { |
|
| 121 | + return $translation[0]; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $original; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @see TranslatorInterface |
|
| 129 | + * |
|
| 130 | + * {@inheritdoc} |
|
| 131 | + */ |
|
| 132 | + public function dnpgettext($domain, $context, $original, $plural, $value) |
|
| 133 | + { |
|
| 134 | + $translation = $this->getTranslation($domain, $context, $original); |
|
| 135 | + $key = $this->getPluralIndex($domain, $value, $translation === false); |
|
| 136 | + |
|
| 137 | + if (isset($translation[$key]) && $translation[$key] !== '') { |
|
| 138 | + return $translation[$key]; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return ($key === 0) ? $original : $plural; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Set new translations to the dictionary. |
|
| 146 | + * |
|
| 147 | + * @param array $translations |
|
| 148 | + */ |
|
| 149 | + protected function addTranslations(array $translations) |
|
| 150 | + { |
|
| 151 | + $domain = isset($translations['domain']) ? $translations['domain'] : ''; |
|
| 152 | + |
|
| 153 | + //Set the first domain loaded as default domain |
|
| 154 | + if ($this->domain === null) { |
|
| 155 | + $this->domain = $domain; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + if (isset($this->dictionary[$domain])) { |
|
| 159 | + $this->dictionary[$domain] = array_replace_recursive($this->dictionary[$domain], $translations['messages']); |
|
| 160 | + |
|
| 161 | + return; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + if (!empty($translations['plural-forms'])) { |
|
| 165 | + list($count, $code) = array_map('trim', explode(';', $translations['plural-forms'], 2)); |
|
| 166 | + |
|
| 167 | + // extract just the expression turn 'n' into a php variable '$n'. |
|
| 168 | + // Slap on a return keyword and semicolon at the end. |
|
| 169 | + $this->plurals[$domain] = [ |
|
| 170 | + 'count' => (int) str_replace('nplurals=', '', $count), |
|
| 171 | + 'code' => str_replace('plural=', 'return ', str_replace('n', '$n', $code)).';', |
|
| 172 | + ]; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $this->dictionary[$domain] = $translations['messages']; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Search and returns a translation. |
|
| 180 | + * |
|
| 181 | + * @param string $domain |
|
| 182 | + * @param string $context |
|
| 183 | + * @param string $original |
|
| 184 | + * |
|
| 185 | + * @return string|false |
|
| 186 | + */ |
|
| 187 | + protected function getTranslation($domain, $context, $original) |
|
| 188 | + { |
|
| 189 | + return isset($this->dictionary[$domain][$context][$original]) |
|
| 190 | + ? $this->dictionary[$domain][$context][$original] |
|
| 191 | + : false; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Executes the plural decision code given the number to decide which |
|
| 196 | + * plural version to take. |
|
| 197 | + * |
|
| 198 | + * @param string $domain |
|
| 199 | + * @param string $n |
|
| 200 | + * @param bool $fallback set to true to get fallback plural index |
|
| 201 | + * |
|
| 202 | + * @return int |
|
| 203 | + */ |
|
| 204 | + protected function getPluralIndex($domain, $n, $fallback) |
|
| 205 | + { |
|
| 206 | + //Not loaded domain or translation, use a fallback |
|
| 207 | + if (!isset($this->plurals[$domain]) || $fallback === true) { |
|
| 208 | + return $n == 1 ? 0 : 1; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if (!isset($this->plurals[$domain]['function'])) { |
|
| 212 | + $code = static::fixTerseIfs($this->plurals[$domain]['code']); |
|
| 213 | + $this->plurals[$domain]['function'] = eval("return function (\$n) { $code };"); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if ($this->plurals[$domain]['count'] <= 2) { |
|
| 217 | + return call_user_func($this->plurals[$domain]['function'], $n) ? 1 : 0; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return call_user_func($this->plurals[$domain]['function'], $n); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * This function will recursively wrap failure states in brackets if they contain a nested terse if. |
|
| 225 | + * |
|
| 226 | + * This because PHP can not handle nested terse if's unless they are wrapped in brackets. |
|
| 227 | + * |
|
| 228 | + * This code probably only works for the gettext plural decision codes. |
|
| 229 | + * |
|
| 230 | + * return ($n==1 ? 0 : $n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2); |
|
| 231 | + * becomes |
|
| 232 | + * return ($n==1 ? 0 : ($n%10>=2 && $n%10<=4 && ($n%100<10 || $n%100>=20) ? 1 : 2)); |
|
| 233 | + * |
|
| 234 | + * @param string $code the terse if string |
|
| 235 | + * @param bool $inner If inner is true we wrap it in brackets |
|
| 236 | + * |
|
| 237 | + * @return string A formatted terse If that PHP can work with. |
|
| 238 | + */ |
|
| 239 | + private static function fixTerseIfs($code, $inner = false) |
|
| 240 | + { |
|
| 241 | + /* |
|
| 242 | 242 | * (?P<expression>[^?]+) Capture everything up to ? as 'expression' |
| 243 | 243 | * \? ? |
| 244 | 244 | * (?P<success>[^:]+) Capture everything up to : as 'success' |
| 245 | 245 | * : : |
| 246 | 246 | * (?P<failure>[^;]+) Capture everything up to ; as 'failure' |
| 247 | 247 | */ |
| 248 | - preg_match('/(?P<expression>[^?]+)\?(?P<success>[^:]+):(?P<failure>[^;]+)/', $code, $matches); |
|
| 248 | + preg_match('/(?P<expression>[^?]+)\?(?P<success>[^:]+):(?P<failure>[^;]+)/', $code, $matches); |
|
| 249 | 249 | |
| 250 | - // If no match was found then no terse if was present |
|
| 251 | - if (!isset($matches[0])) { |
|
| 252 | - return $code; |
|
| 253 | - } |
|
| 250 | + // If no match was found then no terse if was present |
|
| 251 | + if (!isset($matches[0])) { |
|
| 252 | + return $code; |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - $expression = $matches['expression']; |
|
| 256 | - $success = $matches['success']; |
|
| 257 | - $failure = $matches['failure']; |
|
| 255 | + $expression = $matches['expression']; |
|
| 256 | + $success = $matches['success']; |
|
| 257 | + $failure = $matches['failure']; |
|
| 258 | 258 | |
| 259 | - // Go look for another terse if in the failure state. |
|
| 260 | - $failure = static::fixTerseIfs($failure, true); |
|
| 261 | - $code = $expression.' ? '.$success.' : '.$failure; |
|
| 259 | + // Go look for another terse if in the failure state. |
|
| 260 | + $failure = static::fixTerseIfs($failure, true); |
|
| 261 | + $code = $expression.' ? '.$success.' : '.$failure; |
|
| 262 | 262 | |
| 263 | - if ($inner) { |
|
| 264 | - return "($code)"; |
|
| 265 | - } |
|
| 263 | + if ($inner) { |
|
| 264 | + return "($code)"; |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - // note the semicolon. We need that for executing the code. |
|
| 268 | - return "$code;"; |
|
| 269 | - } |
|
| 267 | + // note the semicolon. We need that for executing the code. |
|
| 268 | + return "$code;"; |
|
| 269 | + } |
|
| 270 | 270 | } |
@@ -11,23 +11,23 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class PhpArray extends Extractor implements ExtractorInterface |
| 13 | 13 | { |
| 14 | - use MultidimensionalArrayTrait; |
|
| 14 | + use MultidimensionalArrayTrait; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public static function fromFile($file, Translations $translations, array $options = []) |
|
| 20 | - { |
|
| 21 | - foreach (static::getFiles($file) as $file) { |
|
| 22 | - static::fromArray(include($file), $translations); |
|
| 23 | - } |
|
| 24 | - } |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public static function fromFile($file, Translations $translations, array $options = []) |
|
| 20 | + { |
|
| 21 | + foreach (static::getFiles($file) as $file) { |
|
| 22 | + static::fromArray(include($file), $translations); |
|
| 23 | + } |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * {@inheritdoc} |
|
| 28 | - */ |
|
| 29 | - public static function fromString($string, Translations $translations, array $options = []) |
|
| 30 | - { |
|
| 31 | - throw new BadMethodCallException('PhpArray::fromString() cannot be called. Use PhpArray::fromFile()'); |
|
| 32 | - } |
|
| 26 | + /** |
|
| 27 | + * {@inheritdoc} |
|
| 28 | + */ |
|
| 29 | + public static function fromString($string, Translations $translations, array $options = []) |
|
| 30 | + { |
|
| 31 | + throw new BadMethodCallException('PhpArray::fromString() cannot be called. Use PhpArray::fromFile()'); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -11,37 +11,37 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class CsvDictionary extends Extractor implements ExtractorInterface |
| 13 | 13 | { |
| 14 | - use HeadersExtractorTrait; |
|
| 15 | - use CsvTrait; |
|
| 16 | - |
|
| 17 | - public static $options = [ |
|
| 18 | - 'delimiter' => ",", |
|
| 19 | - 'enclosure' => '"', |
|
| 20 | - 'escape_char' => "\\" |
|
| 21 | - ]; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * {@inheritdoc} |
|
| 25 | - */ |
|
| 26 | - public static function fromString($string, Translations $translations, array $options = []) |
|
| 27 | - { |
|
| 28 | - $options += static::$options; |
|
| 29 | - $handle = fopen('php://memory', 'w'); |
|
| 30 | - |
|
| 31 | - fputs($handle, $string); |
|
| 32 | - rewind($handle); |
|
| 33 | - |
|
| 34 | - while ($row = static::fgetcsv($handle, $options)) { |
|
| 35 | - list($original, $translation) = $row + ['', '']; |
|
| 36 | - |
|
| 37 | - if ($original === '') { |
|
| 38 | - static::extractHeaders($translation, $translations); |
|
| 39 | - continue; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - $translations->insert(null, $original)->setTranslation($translation); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - fclose($handle); |
|
| 46 | - } |
|
| 14 | + use HeadersExtractorTrait; |
|
| 15 | + use CsvTrait; |
|
| 16 | + |
|
| 17 | + public static $options = [ |
|
| 18 | + 'delimiter' => ",", |
|
| 19 | + 'enclosure' => '"', |
|
| 20 | + 'escape_char' => "\\" |
|
| 21 | + ]; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * {@inheritdoc} |
|
| 25 | + */ |
|
| 26 | + public static function fromString($string, Translations $translations, array $options = []) |
|
| 27 | + { |
|
| 28 | + $options += static::$options; |
|
| 29 | + $handle = fopen('php://memory', 'w'); |
|
| 30 | + |
|
| 31 | + fputs($handle, $string); |
|
| 32 | + rewind($handle); |
|
| 33 | + |
|
| 34 | + while ($row = static::fgetcsv($handle, $options)) { |
|
| 35 | + list($original, $translation) = $row + ['', '']; |
|
| 36 | + |
|
| 37 | + if ($original === '') { |
|
| 38 | + static::extractHeaders($translation, $translations); |
|
| 39 | + continue; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + $translations->insert(null, $original)->setTranslation($translation); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + fclose($handle); |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -11,64 +11,64 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class JsCode extends Extractor implements ExtractorInterface, ExtractorMultiInterface |
| 13 | 13 | { |
| 14 | - public static $options = [ |
|
| 15 | - 'constants' => [], |
|
| 14 | + public static $options = [ |
|
| 15 | + 'constants' => [], |
|
| 16 | 16 | |
| 17 | - 'functions' => [ |
|
| 18 | - 'gettext' => 'gettext', |
|
| 19 | - '__' => 'gettext', |
|
| 20 | - 'ngettext' => 'ngettext', |
|
| 21 | - 'n__' => 'ngettext', |
|
| 22 | - 'pgettext' => 'pgettext', |
|
| 23 | - 'p__' => 'pgettext', |
|
| 24 | - 'dgettext' => 'dgettext', |
|
| 25 | - 'd__' => 'dgettext', |
|
| 26 | - 'dngettext' => 'dngettext', |
|
| 27 | - 'dn__' => 'dngettext', |
|
| 28 | - 'dpgettext' => 'dpgettext', |
|
| 29 | - 'dp__' => 'dpgettext', |
|
| 30 | - 'npgettext' => 'npgettext', |
|
| 31 | - 'np__' => 'npgettext', |
|
| 32 | - 'dnpgettext' => 'dnpgettext', |
|
| 33 | - 'dnp__' => 'dnpgettext', |
|
| 34 | - 'noop' => 'noop', |
|
| 35 | - 'noop__' => 'noop', |
|
| 36 | - ], |
|
| 37 | - ]; |
|
| 17 | + 'functions' => [ |
|
| 18 | + 'gettext' => 'gettext', |
|
| 19 | + '__' => 'gettext', |
|
| 20 | + 'ngettext' => 'ngettext', |
|
| 21 | + 'n__' => 'ngettext', |
|
| 22 | + 'pgettext' => 'pgettext', |
|
| 23 | + 'p__' => 'pgettext', |
|
| 24 | + 'dgettext' => 'dgettext', |
|
| 25 | + 'd__' => 'dgettext', |
|
| 26 | + 'dngettext' => 'dngettext', |
|
| 27 | + 'dn__' => 'dngettext', |
|
| 28 | + 'dpgettext' => 'dpgettext', |
|
| 29 | + 'dp__' => 'dpgettext', |
|
| 30 | + 'npgettext' => 'npgettext', |
|
| 31 | + 'np__' => 'npgettext', |
|
| 32 | + 'dnpgettext' => 'dnpgettext', |
|
| 33 | + 'dnp__' => 'dnpgettext', |
|
| 34 | + 'noop' => 'noop', |
|
| 35 | + 'noop__' => 'noop', |
|
| 36 | + ], |
|
| 37 | + ]; |
|
| 38 | 38 | |
| 39 | - protected static $functionsScannerClass = 'Gettext\Utils\JsFunctionsScanner'; |
|
| 39 | + protected static $functionsScannerClass = 'Gettext\Utils\JsFunctionsScanner'; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @inheritdoc |
|
| 43 | - * @throws Exception |
|
| 44 | - */ |
|
| 45 | - public static function fromString($string, Translations $translations, array $options = []) |
|
| 46 | - { |
|
| 47 | - static::fromStringMultiple($string, [$translations], $options); |
|
| 48 | - } |
|
| 41 | + /** |
|
| 42 | + * @inheritdoc |
|
| 43 | + * @throws Exception |
|
| 44 | + */ |
|
| 45 | + public static function fromString($string, Translations $translations, array $options = []) |
|
| 46 | + { |
|
| 47 | + static::fromStringMultiple($string, [$translations], $options); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @inheritDoc |
|
| 52 | - * @throws Exception |
|
| 53 | - */ |
|
| 54 | - public static function fromStringMultiple($string, array $translations, array $options = []) |
|
| 55 | - { |
|
| 56 | - $options += static::$options; |
|
| 50 | + /** |
|
| 51 | + * @inheritDoc |
|
| 52 | + * @throws Exception |
|
| 53 | + */ |
|
| 54 | + public static function fromStringMultiple($string, array $translations, array $options = []) |
|
| 55 | + { |
|
| 56 | + $options += static::$options; |
|
| 57 | 57 | |
| 58 | - /** @var FunctionsScanner $functions */ |
|
| 59 | - $functions = new static::$functionsScannerClass($string); |
|
| 60 | - $functions->saveGettextFunctions($translations, $options); |
|
| 61 | - } |
|
| 58 | + /** @var FunctionsScanner $functions */ |
|
| 59 | + $functions = new static::$functionsScannerClass($string); |
|
| 60 | + $functions->saveGettextFunctions($translations, $options); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @inheritDoc |
|
| 65 | - * @throws Exception |
|
| 66 | - */ |
|
| 67 | - public static function fromFileMultiple($file, array $translations, array $options = []) |
|
| 68 | - { |
|
| 69 | - foreach (static::getFiles($file) as $file) { |
|
| 70 | - $options['file'] = $file; |
|
| 71 | - static::fromStringMultiple(static::readFile($file), $translations, $options); |
|
| 72 | - } |
|
| 73 | - } |
|
| 63 | + /** |
|
| 64 | + * @inheritDoc |
|
| 65 | + * @throws Exception |
|
| 66 | + */ |
|
| 67 | + public static function fromFileMultiple($file, array $translations, array $options = []) |
|
| 68 | + { |
|
| 69 | + foreach (static::getFiles($file) as $file) { |
|
| 70 | + $options['file'] = $file; |
|
| 71 | + static::fromStringMultiple(static::readFile($file), $translations, $options); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -12,75 +12,75 @@ |
||
| 12 | 12 | class Xliff extends Extractor implements ExtractorInterface |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - public static $options = [ |
|
| 16 | - 'unitid_as_id' => false |
|
| 17 | - ]; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * {@inheritdoc} |
|
| 21 | - */ |
|
| 22 | - public static function fromString($string, Translations $translations, array $options = []) |
|
| 23 | - { |
|
| 24 | - $options += static::$options; |
|
| 25 | - |
|
| 26 | - $xml = new SimpleXMLElement($string, null, false); |
|
| 27 | - |
|
| 28 | - foreach ($xml->file as $file) { |
|
| 29 | - if (isset($file->notes)) { |
|
| 30 | - foreach ($file->notes->note as $note) { |
|
| 31 | - $translations->setHeader($note['id'], (string) $note); |
|
| 32 | - } |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - foreach ($file->unit as $unit) { |
|
| 36 | - foreach ($unit->segment as $segment) { |
|
| 37 | - $targets = []; |
|
| 38 | - |
|
| 39 | - foreach ($segment->target as $target) { |
|
| 40 | - $targets[] = (string) $target; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - $translation = $translations->createNewTranslation(null, (string) $segment->source); |
|
| 44 | - if (isset($unit['id'])) { |
|
| 45 | - $unitId = (string) $unit['id']; |
|
| 46 | - $translation->addComment("XLIFF_UNIT_ID: $unitId"); |
|
| 47 | - if ($options['unitid_as_id']) { |
|
| 48 | - $translation->setId($unitId); |
|
| 49 | - } |
|
| 50 | - } |
|
| 51 | - $translation->setTranslation(array_shift($targets)); |
|
| 52 | - $translation->setPluralTranslations($targets); |
|
| 53 | - |
|
| 54 | - if (isset($unit->notes)) { |
|
| 55 | - foreach ($unit->notes->note as $note) { |
|
| 56 | - switch ($note['category']) { |
|
| 57 | - case 'context': |
|
| 58 | - $translation = $translation->getClone((string) $note); |
|
| 59 | - break; |
|
| 60 | - |
|
| 61 | - case 'extracted-comment': |
|
| 62 | - $translation->addExtractedComment((string) $note); |
|
| 63 | - break; |
|
| 64 | - |
|
| 65 | - case 'flag': |
|
| 66 | - $translation->addFlag((string) $note); |
|
| 67 | - break; |
|
| 68 | - |
|
| 69 | - case 'reference': |
|
| 70 | - $ref = explode(':', (string) $note, 2); |
|
| 71 | - $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null); |
|
| 72 | - break; |
|
| 73 | - |
|
| 74 | - default: |
|
| 75 | - $translation->addComment((string) $note); |
|
| 76 | - break; |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - $translations[] = $translation; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - } |
|
| 15 | + public static $options = [ |
|
| 16 | + 'unitid_as_id' => false |
|
| 17 | + ]; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * {@inheritdoc} |
|
| 21 | + */ |
|
| 22 | + public static function fromString($string, Translations $translations, array $options = []) |
|
| 23 | + { |
|
| 24 | + $options += static::$options; |
|
| 25 | + |
|
| 26 | + $xml = new SimpleXMLElement($string, null, false); |
|
| 27 | + |
|
| 28 | + foreach ($xml->file as $file) { |
|
| 29 | + if (isset($file->notes)) { |
|
| 30 | + foreach ($file->notes->note as $note) { |
|
| 31 | + $translations->setHeader($note['id'], (string) $note); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + foreach ($file->unit as $unit) { |
|
| 36 | + foreach ($unit->segment as $segment) { |
|
| 37 | + $targets = []; |
|
| 38 | + |
|
| 39 | + foreach ($segment->target as $target) { |
|
| 40 | + $targets[] = (string) $target; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + $translation = $translations->createNewTranslation(null, (string) $segment->source); |
|
| 44 | + if (isset($unit['id'])) { |
|
| 45 | + $unitId = (string) $unit['id']; |
|
| 46 | + $translation->addComment("XLIFF_UNIT_ID: $unitId"); |
|
| 47 | + if ($options['unitid_as_id']) { |
|
| 48 | + $translation->setId($unitId); |
|
| 49 | + } |
|
| 50 | + } |
|
| 51 | + $translation->setTranslation(array_shift($targets)); |
|
| 52 | + $translation->setPluralTranslations($targets); |
|
| 53 | + |
|
| 54 | + if (isset($unit->notes)) { |
|
| 55 | + foreach ($unit->notes->note as $note) { |
|
| 56 | + switch ($note['category']) { |
|
| 57 | + case 'context': |
|
| 58 | + $translation = $translation->getClone((string) $note); |
|
| 59 | + break; |
|
| 60 | + |
|
| 61 | + case 'extracted-comment': |
|
| 62 | + $translation->addExtractedComment((string) $note); |
|
| 63 | + break; |
|
| 64 | + |
|
| 65 | + case 'flag': |
|
| 66 | + $translation->addFlag((string) $note); |
|
| 67 | + break; |
|
| 68 | + |
|
| 69 | + case 'reference': |
|
| 70 | + $ref = explode(':', (string) $note, 2); |
|
| 71 | + $translation->addReference($ref[0], isset($ref[1]) ? $ref[1] : null); |
|
| 72 | + break; |
|
| 73 | + |
|
| 74 | + default: |
|
| 75 | + $translation->addComment((string) $note); |
|
| 76 | + break; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + $translations[] = $translation; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | 86 | } |
@@ -11,17 +11,17 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class YamlDictionary extends Extractor implements ExtractorInterface |
| 13 | 13 | { |
| 14 | - use DictionaryTrait; |
|
| 14 | + use DictionaryTrait; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * {@inheritdoc} |
|
| 18 | - */ |
|
| 19 | - public static function fromString($string, Translations $translations, array $options = []) |
|
| 20 | - { |
|
| 21 | - $messages = YamlParser::parse($string); |
|
| 16 | + /** |
|
| 17 | + * {@inheritdoc} |
|
| 18 | + */ |
|
| 19 | + public static function fromString($string, Translations $translations, array $options = []) |
|
| 20 | + { |
|
| 21 | + $messages = YamlParser::parse($string); |
|
| 22 | 22 | |
| 23 | - if (is_array($messages)) { |
|
| 24 | - static::fromArray($messages, $translations); |
|
| 25 | - } |
|
| 26 | - } |
|
| 23 | + if (is_array($messages)) { |
|
| 24 | + static::fromArray($messages, $translations); |
|
| 25 | + } |
|
| 26 | + } |
|
| 27 | 27 | } |
@@ -13,33 +13,33 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Twig extends Extractor implements ExtractorInterface |
| 15 | 15 | { |
| 16 | - public static $options = [ |
|
| 17 | - 'extractComments' => 'notes:', |
|
| 18 | - 'twig' => null, |
|
| 19 | - ]; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * {@inheritdoc} |
|
| 23 | - */ |
|
| 24 | - public static function fromString($string, Translations $translations, array $options = []) |
|
| 25 | - { |
|
| 26 | - $options += static::$options; |
|
| 27 | - |
|
| 28 | - $twig = $options['twig'] ?: static::createTwig(); |
|
| 29 | - |
|
| 30 | - PhpCode::fromString($twig->compileSource(new Twig_Source($string, '')), $translations, $options); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Returns a Twig instance. |
|
| 35 | - * |
|
| 36 | - * @return Twig_Environment |
|
| 37 | - */ |
|
| 38 | - protected static function createTwig() |
|
| 39 | - { |
|
| 40 | - $twig = new Twig_Environment(new Twig_Loader_Array(['' => ''])); |
|
| 41 | - $twig->addExtension(new Twig_Extensions_Extension_I18n()); |
|
| 42 | - |
|
| 43 | - return static::$options['twig'] = $twig; |
|
| 44 | - } |
|
| 16 | + public static $options = [ |
|
| 17 | + 'extractComments' => 'notes:', |
|
| 18 | + 'twig' => null, |
|
| 19 | + ]; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * {@inheritdoc} |
|
| 23 | + */ |
|
| 24 | + public static function fromString($string, Translations $translations, array $options = []) |
|
| 25 | + { |
|
| 26 | + $options += static::$options; |
|
| 27 | + |
|
| 28 | + $twig = $options['twig'] ?: static::createTwig(); |
|
| 29 | + |
|
| 30 | + PhpCode::fromString($twig->compileSource(new Twig_Source($string, '')), $translations, $options); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Returns a Twig instance. |
|
| 35 | + * |
|
| 36 | + * @return Twig_Environment |
|
| 37 | + */ |
|
| 38 | + protected static function createTwig() |
|
| 39 | + { |
|
| 40 | + $twig = new Twig_Environment(new Twig_Loader_Array(['' => ''])); |
|
| 41 | + $twig->addExtension(new Twig_Extensions_Extension_I18n()); |
|
| 42 | + |
|
| 43 | + return static::$options['twig'] = $twig; |
|
| 44 | + } |
|
| 45 | 45 | } |
@@ -11,24 +11,24 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Blade extends Extractor implements ExtractorInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * {@inheritdoc} |
|
| 16 | - */ |
|
| 17 | - public static function fromString($string, Translations $translations, array $options = []) |
|
| 18 | - { |
|
| 19 | - if (empty($options['facade'])) { |
|
| 20 | - $cachePath = empty($options['cachePath']) ? sys_get_temp_dir() : $options['cachePath']; |
|
| 21 | - $bladeCompiler = new BladeCompiler(new Filesystem(), $cachePath); |
|
| 14 | + /** |
|
| 15 | + * {@inheritdoc} |
|
| 16 | + */ |
|
| 17 | + public static function fromString($string, Translations $translations, array $options = []) |
|
| 18 | + { |
|
| 19 | + if (empty($options['facade'])) { |
|
| 20 | + $cachePath = empty($options['cachePath']) ? sys_get_temp_dir() : $options['cachePath']; |
|
| 21 | + $bladeCompiler = new BladeCompiler(new Filesystem(), $cachePath); |
|
| 22 | 22 | |
| 23 | - if (method_exists($bladeCompiler, 'withoutComponentTags')) { |
|
| 24 | - $bladeCompiler->withoutComponentTags(); |
|
| 25 | - } |
|
| 23 | + if (method_exists($bladeCompiler, 'withoutComponentTags')) { |
|
| 24 | + $bladeCompiler->withoutComponentTags(); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - $string = $bladeCompiler->compileString($string); |
|
| 28 | - } else { |
|
| 29 | - $string = $options['facade']::compileString($string); |
|
| 30 | - } |
|
| 27 | + $string = $bladeCompiler->compileString($string); |
|
| 28 | + } else { |
|
| 29 | + $string = $options['facade']::compileString($string); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - PhpCode::fromString($string, $translations, $options); |
|
| 33 | - } |
|
| 32 | + PhpCode::fromString($string, $translations, $options); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -6,21 +6,21 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface ExtractorInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Extract the translations from a file. |
|
| 11 | - * |
|
| 12 | - * @param array|string $file A path of a file or files |
|
| 13 | - * @param Translations $translations The translations instance to append the new translations. |
|
| 14 | - * @param array $options |
|
| 15 | - */ |
|
| 16 | - public static function fromFile($file, Translations $translations, array $options = []); |
|
| 9 | + /** |
|
| 10 | + * Extract the translations from a file. |
|
| 11 | + * |
|
| 12 | + * @param array|string $file A path of a file or files |
|
| 13 | + * @param Translations $translations The translations instance to append the new translations. |
|
| 14 | + * @param array $options |
|
| 15 | + */ |
|
| 16 | + public static function fromFile($file, Translations $translations, array $options = []); |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Parses a string and append the translations found in the Translations instance. |
|
| 20 | - * |
|
| 21 | - * @param string $string |
|
| 22 | - * @param Translations $translations |
|
| 23 | - * @param array $options |
|
| 24 | - */ |
|
| 25 | - public static function fromString($string, Translations $translations, array $options = []); |
|
| 18 | + /** |
|
| 19 | + * Parses a string and append the translations found in the Translations instance. |
|
| 20 | + * |
|
| 21 | + * @param string $string |
|
| 22 | + * @param Translations $translations |
|
| 23 | + * @param array $options |
|
| 24 | + */ |
|
| 25 | + public static function fromString($string, Translations $translations, array $options = []); |
|
| 26 | 26 | } |