@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation\Resolvers; |
| 5 | 5 | |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | /** @var string|NULL */ |
| 17 | 17 | protected $lang = NULL; |
| 18 | 18 | |
| 19 | - function getLang(): ?string { |
|
| 19 | + function getLang(): ? string { |
|
| 20 | 20 | return $this->lang; |
| 21 | 21 | } |
| 22 | 22 | |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | $this->lang = $lang; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - function resolve(): ?string { |
|
| 27 | + function resolve(): ? string { |
|
| 28 | 28 | return $this->getLang(); |
| 29 | 29 | } |
| 30 | 30 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation\Resolvers; |
| 5 | 5 | |
@@ -21,10 +21,10 @@ discard block |
||
| 21 | 21 | $this[] = $resolver; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - function resolve(): ?string { |
|
| 25 | - foreach($this as $resolver) { |
|
| 24 | + function resolve(): ? string { |
|
| 25 | + foreach ($this as $resolver) { |
|
| 26 | 26 | $lang = $resolver->resolve(); |
| 27 | - if(!is_null($lang)) { |
|
| 27 | + if (!is_null($lang)) { |
|
| 28 | 28 | return $lang; |
| 29 | 29 | } |
| 30 | 30 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation\Resolvers; |
| 5 | 5 | |
@@ -17,9 +17,9 @@ discard block |
||
| 17 | 17 | /** @var string */ |
| 18 | 18 | protected $varName = "TRANSLATOR_LANGUAGE"; |
| 19 | 19 | |
| 20 | - function getLang(): ?string { |
|
| 20 | + function getLang(): ? string { |
|
| 21 | 21 | $lang = getenv($this->varName); |
| 22 | - if($lang) { |
|
| 22 | + if ($lang) { |
|
| 23 | 23 | return $lang; |
| 24 | 24 | } |
| 25 | 25 | return NULL; |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | $this->varName = $varName; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - function resolve(): ?string { |
|
| 40 | + function resolve(): ? string { |
|
| 41 | 41 | return $this->getLang(); |
| 42 | 42 | } |
| 43 | 43 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation\Resolvers; |
| 5 | 5 | |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | protected $varName = "lang"; |
| 27 | 27 | |
| 28 | 28 | function __construct(Session $session = NULL) { |
| 29 | - if(is_null($session)) { |
|
| 29 | + if (is_null($session)) { |
|
| 30 | 30 | $request = (new RequestFactory)->createHttpRequest(); |
| 31 | 31 | $response = new Response; |
| 32 | 32 | $session = new Session($request, $response); |
@@ -35,8 +35,8 @@ discard block |
||
| 35 | 35 | $this->section = $session->getSection(get_class($this)); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - function getLang(): ?string { |
|
| 39 | - if(empty($this->section->{$this->varName})) { |
|
| 38 | + function getLang(): ? string { |
|
| 39 | + if (empty($this->section->{$this->varName})) { |
|
| 40 | 40 | return NULL; |
| 41 | 41 | } |
| 42 | 42 | return $this->section->{$this->varName}; |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | /** |
| 58 | 58 | * Resolve language |
| 59 | 59 | */ |
| 60 | - function resolve(): ?string { |
|
| 60 | + function resolve(): ? string { |
|
| 61 | 61 | return $this->getLang(); |
| 62 | 62 | } |
| 63 | 63 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation\Resolvers; |
| 5 | 5 | |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | protected $request; |
| 23 | 23 | |
| 24 | 24 | function __construct(IRequest $request = NULL) { |
| 25 | - if(is_null($request)) { |
|
| 25 | + if (is_null($request)) { |
|
| 26 | 26 | $request = (new RequestFactory)->createHttpRequest(); |
| 27 | 27 | } |
| 28 | 28 | $this->request = $request; |
@@ -38,28 +38,28 @@ discard block |
||
| 38 | 38 | * Taken from Nette\Http\Request::detectLanguage() |
| 39 | 39 | * @author David Grudl |
| 40 | 40 | */ |
| 41 | - function resolve(): ?string { |
|
| 42 | - if(is_null($this->loader)) { |
|
| 41 | + function resolve(): ? string { |
|
| 42 | + if (is_null($this->loader)) { |
|
| 43 | 43 | throw new LoaderNotSetException("Loader is not available, cannot detect possible languages."); |
| 44 | 44 | } |
| 45 | 45 | $header = $this->request->getHeader("Accept-Language"); |
| 46 | 46 | $langs = $this->loader->getAvailableLanguages(); |
| 47 | - if(is_null($header)) { |
|
| 47 | + if (is_null($header)) { |
|
| 48 | 48 | return NULL; |
| 49 | 49 | } |
| 50 | - $s = strtolower($header); // case insensitive |
|
| 51 | - $s = strtr($s, '_', '-'); // cs_CZ means cs-CZ |
|
| 52 | - rsort($langs); // first more specific |
|
| 50 | + $s = strtolower($header); // case insensitive |
|
| 51 | + $s = strtr($s, '_', '-'); // cs_CZ means cs-CZ |
|
| 52 | + rsort($langs); // first more specific |
|
| 53 | 53 | $pattern = ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#'; |
| 54 | 54 | preg_match_all('#(' . implode('|', $langs) . $pattern, $s, $matches); |
| 55 | - if(!$matches[0]) { |
|
| 55 | + if (!$matches[0]) { |
|
| 56 | 56 | return NULL; |
| 57 | 57 | } |
| 58 | 58 | $max = 0; |
| 59 | 59 | $lang = NULL; |
| 60 | - foreach($matches[1] as $key => $value) { |
|
| 60 | + foreach ($matches[1] as $key => $value) { |
|
| 61 | 61 | $q = ($matches[2][$key] === '') ? 1.0 : (float) $matches[2][$key]; |
| 62 | - if($q > $max) { |
|
| 62 | + if ($q > $max) { |
|
| 63 | 63 | $max = $q; |
| 64 | 64 | $lang = $value; |
| 65 | 65 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation\Resolvers; |
| 5 | 5 | |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | * |
| 19 | 19 | * @return NULL |
| 20 | 20 | */ |
| 21 | - function resolve(): ?string { |
|
| 21 | + function resolve(): ? string { |
|
| 22 | 22 | return NULL; |
| 23 | 23 | } |
| 24 | 24 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation\Resolvers; |
| 5 | 5 | |
@@ -12,6 +12,6 @@ discard block |
||
| 12 | 12 | /** |
| 13 | 13 | * Resolve language |
| 14 | 14 | */ |
| 15 | - function resolve(): ?string; |
|
| 15 | + function resolve(): ? string; |
|
| 16 | 16 | } |
| 17 | 17 | ?> |
| 18 | 18 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation; |
| 5 | 5 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | */ |
| 31 | 31 | function __construct(ILoader $loader, string $folder, array $languages = []) { |
| 32 | 32 | $this->loader = $loader; |
| 33 | - if(!count($languages)) { |
|
| 33 | + if (!count($languages)) { |
|
| 34 | 34 | $languages = $loader->getAvailableLanguages(); |
| 35 | 35 | } |
| 36 | 36 | $this->languages = $languages; |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | * Compile catalogues |
| 42 | 42 | */ |
| 43 | 43 | function compile(): void { |
| 44 | - foreach($this->languages as $language) { |
|
| 44 | + foreach ($this->languages as $language) { |
|
| 45 | 45 | $this->loader->setLang($language); |
| 46 | 46 | $texts = $this->loader->getTexts(); |
| 47 | 47 | $texts["__resources"] = $this->loader->getResources(); |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | |
| 4 | 4 | namespace Nexendrie\Translation; |
| 5 | 5 | |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | * @return string[] |
| 52 | 52 | */ |
| 53 | 53 | protected function extractDomainAndMessage(string $message): array { |
| 54 | - if(!Strings::contains($message, ".")) { |
|
| 54 | + if (!Strings::contains($message, ".")) { |
|
| 55 | 55 | return ["messages", $message]; |
| 56 | 56 | } |
| 57 | 57 | return explode(".", $message, 2); |
@@ -62,9 +62,9 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | protected function multiLevelTrans(array $message, array $texts): string { |
| 64 | 64 | $text = $texts; |
| 65 | - foreach($message as $part) { |
|
| 65 | + foreach ($message as $part) { |
|
| 66 | 66 | $text = Arrays::get($text, $part, ""); |
| 67 | - if($text === "") { |
|
| 67 | + if ($text === "") { |
|
| 68 | 68 | break; |
| 69 | 69 | } |
| 70 | 70 | } |
@@ -76,9 +76,9 @@ discard block |
||
| 76 | 76 | */ |
| 77 | 77 | protected function multiChoiceTrans(string $message, int $count): string { |
| 78 | 78 | $variants = explode("|", $message); |
| 79 | - foreach($variants as $variant) { |
|
| 79 | + foreach ($variants as $variant) { |
|
| 80 | 80 | $interval = Intervals::findInterval($variant); |
| 81 | - if(is_string($interval) AND Intervals::isInInterval($count, $interval)) { |
|
| 81 | + if (is_string($interval) AND Intervals::isInInterval($count, $interval)) { |
|
| 82 | 82 | return Strings::trim(Strings::after($variant, $interval)); |
| 83 | 83 | } |
| 84 | 84 | } |
@@ -101,19 +101,19 @@ discard block |
||
| 101 | 101 | list($domain, $m) = $this->extractDomainAndMessage($message); |
| 102 | 102 | $texts = Arrays::get($this->loader->getTexts(), $domain, []); |
| 103 | 103 | $parts = explode(".", $m); |
| 104 | - if(count($parts) === 1) { |
|
| 104 | + if (count($parts) === 1) { |
|
| 105 | 105 | $parts = [$m]; |
| 106 | 106 | } |
| 107 | 107 | $text = $this->multiLevelTrans($parts, $texts); |
| 108 | - if($text === "") { |
|
| 108 | + if ($text === "") { |
|
| 109 | 109 | $this->onUntranslated($message); |
| 110 | 110 | return $message; |
| 111 | 111 | } |
| 112 | - if(is_string(Intervals::findInterval($text))) { |
|
| 112 | + if (is_string(Intervals::findInterval($text))) { |
|
| 113 | 113 | $text = $this->multiChoiceTrans($text, $count); |
| 114 | 114 | } |
| 115 | 115 | $params["count"] = $count; |
| 116 | - foreach($params as $key => $value) { |
|
| 116 | + foreach ($params as $key => $value) { |
|
| 117 | 117 | $text = str_replace("%$key%", $value, $text); |
| 118 | 118 | } |
| 119 | 119 | return $text; |