Exceptions
3 years ago
Lang
3 years ago
List
3 years ago
Traits
3 years ago
AbstractTranslator.php
3 years ago
Carbon.php
4 years ago
CarbonConverterInterface.php
4 years ago
CarbonImmutable.php
4 years ago
CarbonInterface.php
3 years ago
CarbonInterval.php
3 years ago
CarbonPeriod.php
3 years ago
CarbonTimeZone.php
3 years ago
Factory.php
4 years ago
FactoryImmutable.php
4 years ago
Language.php
4 years ago
Translator.php
4 years ago
TranslatorImmutable.php
4 years ago
TranslatorStrongTypeInterface.php
4 years ago
index.php
3 years ago
AbstractTranslator.php
190 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use Closure; |
| 5 | use ReflectionException; |
| 6 | use ReflectionFunction; |
| 7 | use MailPoetVendor\Symfony\Component\Translation; |
| 8 | use MailPoetVendor\Symfony\Component\Translation\Formatter\MessageFormatterInterface; |
| 9 | use MailPoetVendor\Symfony\Component\Translation\Loader\ArrayLoader; |
| 10 | abstract class AbstractTranslator extends Translation\Translator |
| 11 | { |
| 12 | protected static $singletons = []; |
| 13 | protected $messages = []; |
| 14 | protected $directories = []; |
| 15 | protected $initializing = \false; |
| 16 | protected $aliases = ['me' => 'sr_Latn_ME', 'scr' => 'sh']; |
| 17 | public static function get($locale = null) |
| 18 | { |
| 19 | $locale = $locale ?: 'en'; |
| 20 | $key = static::class === Translator::class ? $locale : static::class . '|' . $locale; |
| 21 | if (!isset(static::$singletons[$key])) { |
| 22 | static::$singletons[$key] = new static($locale); |
| 23 | } |
| 24 | return static::$singletons[$key]; |
| 25 | } |
| 26 | public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = \false) |
| 27 | { |
| 28 | parent::setLocale($locale); |
| 29 | $this->initializing = \true; |
| 30 | $this->directories = [__DIR__ . '/Lang']; |
| 31 | $this->addLoader('array', new ArrayLoader()); |
| 32 | parent::__construct($locale, $formatter, $cacheDir, $debug); |
| 33 | $this->initializing = \false; |
| 34 | } |
| 35 | public function getDirectories() : array |
| 36 | { |
| 37 | return $this->directories; |
| 38 | } |
| 39 | public function setDirectories(array $directories) |
| 40 | { |
| 41 | $this->directories = $directories; |
| 42 | return $this; |
| 43 | } |
| 44 | public function addDirectory(string $directory) |
| 45 | { |
| 46 | $this->directories[] = $directory; |
| 47 | return $this; |
| 48 | } |
| 49 | public function removeDirectory(string $directory) |
| 50 | { |
| 51 | $search = \rtrim(\strtr($directory, '\\', '/'), '/'); |
| 52 | return $this->setDirectories(\array_filter($this->getDirectories(), function ($item) use($search) { |
| 53 | return \rtrim(\strtr($item, '\\', '/'), '/') !== $search; |
| 54 | })); |
| 55 | } |
| 56 | public function resetMessages($locale = null) |
| 57 | { |
| 58 | if ($locale === null) { |
| 59 | $this->messages = []; |
| 60 | return \true; |
| 61 | } |
| 62 | foreach ($this->getDirectories() as $directory) { |
| 63 | $data = @(include \sprintf('%s/%s.php', \rtrim($directory, '\\/'), $locale)); |
| 64 | if ($data !== \false) { |
| 65 | $this->messages[$locale] = $data; |
| 66 | $this->addResource('array', $this->messages[$locale], $locale); |
| 67 | return \true; |
| 68 | } |
| 69 | } |
| 70 | return \false; |
| 71 | } |
| 72 | public function getLocalesFiles($prefix = '') |
| 73 | { |
| 74 | $files = []; |
| 75 | foreach ($this->getDirectories() as $directory) { |
| 76 | $directory = \rtrim($directory, '\\/'); |
| 77 | foreach (\glob("{$directory}/{$prefix}*.php") as $file) { |
| 78 | $files[] = $file; |
| 79 | } |
| 80 | } |
| 81 | return \array_unique($files); |
| 82 | } |
| 83 | public function getAvailableLocales($prefix = '') |
| 84 | { |
| 85 | $locales = []; |
| 86 | foreach ($this->getLocalesFiles($prefix) as $file) { |
| 87 | $locales[] = \substr($file, \strrpos($file, '/') + 1, -4); |
| 88 | } |
| 89 | return \array_unique(\array_merge($locales, \array_keys($this->messages))); |
| 90 | } |
| 91 | protected function translate(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null) : string |
| 92 | { |
| 93 | if ($domain === null) { |
| 94 | $domain = 'messages'; |
| 95 | } |
| 96 | $catalogue = $this->getCatalogue($locale); |
| 97 | $format = $this instanceof TranslatorStrongTypeInterface ? $this->getFromCatalogue($catalogue, (string) $id, $domain) : $this->getCatalogue($locale)->get((string) $id, $domain); |
| 98 | // @codeCoverageIgnore |
| 99 | if ($format instanceof Closure) { |
| 100 | // @codeCoverageIgnoreStart |
| 101 | try { |
| 102 | $count = (new ReflectionFunction($format))->getNumberOfRequiredParameters(); |
| 103 | } catch (ReflectionException $exception) { |
| 104 | $count = 0; |
| 105 | } |
| 106 | // @codeCoverageIgnoreEnd |
| 107 | return $format(...\array_values($parameters), ...\array_fill(0, \max(0, $count - \count($parameters)), null)); |
| 108 | } |
| 109 | return parent::trans($id, $parameters, $domain, $locale); |
| 110 | } |
| 111 | protected function loadMessagesFromFile($locale) |
| 112 | { |
| 113 | return isset($this->messages[$locale]) || $this->resetMessages($locale); |
| 114 | } |
| 115 | public function setMessages($locale, $messages) |
| 116 | { |
| 117 | $this->loadMessagesFromFile($locale); |
| 118 | $this->addResource('array', $messages, $locale); |
| 119 | $this->messages[$locale] = \array_merge($this->messages[$locale] ?? [], $messages); |
| 120 | return $this; |
| 121 | } |
| 122 | public function setTranslations($messages) |
| 123 | { |
| 124 | return $this->setMessages($this->getLocale(), $messages); |
| 125 | } |
| 126 | public function getMessages($locale = null) |
| 127 | { |
| 128 | return $locale === null ? $this->messages : $this->messages[$locale]; |
| 129 | } |
| 130 | public function setLocale($locale) |
| 131 | { |
| 132 | $locale = \preg_replace_callback('/[-_]([a-z]{2,}|\\d{2,})/', function ($matches) { |
| 133 | // _2-letters or YUE is a region, _3+-letters is a variant |
| 134 | $upper = \strtoupper($matches[1]); |
| 135 | if ($upper === 'YUE' || $upper === 'ISO' || \strlen($upper) < 3) { |
| 136 | return "_{$upper}"; |
| 137 | } |
| 138 | return '_' . \ucfirst($matches[1]); |
| 139 | }, \strtolower($locale)); |
| 140 | $previousLocale = $this->getLocale(); |
| 141 | if ($previousLocale === $locale && isset($this->messages[$locale])) { |
| 142 | return \true; |
| 143 | } |
| 144 | unset(static::$singletons[$previousLocale]); |
| 145 | if ($locale === 'auto') { |
| 146 | $completeLocale = \setlocale(\LC_TIME, '0'); |
| 147 | $locale = \preg_replace('/^([^_.-]+).*$/', '$1', $completeLocale); |
| 148 | $locales = $this->getAvailableLocales($locale); |
| 149 | $completeLocaleChunks = \preg_split('/[_.-]+/', $completeLocale); |
| 150 | $getScore = function ($language) use($completeLocaleChunks) { |
| 151 | return self::compareChunkLists($completeLocaleChunks, \preg_split('/[_.-]+/', $language)); |
| 152 | }; |
| 153 | \usort($locales, function ($first, $second) use($getScore) { |
| 154 | return $getScore($second) <=> $getScore($first); |
| 155 | }); |
| 156 | $locale = $locales[0]; |
| 157 | } |
| 158 | if (isset($this->aliases[$locale])) { |
| 159 | $locale = $this->aliases[$locale]; |
| 160 | } |
| 161 | // If subtag (ex: en_CA) first load the macro (ex: en) to have a fallback |
| 162 | if (\str_contains($locale, '_') && $this->loadMessagesFromFile($macroLocale = \preg_replace('/^([^_]+).*$/', '$1', $locale))) { |
| 163 | parent::setLocale($macroLocale); |
| 164 | } |
| 165 | if (!$this->loadMessagesFromFile($locale) && !$this->initializing) { |
| 166 | return \false; |
| 167 | } |
| 168 | parent::setLocale($locale); |
| 169 | return \true; |
| 170 | } |
| 171 | public function __debugInfo() |
| 172 | { |
| 173 | return ['locale' => $this->getLocale()]; |
| 174 | } |
| 175 | private static function compareChunkLists($referenceChunks, $chunks) |
| 176 | { |
| 177 | $score = 0; |
| 178 | foreach ($referenceChunks as $index => $chunk) { |
| 179 | if (!isset($chunks[$index])) { |
| 180 | $score++; |
| 181 | continue; |
| 182 | } |
| 183 | if (\strtolower($chunks[$index]) === \strtolower($chunk)) { |
| 184 | $score += 10; |
| 185 | } |
| 186 | } |
| 187 | return $score; |
| 188 | } |
| 189 | } |
| 190 |