@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function has(string $domain, string $id): bool |
| 59 | 59 | { |
| 60 | - if (!isset($this->data[$domain])) { |
|
| 60 | + if (!isset($this->data[$domain])){ |
|
| 61 | 61 | return false; |
| 62 | 62 | } |
| 63 | 63 | |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function get(string $domain, string $string): string |
| 71 | 71 | { |
| 72 | - if (!$this->has($domain, $string)) { |
|
| 72 | + if (!$this->has($domain, $string)){ |
|
| 73 | 73 | throw new CatalogueException("Undefined string in domain '{$domain}'"); |
| 74 | 74 | } |
| 75 | 75 | |
@@ -99,15 +99,15 @@ discard block |
||
| 99 | 99 | */ |
| 100 | 100 | public function mergeFrom(MessageCatalogue $catalogue, bool $follow = true): void |
| 101 | 101 | { |
| 102 | - foreach ($catalogue->all() as $domain => $messages) { |
|
| 103 | - if (!isset($this->data[$domain])) { |
|
| 102 | + foreach ($catalogue->all() as $domain => $messages){ |
|
| 103 | + if (!isset($this->data[$domain])){ |
|
| 104 | 104 | $this->data[$domain] = []; |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - if ($follow) { |
|
| 107 | + if ($follow){ |
|
| 108 | 108 | //MessageCatalogue string has higher priority that string stored in memory |
| 109 | 109 | $this->data[$domain] = array_merge($messages, $this->data[$domain]); |
| 110 | - } else { |
|
| 110 | + }else{ |
|
| 111 | 111 | $this->data[$domain] = array_merge($this->data[$domain], $messages); |
| 112 | 112 | } |
| 113 | 113 | } |
@@ -57,7 +57,8 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public function has(string $domain, string $id): bool |
| 59 | 59 | { |
| 60 | - if (!isset($this->data[$domain])) { |
|
| 60 | + if (!isset($this->data[$domain])) |
|
| 61 | + { |
|
| 61 | 62 | return false; |
| 62 | 63 | } |
| 63 | 64 | |
@@ -69,7 +70,8 @@ discard block |
||
| 69 | 70 | */ |
| 70 | 71 | public function get(string $domain, string $string): string |
| 71 | 72 | { |
| 72 | - if (!$this->has($domain, $string)) { |
|
| 73 | + if (!$this->has($domain, $string)) |
|
| 74 | + { |
|
| 73 | 75 | throw new CatalogueException("Undefined string in domain '{$domain}'"); |
| 74 | 76 | } |
| 75 | 77 | |
@@ -99,15 +101,20 @@ discard block |
||
| 99 | 101 | */ |
| 100 | 102 | public function mergeFrom(MessageCatalogue $catalogue, bool $follow = true): void |
| 101 | 103 | { |
| 102 | - foreach ($catalogue->all() as $domain => $messages) { |
|
| 103 | - if (!isset($this->data[$domain])) { |
|
| 104 | + foreach ($catalogue->all() as $domain => $messages) |
|
| 105 | + { |
|
| 106 | + if (!isset($this->data[$domain])) |
|
| 107 | + { |
|
| 104 | 108 | $this->data[$domain] = []; |
| 105 | 109 | } |
| 106 | 110 | |
| 107 | - if ($follow) { |
|
| 111 | + if ($follow) |
|
| 112 | + { |
|
| 108 | 113 | //MessageCatalogue string has higher priority that string stored in memory |
| 109 | 114 | $this->data[$domain] = array_merge($messages, $this->data[$domain]); |
| 110 | - } else { |
|
| 115 | + } |
|
| 116 | + else |
|
| 117 | + { |
|
| 111 | 118 | $this->data[$domain] = array_merge($this->data[$domain], $messages); |
| 112 | 119 | } |
| 113 | 120 | } |
@@ -37,10 +37,10 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | public function matches(string $string, string $pattern): bool |
| 39 | 39 | { |
| 40 | - if ($string === $pattern) { |
|
| 40 | + if ($string === $pattern){ |
|
| 41 | 41 | return true; |
| 42 | 42 | } |
| 43 | - if (!$this->isPattern($pattern)) { |
|
| 43 | + if (!$this->isPattern($pattern)){ |
|
| 44 | 44 | return false; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -37,10 +37,12 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | public function matches(string $string, string $pattern): bool |
| 39 | 39 | { |
| 40 | - if ($string === $pattern) { |
|
| 40 | + if ($string === $pattern) |
|
| 41 | + { |
|
| 41 | 42 | return true; |
| 42 | 43 | } |
| 43 | - if (!$this->isPattern($pattern)) { |
|
| 44 | + if (!$this->isPattern($pattern)) |
|
| 45 | + { |
|
| 44 | 46 | return false; |
| 45 | 47 | } |
| 46 | 48 | |
@@ -41,13 +41,13 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | protected function say(string $string, array $options = [], $bundle = null): string |
| 43 | 43 | { |
| 44 | - if (Translator::isMessage($string)) { |
|
| 44 | + if (Translator::isMessage($string)){ |
|
| 45 | 45 | //Cut [[ and ]] |
| 46 | 46 | $string = substr($string, 2, -2); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | $container = ContainerScope::getContainer(); |
| 50 | - if (empty($container) || !$container->has(TranslatorInterface::class)) { |
|
| 50 | + if (empty($container) || !$container->has(TranslatorInterface::class)){ |
|
| 51 | 51 | return Translator::interpolate($string, $options); |
| 52 | 52 | } |
| 53 | 53 | |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | $translator = $container->get(TranslatorInterface::class); |
| 58 | 58 | |
| 59 | - if (is_null($bundle)) { |
|
| 59 | + if (is_null($bundle)){ |
|
| 60 | 60 | $bundle = $translator->getDomain(static::class); |
| 61 | 61 | } |
| 62 | 62 | |
@@ -41,13 +41,15 @@ discard block |
||
| 41 | 41 | */ |
| 42 | 42 | protected function say(string $string, array $options = [], $bundle = null): string |
| 43 | 43 | { |
| 44 | - if (Translator::isMessage($string)) { |
|
| 44 | + if (Translator::isMessage($string)) |
|
| 45 | + { |
|
| 45 | 46 | //Cut [[ and ]] |
| 46 | 47 | $string = substr($string, 2, -2); |
| 47 | 48 | } |
| 48 | 49 | |
| 49 | 50 | $container = ContainerScope::getContainer(); |
| 50 | - if (empty($container) || !$container->has(TranslatorInterface::class)) { |
|
| 51 | + if (empty($container) || !$container->has(TranslatorInterface::class)) |
|
| 52 | + { |
|
| 51 | 53 | return Translator::interpolate($string, $options); |
| 52 | 54 | } |
| 53 | 55 | |
@@ -56,7 +58,8 @@ discard block |
||
| 56 | 58 | */ |
| 57 | 59 | $translator = $container->get(TranslatorInterface::class); |
| 58 | 60 | |
| 59 | - if (is_null($bundle)) { |
|
| 61 | + if (is_null($bundle)) |
|
| 62 | + { |
|
| 60 | 63 | $bundle = $translator->getDomain(static::class); |
| 61 | 64 | } |
| 62 | 65 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | public function getLocaleDirectory(string $locale): string |
| 96 | 96 | { |
| 97 | - return $this->getLocalesDirectory() . $locale . '/'; |
|
| 97 | + return $this->getLocalesDirectory().$locale.'/'; |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | /** |
@@ -107,9 +107,9 @@ discard block |
||
| 107 | 107 | { |
| 108 | 108 | $bundle = strtolower(str_replace(['/', '\\'], '-', $bundle)); |
| 109 | 109 | |
| 110 | - foreach ($this->config['domains'] as $domain => $patterns) { |
|
| 111 | - foreach ($patterns as $pattern) { |
|
| 112 | - if ($this->matcher->matches($bundle, $pattern)) { |
|
| 110 | + foreach ($this->config['domains'] as $domain => $patterns){ |
|
| 111 | + foreach ($patterns as $pattern){ |
|
| 112 | + if ($this->matcher->matches($bundle, $pattern)){ |
|
| 113 | 113 | return $domain; |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -107,9 +107,12 @@ |
||
| 107 | 107 | { |
| 108 | 108 | $bundle = strtolower(str_replace(['/', '\\'], '-', $bundle)); |
| 109 | 109 | |
| 110 | - foreach ($this->config['domains'] as $domain => $patterns) { |
|
| 111 | - foreach ($patterns as $pattern) { |
|
| 112 | - if ($this->matcher->matches($bundle, $pattern)) { |
|
| 110 | + foreach ($this->config['domains'] as $domain => $patterns) |
|
| 111 | + { |
|
| 112 | + foreach ($patterns as $pattern) |
|
| 113 | + { |
|
| 114 | + if ($this->matcher->matches($bundle, $pattern)) |
|
| 115 | + { |
|
| 113 | 116 | return $domain; |
| 114 | 117 | } |
| 115 | 118 | } |
@@ -50,7 +50,7 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | public function loadCatalogue(string $locale): CatalogueInterface |
| 52 | 52 | { |
| 53 | - if (!$this->hasLocale($locale)) { |
|
| 53 | + if (!$this->hasLocale($locale)){ |
|
| 54 | 54 | throw new LocaleException($locale); |
| 55 | 55 | } |
| 56 | 56 | |
@@ -50,7 +50,8 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | public function loadCatalogue(string $locale): CatalogueInterface |
| 52 | 52 | { |
| 53 | - if (!$this->hasLocale($locale)) { |
|
| 53 | + if (!$this->hasLocale($locale)) |
|
| 54 | + { |
|
| 54 | 55 | throw new LocaleException($locale); |
| 55 | 56 | } |
| 56 | 57 | |
@@ -50,12 +50,12 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function getLocales(): array |
| 52 | 52 | { |
| 53 | - if ($this->locales !== []) { |
|
| 53 | + if ($this->locales !== []){ |
|
| 54 | 54 | return $this->locales; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | $this->locales = (array)$this->cache->getLocales(); |
| 58 | - if ($this->locales === []) { |
|
| 58 | + if ($this->locales === []){ |
|
| 59 | 59 | $this->locales = $this->loader->getLocales(); |
| 60 | 60 | $this->cache->setLocales($this->locales); |
| 61 | 61 | } |
@@ -68,14 +68,14 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function load(string $locale): CatalogueInterface |
| 70 | 70 | { |
| 71 | - if (isset($this->catalogues[$locale])) { |
|
| 71 | + if (isset($this->catalogues[$locale])){ |
|
| 72 | 72 | return $this->catalogues[$locale]; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | $data = (array)$this->cache->loadLocale($locale); |
| 76 | - if (!empty($data)) { |
|
| 76 | + if (!empty($data)){ |
|
| 77 | 77 | $this->catalogues[$locale] = new Catalogue($locale, $data); |
| 78 | - } else { |
|
| 78 | + }else{ |
|
| 79 | 79 | $this->catalogues[$locale] = $this->loader->loadCatalogue($locale); |
| 80 | 80 | } |
| 81 | 81 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | public function reset(): void |
| 113 | 113 | { |
| 114 | 114 | $this->cache->setLocales(null); |
| 115 | - foreach ($this->getLocales() as $locale) { |
|
| 115 | + foreach ($this->getLocales() as $locale){ |
|
| 116 | 116 | $this->cache->saveLocale($locale, null); |
| 117 | 117 | } |
| 118 | 118 | |
@@ -50,12 +50,14 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function getLocales(): array |
| 52 | 52 | { |
| 53 | - if ($this->locales !== []) { |
|
| 53 | + if ($this->locales !== []) |
|
| 54 | + { |
|
| 54 | 55 | return $this->locales; |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | 58 | $this->locales = (array)$this->cache->getLocales(); |
| 58 | - if ($this->locales === []) { |
|
| 59 | + if ($this->locales === []) |
|
| 60 | + { |
|
| 59 | 61 | $this->locales = $this->loader->getLocales(); |
| 60 | 62 | $this->cache->setLocales($this->locales); |
| 61 | 63 | } |
@@ -68,14 +70,18 @@ discard block |
||
| 68 | 70 | */ |
| 69 | 71 | public function load(string $locale): CatalogueInterface |
| 70 | 72 | { |
| 71 | - if (isset($this->catalogues[$locale])) { |
|
| 73 | + if (isset($this->catalogues[$locale])) |
|
| 74 | + { |
|
| 72 | 75 | return $this->catalogues[$locale]; |
| 73 | 76 | } |
| 74 | 77 | |
| 75 | 78 | $data = (array)$this->cache->loadLocale($locale); |
| 76 | - if (!empty($data)) { |
|
| 79 | + if (!empty($data)) |
|
| 80 | + { |
|
| 77 | 81 | $this->catalogues[$locale] = new Catalogue($locale, $data); |
| 78 | - } else { |
|
| 82 | + } |
|
| 83 | + else |
|
| 84 | + { |
|
| 79 | 85 | $this->catalogues[$locale] = $this->loader->loadCatalogue($locale); |
| 80 | 86 | } |
| 81 | 87 | |
@@ -112,7 +118,8 @@ discard block |
||
| 112 | 118 | public function reset(): void |
| 113 | 119 | { |
| 114 | 120 | $this->cache->setLocales(null); |
| 115 | - foreach ($this->getLocales() as $locale) { |
|
| 121 | + foreach ($this->getLocales() as $locale) |
|
| 122 | + { |
|
| 116 | 123 | $this->cache->saveLocale($locale, null); |
| 117 | 124 | } |
| 118 | 125 | |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | use Spiral\Translator\Exception\TranslatorException; |
| 15 | 15 | use Spiral\Translator\TranslatorInterface; |
| 16 | 16 | |
| 17 | -if (!function_exists('l')) { |
|
| 17 | +if (!function_exists('l')){ |
|
| 18 | 18 | /** |
| 19 | 19 | * Translate message using default or specific bundle name. |
| 20 | 20 | * |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | function l(string $string, array $options = [], string $domain = null): string |
| 35 | 35 | { |
| 36 | 36 | $container = ContainerScope::getContainer(); |
| 37 | - if (empty($container) || !$container->has(TranslatorInterface::class)) { |
|
| 37 | + if (empty($container) || !$container->has(TranslatorInterface::class)){ |
|
| 38 | 38 | throw new ScopeException( |
| 39 | 39 | '`TranslatorInterface` binding is missing or container scope is not set' |
| 40 | 40 | ); |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | -if (!function_exists('p')) { |
|
| 50 | +if (!function_exists('p')){ |
|
| 51 | 51 | /** |
| 52 | 52 | * Pluralize string using language pluralization options and specified numeric value. |
| 53 | 53 | * |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | function p(string $string, int $number, array $options = [], string $domain = null): string |
| 68 | 68 | { |
| 69 | 69 | $container = ContainerScope::getContainer(); |
| 70 | - if (empty($container) || !$container->has(TranslatorInterface::class)) { |
|
| 70 | + if (empty($container) || !$container->has(TranslatorInterface::class)){ |
|
| 71 | 71 | throw new ScopeException( |
| 72 | 72 | '`TranslatorInterface` binding is missing or container scope is not set' |
| 73 | 73 | ); |
@@ -14,7 +14,8 @@ discard block |
||
| 14 | 14 | use Spiral\Translator\Exception\TranslatorException; |
| 15 | 15 | use Spiral\Translator\TranslatorInterface; |
| 16 | 16 | |
| 17 | -if (!function_exists('l')) { |
|
| 17 | +if (!function_exists('l')) |
|
| 18 | +{ |
|
| 18 | 19 | /** |
| 19 | 20 | * Translate message using default or specific bundle name. |
| 20 | 21 | * |
@@ -34,7 +35,8 @@ discard block |
||
| 34 | 35 | function l(string $string, array $options = [], string $domain = null): string |
| 35 | 36 | { |
| 36 | 37 | $container = ContainerScope::getContainer(); |
| 37 | - if (empty($container) || !$container->has(TranslatorInterface::class)) { |
|
| 38 | + if (empty($container) || !$container->has(TranslatorInterface::class)) |
|
| 39 | + { |
|
| 38 | 40 | throw new ScopeException( |
| 39 | 41 | '`TranslatorInterface` binding is missing or container scope is not set' |
| 40 | 42 | ); |
@@ -47,7 +49,8 @@ discard block |
||
| 47 | 49 | } |
| 48 | 50 | } |
| 49 | 51 | |
| 50 | -if (!function_exists('p')) { |
|
| 52 | +if (!function_exists('p')) |
|
| 53 | +{ |
|
| 51 | 54 | /** |
| 52 | 55 | * Pluralize string using language pluralization options and specified numeric value. |
| 53 | 56 | * |
@@ -67,7 +70,8 @@ discard block |
||
| 67 | 70 | function p(string $string, int $number, array $options = [], string $domain = null): string |
| 68 | 71 | { |
| 69 | 72 | $container = ContainerScope::getContainer(); |
| 70 | - if (empty($container) || !$container->has(TranslatorInterface::class)) { |
|
| 73 | + if (empty($container) || !$container->has(TranslatorInterface::class)) |
|
| 74 | + { |
|
| 71 | 75 | throw new ScopeException( |
| 72 | 76 | '`TranslatorInterface` binding is missing or container scope is not set' |
| 73 | 77 | ); |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | TranslatorConfig $config, |
| 45 | 45 | CatalogueManagerInterface $catalogueManager, |
| 46 | 46 | IdentityTranslator $identityTranslator = null |
| 47 | - ) { |
|
| 47 | + ){ |
|
| 48 | 48 | $this->config = $config; |
| 49 | 49 | $this->identityTranslator = $identityTranslator ?? new IdentityTranslator(); |
| 50 | 50 | $this->catalogueManager = $catalogueManager; |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function setLocale(string $locale): void |
| 70 | 70 | { |
| 71 | - if (!$this->catalogueManager->has($locale)) { |
|
| 71 | + if (!$this->catalogueManager->has($locale)){ |
|
| 72 | 72 | throw new LocaleException($locale); |
| 73 | 73 | } |
| 74 | 74 | |
@@ -124,11 +124,11 @@ discard block |
||
| 124 | 124 | array $parameters = [], |
| 125 | 125 | $domain = null, |
| 126 | 126 | $locale = null |
| 127 | - ) { |
|
| 127 | + ){ |
|
| 128 | 128 | $domain = $domain ?? $this->config->getDefaultDomain(); |
| 129 | 129 | $locale = $locale ?? $this->locale; |
| 130 | 130 | |
| 131 | - try { |
|
| 131 | + try{ |
|
| 132 | 132 | $message = $this->get($locale, $domain, $id); |
| 133 | 133 | |
| 134 | 134 | $pluralized = $this->identityTranslator->trans( |
@@ -137,12 +137,12 @@ discard block |
||
| 137 | 137 | null, |
| 138 | 138 | $locale |
| 139 | 139 | ); |
| 140 | - } catch (\InvalidArgumentException $e) { |
|
| 140 | + }catch (\InvalidArgumentException $e){ |
|
| 141 | 141 | //Wrapping into more explanatory exception |
| 142 | 142 | throw new PluralizationException($e->getMessage(), $e->getCode(), $e); |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - if (empty($parameters['n']) && is_numeric($number)) { |
|
| 145 | + if (empty($parameters['n']) && is_numeric($number)){ |
|
| 146 | 146 | $parameters['n'] = $number; |
| 147 | 147 | } |
| 148 | 148 | |
@@ -168,18 +168,18 @@ discard block |
||
| 168 | 168 | string $postfix = '}' |
| 169 | 169 | ): string { |
| 170 | 170 | $replaces = []; |
| 171 | - foreach ($values as $key => $value) { |
|
| 171 | + foreach ($values as $key => $value){ |
|
| 172 | 172 | $value = (is_array($value) || $value instanceof \Closure) ? '' : $value; |
| 173 | 173 | |
| 174 | - if (is_object($value)) { |
|
| 175 | - if (method_exists($value, '__toString')) { |
|
| 174 | + if (is_object($value)){ |
|
| 175 | + if (method_exists($value, '__toString')){ |
|
| 176 | 176 | $value = $value->__toString(); |
| 177 | - } else { |
|
| 177 | + }else{ |
|
| 178 | 178 | $value = ''; |
| 179 | 179 | } |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | - $replaces[$prefix . $key . $postfix] = $value; |
|
| 182 | + $replaces[$prefix.$key.$postfix] = $value; |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | return strtr($string, $replaces); |
@@ -207,18 +207,18 @@ discard block |
||
| 207 | 207 | */ |
| 208 | 208 | protected function get(string &$locale, string $domain, string $string): string |
| 209 | 209 | { |
| 210 | - if ($this->catalogueManager->get($locale)->has($domain, $string)) { |
|
| 210 | + if ($this->catalogueManager->get($locale)->has($domain, $string)){ |
|
| 211 | 211 | return $this->catalogueManager->get($locale)->get($domain, $string); |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | $locale = $this->config->getFallbackLocale(); |
| 215 | 215 | |
| 216 | - if ($this->catalogueManager->get($locale)->has($domain, $string)) { |
|
| 216 | + if ($this->catalogueManager->get($locale)->has($domain, $string)){ |
|
| 217 | 217 | return $this->catalogueManager->get($locale)->get($domain, $string); |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | // we can automatically register message |
| 221 | - if ($this->config->isAutoRegisterMessages()) { |
|
| 221 | + if ($this->config->isAutoRegisterMessages()){ |
|
| 222 | 222 | $this->catalogueManager->get($locale)->set($domain, $string, $string); |
| 223 | 223 | $this->catalogueManager->save($locale); |
| 224 | 224 | } |
@@ -68,7 +68,8 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function setLocale(string $locale): void |
| 70 | 70 | { |
| 71 | - if (!$this->catalogueManager->has($locale)) { |
|
| 71 | + if (!$this->catalogueManager->has($locale)) |
|
| 72 | + { |
|
| 72 | 73 | throw new LocaleException($locale); |
| 73 | 74 | } |
| 74 | 75 | |
@@ -128,7 +129,8 @@ discard block |
||
| 128 | 129 | $domain = $domain ?? $this->config->getDefaultDomain(); |
| 129 | 130 | $locale = $locale ?? $this->locale; |
| 130 | 131 | |
| 131 | - try { |
|
| 132 | + try |
|
| 133 | + { |
|
| 132 | 134 | $message = $this->get($locale, $domain, $id); |
| 133 | 135 | |
| 134 | 136 | $pluralized = $this->identityTranslator->trans( |
@@ -137,12 +139,15 @@ discard block |
||
| 137 | 139 | null, |
| 138 | 140 | $locale |
| 139 | 141 | ); |
| 140 | - } catch (\InvalidArgumentException $e) { |
|
| 142 | + } |
|
| 143 | + catch (\InvalidArgumentException $e) |
|
| 144 | + { |
|
| 141 | 145 | //Wrapping into more explanatory exception |
| 142 | 146 | throw new PluralizationException($e->getMessage(), $e->getCode(), $e); |
| 143 | 147 | } |
| 144 | 148 | |
| 145 | - if (empty($parameters['n']) && is_numeric($number)) { |
|
| 149 | + if (empty($parameters['n']) && is_numeric($number)) |
|
| 150 | + { |
|
| 146 | 151 | $parameters['n'] = $number; |
| 147 | 152 | } |
| 148 | 153 | |
@@ -168,13 +173,18 @@ discard block |
||
| 168 | 173 | string $postfix = '}' |
| 169 | 174 | ): string { |
| 170 | 175 | $replaces = []; |
| 171 | - foreach ($values as $key => $value) { |
|
| 176 | + foreach ($values as $key => $value) |
|
| 177 | + { |
|
| 172 | 178 | $value = (is_array($value) || $value instanceof \Closure) ? '' : $value; |
| 173 | 179 | |
| 174 | - if (is_object($value)) { |
|
| 175 | - if (method_exists($value, '__toString')) { |
|
| 180 | + if (is_object($value)) |
|
| 181 | + { |
|
| 182 | + if (method_exists($value, '__toString')) |
|
| 183 | + { |
|
| 176 | 184 | $value = $value->__toString(); |
| 177 | - } else { |
|
| 185 | + } |
|
| 186 | + else |
|
| 187 | + { |
|
| 178 | 188 | $value = ''; |
| 179 | 189 | } |
| 180 | 190 | } |
@@ -207,18 +217,21 @@ discard block |
||
| 207 | 217 | */ |
| 208 | 218 | protected function get(string &$locale, string $domain, string $string): string |
| 209 | 219 | { |
| 210 | - if ($this->catalogueManager->get($locale)->has($domain, $string)) { |
|
| 220 | + if ($this->catalogueManager->get($locale)->has($domain, $string)) |
|
| 221 | + { |
|
| 211 | 222 | return $this->catalogueManager->get($locale)->get($domain, $string); |
| 212 | 223 | } |
| 213 | 224 | |
| 214 | 225 | $locale = $this->config->getFallbackLocale(); |
| 215 | 226 | |
| 216 | - if ($this->catalogueManager->get($locale)->has($domain, $string)) { |
|
| 227 | + if ($this->catalogueManager->get($locale)->has($domain, $string)) |
|
| 228 | + { |
|
| 217 | 229 | return $this->catalogueManager->get($locale)->get($domain, $string); |
| 218 | 230 | } |
| 219 | 231 | |
| 220 | 232 | // we can automatically register message |
| 221 | - if ($this->config->isAutoRegisterMessages()) { |
|
| 233 | + if ($this->config->isAutoRegisterMessages()) |
|
| 234 | + { |
|
| 222 | 235 | $this->catalogueManager->get($locale)->set($domain, $string, $string); |
| 223 | 236 | $this->catalogueManager->save($locale); |
| 224 | 237 | } |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | */ |
| 62 | 62 | public function registerMessage(string $domain, string $string, bool $resolveDomain = true): void |
| 63 | 63 | { |
| 64 | - if ($resolveDomain) { |
|
| 64 | + if ($resolveDomain){ |
|
| 65 | 65 | $domain = $this->config->resolveDomain($domain); |
| 66 | 66 | } |
| 67 | 67 | |
@@ -82,9 +82,9 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | public function indexClasses(ClassesInterface $locator): void |
| 84 | 84 | { |
| 85 | - foreach ($locator->getClasses(TranslatorTrait::class) as $class) { |
|
| 85 | + foreach ($locator->getClasses(TranslatorTrait::class) as $class){ |
|
| 86 | 86 | $strings = $this->fetchMessages($class, true); |
| 87 | - foreach ($strings as $string) { |
|
| 87 | + foreach ($strings as $string){ |
|
| 88 | 88 | $this->registerMessage($class->getName(), $string); |
| 89 | 89 | } |
| 90 | 90 | } |
@@ -118,8 +118,8 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | private function registerInvocations(array $invocations): void |
| 120 | 120 | { |
| 121 | - foreach ($invocations as $invocation) { |
|
| 122 | - if ($invocation->getArgument(0)->getType() != ReflectionArgument::STRING) { |
|
| 121 | + foreach ($invocations as $invocation){ |
|
| 122 | + if ($invocation->getArgument(0)->getType() != ReflectionArgument::STRING){ |
|
| 123 | 123 | //We can only index invocations with constant string arguments |
| 124 | 124 | continue; |
| 125 | 125 | } |
@@ -142,20 +142,20 @@ discard block |
||
| 142 | 142 | { |
| 143 | 143 | $target = $reflection->getDefaultProperties() + $reflection->getConstants(); |
| 144 | 144 | |
| 145 | - foreach ($reflection->getProperties() as $property) { |
|
| 146 | - if (is_string($property->getDocComment()) && strpos($property->getDocComment(), '@do-not-index')) { |
|
| 145 | + foreach ($reflection->getProperties() as $property){ |
|
| 146 | + if (is_string($property->getDocComment()) && strpos($property->getDocComment(), '@do-not-index')){ |
|
| 147 | 147 | unset($target[$property->getName()]); |
| 148 | 148 | } |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | $strings = []; |
| 152 | 152 | array_walk_recursive($target, function ($value) use (&$strings): void { |
| 153 | - if (is_string($value) && Translator::isMessage($value)) { |
|
| 153 | + if (is_string($value) && Translator::isMessage($value)){ |
|
| 154 | 154 | $strings[] = $this->prepareMessage($value); |
| 155 | 155 | } |
| 156 | 156 | }); |
| 157 | 157 | |
| 158 | - if ($inherit && $reflection->getParentClass()) { |
|
| 158 | + if ($inherit && $reflection->getParentClass()){ |
|
| 159 | 159 | //Joining strings data with parent class values (inheritance ON) - resolved into same |
| 160 | 160 | //domain on export |
| 161 | 161 | $strings = array_merge( |
@@ -178,27 +178,27 @@ discard block |
||
| 178 | 178 | //Translation using default bundle |
| 179 | 179 | $domain = $this->config->getDefaultDomain(); |
| 180 | 180 | |
| 181 | - if ($invocation->getName() === 'say') { |
|
| 181 | + if ($invocation->getName() === 'say'){ |
|
| 182 | 182 | //Let's try to confirm domain |
| 183 | 183 | $domain = $this->config->resolveDomain($invocation->getClass()); |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | //`l` and `p`, `say` functions |
| 187 | 187 | $argument = null; |
| 188 | - switch (strtolower($invocation->getName())) { |
|
| 188 | + switch (strtolower($invocation->getName())){ |
|
| 189 | 189 | case 'say': |
| 190 | 190 | case 'l': |
| 191 | - if ($invocation->countArguments() >= 3) { |
|
| 191 | + if ($invocation->countArguments() >= 3){ |
|
| 192 | 192 | $argument = $invocation->getArgument(2); |
| 193 | 193 | } |
| 194 | 194 | break; |
| 195 | 195 | case 'p': |
| 196 | - if ($invocation->countArguments() >= 4) { |
|
| 196 | + if ($invocation->countArguments() >= 4){ |
|
| 197 | 197 | $argument = $invocation->getArgument(3); |
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - if (!empty($argument) && $argument->getType() === ReflectionArgument::STRING) { |
|
| 201 | + if (!empty($argument) && $argument->getType() === ReflectionArgument::STRING){ |
|
| 202 | 202 | //Domain specified in arguments |
| 203 | 203 | $domain = $this->config->resolveDomain($argument->stringValue()); |
| 204 | 204 | } |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | */ |
| 215 | 215 | private function prepareMessage(string $string): string |
| 216 | 216 | { |
| 217 | - if (Translator::isMessage($string)) { |
|
| 217 | + if (Translator::isMessage($string)){ |
|
| 218 | 218 | $string = substr($string, 2, -2); |
| 219 | 219 | } |
| 220 | 220 | |
@@ -61,7 +61,8 @@ discard block |
||
| 61 | 61 | */ |
| 62 | 62 | public function registerMessage(string $domain, string $string, bool $resolveDomain = true): void |
| 63 | 63 | { |
| 64 | - if ($resolveDomain) { |
|
| 64 | + if ($resolveDomain) |
|
| 65 | + { |
|
| 65 | 66 | $domain = $this->config->resolveDomain($domain); |
| 66 | 67 | } |
| 67 | 68 | |
@@ -82,9 +83,11 @@ discard block |
||
| 82 | 83 | */ |
| 83 | 84 | public function indexClasses(ClassesInterface $locator): void |
| 84 | 85 | { |
| 85 | - foreach ($locator->getClasses(TranslatorTrait::class) as $class) { |
|
| 86 | + foreach ($locator->getClasses(TranslatorTrait::class) as $class) |
|
| 87 | + { |
|
| 86 | 88 | $strings = $this->fetchMessages($class, true); |
| 87 | - foreach ($strings as $string) { |
|
| 89 | + foreach ($strings as $string) |
|
| 90 | + { |
|
| 88 | 91 | $this->registerMessage($class->getName(), $string); |
| 89 | 92 | } |
| 90 | 93 | } |
@@ -118,8 +121,10 @@ discard block |
||
| 118 | 121 | */ |
| 119 | 122 | private function registerInvocations(array $invocations): void |
| 120 | 123 | { |
| 121 | - foreach ($invocations as $invocation) { |
|
| 122 | - if ($invocation->getArgument(0)->getType() != ReflectionArgument::STRING) { |
|
| 124 | + foreach ($invocations as $invocation) |
|
| 125 | + { |
|
| 126 | + if ($invocation->getArgument(0)->getType() != ReflectionArgument::STRING) |
|
| 127 | + { |
|
| 123 | 128 | //We can only index invocations with constant string arguments |
| 124 | 129 | continue; |
| 125 | 130 | } |
@@ -142,20 +147,24 @@ discard block |
||
| 142 | 147 | { |
| 143 | 148 | $target = $reflection->getDefaultProperties() + $reflection->getConstants(); |
| 144 | 149 | |
| 145 | - foreach ($reflection->getProperties() as $property) { |
|
| 146 | - if (is_string($property->getDocComment()) && strpos($property->getDocComment(), '@do-not-index')) { |
|
| 150 | + foreach ($reflection->getProperties() as $property) |
|
| 151 | + { |
|
| 152 | + if (is_string($property->getDocComment()) && strpos($property->getDocComment(), '@do-not-index')) |
|
| 153 | + { |
|
| 147 | 154 | unset($target[$property->getName()]); |
| 148 | 155 | } |
| 149 | 156 | } |
| 150 | 157 | |
| 151 | 158 | $strings = []; |
| 152 | 159 | array_walk_recursive($target, function ($value) use (&$strings): void { |
| 153 | - if (is_string($value) && Translator::isMessage($value)) { |
|
| 160 | + if (is_string($value) && Translator::isMessage($value)) |
|
| 161 | + { |
|
| 154 | 162 | $strings[] = $this->prepareMessage($value); |
| 155 | 163 | } |
| 156 | 164 | }); |
| 157 | 165 | |
| 158 | - if ($inherit && $reflection->getParentClass()) { |
|
| 166 | + if ($inherit && $reflection->getParentClass()) |
|
| 167 | + { |
|
| 159 | 168 | //Joining strings data with parent class values (inheritance ON) - resolved into same |
| 160 | 169 | //domain on export |
| 161 | 170 | $strings = array_merge( |
@@ -178,27 +187,32 @@ discard block |
||
| 178 | 187 | //Translation using default bundle |
| 179 | 188 | $domain = $this->config->getDefaultDomain(); |
| 180 | 189 | |
| 181 | - if ($invocation->getName() === 'say') { |
|
| 190 | + if ($invocation->getName() === 'say') |
|
| 191 | + { |
|
| 182 | 192 | //Let's try to confirm domain |
| 183 | 193 | $domain = $this->config->resolveDomain($invocation->getClass()); |
| 184 | 194 | } |
| 185 | 195 | |
| 186 | 196 | //`l` and `p`, `say` functions |
| 187 | 197 | $argument = null; |
| 188 | - switch (strtolower($invocation->getName())) { |
|
| 198 | + switch (strtolower($invocation->getName())) |
|
| 199 | + { |
|
| 189 | 200 | case 'say': |
| 190 | 201 | case 'l': |
| 191 | - if ($invocation->countArguments() >= 3) { |
|
| 202 | + if ($invocation->countArguments() >= 3) |
|
| 203 | + { |
|
| 192 | 204 | $argument = $invocation->getArgument(2); |
| 193 | 205 | } |
| 194 | 206 | break; |
| 195 | 207 | case 'p': |
| 196 | - if ($invocation->countArguments() >= 4) { |
|
| 208 | + if ($invocation->countArguments() >= 4) |
|
| 209 | + { |
|
| 197 | 210 | $argument = $invocation->getArgument(3); |
| 198 | 211 | } |
| 199 | 212 | } |
| 200 | 213 | |
| 201 | - if (!empty($argument) && $argument->getType() === ReflectionArgument::STRING) { |
|
| 214 | + if (!empty($argument) && $argument->getType() === ReflectionArgument::STRING) |
|
| 215 | + { |
|
| 202 | 216 | //Domain specified in arguments |
| 203 | 217 | $domain = $this->config->resolveDomain($argument->stringValue()); |
| 204 | 218 | } |
@@ -214,7 +228,8 @@ discard block |
||
| 214 | 228 | */ |
| 215 | 229 | private function prepareMessage(string $string): string |
| 216 | 230 | { |
| 217 | - if (Translator::isMessage($string)) { |
|
| 231 | + if (Translator::isMessage($string)) |
|
| 232 | + { |
|
| 218 | 233 | $string = substr($string, 2, -2); |
| 219 | 234 | } |
| 220 | 235 | |