Exception
1 year ago
Provider
1 year ago
Test
1 year ago
Util
1 year ago
DataCollectorTranslator.php
1 year ago
IdentityTranslator.php
1 year ago
LoggingTranslator.php
1 year ago
MessageCatalogue.php
1 year ago
MessageCatalogueInterface.php
1 year ago
MetadataAwareInterface.php
1 year ago
PseudoLocalizationTranslator.php
1 year ago
TranslatableMessage.php
1 year ago
Translator.php
1 year ago
TranslatorBag.php
1 year ago
TranslatorBagInterface.php
1 year ago
index.php
3 years ago
Translator.php
297 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Translation; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\Config\ConfigCacheFactory; |
| 5 | use MailPoetVendor\Symfony\Component\Config\ConfigCacheFactoryInterface; |
| 6 | use MailPoetVendor\Symfony\Component\Config\ConfigCacheInterface; |
| 7 | use MailPoetVendor\Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 8 | use MailPoetVendor\Symfony\Component\Translation\Exception\NotFoundResourceException; |
| 9 | use MailPoetVendor\Symfony\Component\Translation\Exception\RuntimeException; |
| 10 | use MailPoetVendor\Symfony\Component\Translation\Formatter\IntlFormatterInterface; |
| 11 | use MailPoetVendor\Symfony\Component\Translation\Formatter\MessageFormatter; |
| 12 | use MailPoetVendor\Symfony\Component\Translation\Formatter\MessageFormatterInterface; |
| 13 | use MailPoetVendor\Symfony\Component\Translation\Loader\LoaderInterface; |
| 14 | use MailPoetVendor\Symfony\Contracts\Translation\LocaleAwareInterface; |
| 15 | use MailPoetVendor\Symfony\Contracts\Translation\TranslatorInterface; |
| 16 | // Help opcache.preload discover always-needed symbols |
| 17 | \class_exists(MessageCatalogue::class); |
| 18 | class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface |
| 19 | { |
| 20 | protected $catalogues = []; |
| 21 | private $locale; |
| 22 | private $fallbackLocales = []; |
| 23 | private $loaders = []; |
| 24 | private $resources = []; |
| 25 | private $formatter; |
| 26 | private $cacheDir; |
| 27 | private $debug; |
| 28 | private $cacheVary; |
| 29 | private $configCacheFactory; |
| 30 | private $parentLocales; |
| 31 | private $hasIntlFormatter; |
| 32 | public function __construct(string $locale, ?MessageFormatterInterface $formatter = null, ?string $cacheDir = null, bool $debug = \false, array $cacheVary = []) |
| 33 | { |
| 34 | $this->setLocale($locale); |
| 35 | if (null === $formatter) { |
| 36 | $formatter = new MessageFormatter(); |
| 37 | } |
| 38 | $this->formatter = $formatter; |
| 39 | $this->cacheDir = $cacheDir; |
| 40 | $this->debug = $debug; |
| 41 | $this->cacheVary = $cacheVary; |
| 42 | $this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface; |
| 43 | } |
| 44 | public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory) |
| 45 | { |
| 46 | $this->configCacheFactory = $configCacheFactory; |
| 47 | } |
| 48 | public function addLoader(string $format, LoaderInterface $loader) |
| 49 | { |
| 50 | $this->loaders[$format] = $loader; |
| 51 | } |
| 52 | public function addResource(string $format, $resource, string $locale, ?string $domain = null) |
| 53 | { |
| 54 | if (null === $domain) { |
| 55 | $domain = 'messages'; |
| 56 | } |
| 57 | $this->assertValidLocale($locale); |
| 58 | $locale ?: ($locale = \class_exists(\Locale::class) ? \Locale::getDefault() : 'en'); |
| 59 | $this->resources[$locale][] = [$format, $resource, $domain]; |
| 60 | if (\in_array($locale, $this->fallbackLocales)) { |
| 61 | $this->catalogues = []; |
| 62 | } else { |
| 63 | unset($this->catalogues[$locale]); |
| 64 | } |
| 65 | } |
| 66 | public function setLocale(string $locale) |
| 67 | { |
| 68 | $this->assertValidLocale($locale); |
| 69 | $this->locale = $locale; |
| 70 | } |
| 71 | public function getLocale() |
| 72 | { |
| 73 | return $this->locale ?: (\class_exists(\Locale::class) ? \Locale::getDefault() : 'en'); |
| 74 | } |
| 75 | public function setFallbackLocales(array $locales) |
| 76 | { |
| 77 | // needed as the fallback locales are linked to the already loaded catalogues |
| 78 | $this->catalogues = []; |
| 79 | foreach ($locales as $locale) { |
| 80 | $this->assertValidLocale($locale); |
| 81 | } |
| 82 | $this->fallbackLocales = $this->cacheVary['fallback_locales'] = $locales; |
| 83 | } |
| 84 | public function getFallbackLocales() : array |
| 85 | { |
| 86 | return $this->fallbackLocales; |
| 87 | } |
| 88 | public function trans(?string $id, array $parameters = [], ?string $domain = null, ?string $locale = null) |
| 89 | { |
| 90 | if (null === $id || '' === $id) { |
| 91 | return ''; |
| 92 | } |
| 93 | if (null === $domain) { |
| 94 | $domain = 'messages'; |
| 95 | } |
| 96 | $catalogue = $this->getCatalogue($locale); |
| 97 | $locale = $catalogue->getLocale(); |
| 98 | while (!$catalogue->defines($id, $domain)) { |
| 99 | if ($cat = $catalogue->getFallbackCatalogue()) { |
| 100 | $catalogue = $cat; |
| 101 | $locale = $catalogue->getLocale(); |
| 102 | } else { |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | $len = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX); |
| 107 | if ($this->hasIntlFormatter && ($catalogue->defines($id, $domain . MessageCatalogue::INTL_DOMAIN_SUFFIX) || \strlen($domain) > $len && 0 === \substr_compare($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX, -$len, $len))) { |
| 108 | return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, $parameters); |
| 109 | } |
| 110 | return $this->formatter->format($catalogue->get($id, $domain), $locale, $parameters); |
| 111 | } |
| 112 | public function getCatalogue(?string $locale = null) |
| 113 | { |
| 114 | if (!$locale) { |
| 115 | $locale = $this->getLocale(); |
| 116 | } else { |
| 117 | $this->assertValidLocale($locale); |
| 118 | } |
| 119 | if (!isset($this->catalogues[$locale])) { |
| 120 | $this->loadCatalogue($locale); |
| 121 | } |
| 122 | return $this->catalogues[$locale]; |
| 123 | } |
| 124 | public function getCatalogues() : array |
| 125 | { |
| 126 | return \array_values($this->catalogues); |
| 127 | } |
| 128 | protected function getLoaders() |
| 129 | { |
| 130 | return $this->loaders; |
| 131 | } |
| 132 | protected function loadCatalogue(string $locale) |
| 133 | { |
| 134 | if (null === $this->cacheDir) { |
| 135 | $this->initializeCatalogue($locale); |
| 136 | } else { |
| 137 | $this->initializeCacheCatalogue($locale); |
| 138 | } |
| 139 | } |
| 140 | protected function initializeCatalogue(string $locale) |
| 141 | { |
| 142 | $this->assertValidLocale($locale); |
| 143 | try { |
| 144 | $this->doLoadCatalogue($locale); |
| 145 | } catch (NotFoundResourceException $e) { |
| 146 | if (!$this->computeFallbackLocales($locale)) { |
| 147 | throw $e; |
| 148 | } |
| 149 | } |
| 150 | $this->loadFallbackCatalogues($locale); |
| 151 | } |
| 152 | private function initializeCacheCatalogue(string $locale) : void |
| 153 | { |
| 154 | if (isset($this->catalogues[$locale])) { |
| 155 | return; |
| 156 | } |
| 157 | $this->assertValidLocale($locale); |
| 158 | $cache = $this->getConfigCacheFactory()->cache($this->getCatalogueCachePath($locale), function (ConfigCacheInterface $cache) use($locale) { |
| 159 | $this->dumpCatalogue($locale, $cache); |
| 160 | }); |
| 161 | if (isset($this->catalogues[$locale])) { |
| 162 | return; |
| 163 | } |
| 164 | $this->catalogues[$locale] = (include $cache->getPath()); |
| 165 | } |
| 166 | private function dumpCatalogue(string $locale, ConfigCacheInterface $cache) : void |
| 167 | { |
| 168 | $this->initializeCatalogue($locale); |
| 169 | $fallbackContent = $this->getFallbackContent($this->catalogues[$locale]); |
| 170 | $content = \sprintf(<<<EOF |
| 171 | <?php |
| 172 | use Symfony\\Component\\Translation\\MessageCatalogue; |
| 173 | \$catalogue = new MessageCatalogue('%s', %s); |
| 174 | %s |
| 175 | return \$catalogue; |
| 176 | EOF |
| 177 | , $locale, \var_export($this->getAllMessages($this->catalogues[$locale]), \true), $fallbackContent); |
| 178 | $cache->write($content, $this->catalogues[$locale]->getResources()); |
| 179 | } |
| 180 | private function getFallbackContent(MessageCatalogue $catalogue) : string |
| 181 | { |
| 182 | $fallbackContent = ''; |
| 183 | $current = ''; |
| 184 | $replacementPattern = '/[^a-z0-9_]/i'; |
| 185 | $fallbackCatalogue = $catalogue->getFallbackCatalogue(); |
| 186 | while ($fallbackCatalogue) { |
| 187 | $fallback = $fallbackCatalogue->getLocale(); |
| 188 | $fallbackSuffix = \ucfirst(\preg_replace($replacementPattern, '_', $fallback)); |
| 189 | $currentSuffix = \ucfirst(\preg_replace($replacementPattern, '_', $current)); |
| 190 | $fallbackContent .= \sprintf(<<<'EOF' |
| 191 | $catalogue%s = new MessageCatalogue('%s', %s); |
| 192 | $catalogue%s->addFallbackCatalogue($catalogue%s); |
| 193 | EOF |
| 194 | , $fallbackSuffix, $fallback, \var_export($this->getAllMessages($fallbackCatalogue), \true), $currentSuffix, $fallbackSuffix); |
| 195 | $current = $fallbackCatalogue->getLocale(); |
| 196 | $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); |
| 197 | } |
| 198 | return $fallbackContent; |
| 199 | } |
| 200 | private function getCatalogueCachePath(string $locale) : string |
| 201 | { |
| 202 | return $this->cacheDir . '/catalogue.' . $locale . '.' . \strtr(\substr(\base64_encode(\hash('sha256', \serialize($this->cacheVary), \true)), 0, 7), '/', '_') . '.php'; |
| 203 | } |
| 204 | protected function doLoadCatalogue(string $locale) : void |
| 205 | { |
| 206 | $this->catalogues[$locale] = new MessageCatalogue($locale); |
| 207 | if (isset($this->resources[$locale])) { |
| 208 | foreach ($this->resources[$locale] as $resource) { |
| 209 | if (!isset($this->loaders[$resource[0]])) { |
| 210 | if (\is_string($resource[1])) { |
| 211 | throw new RuntimeException(\sprintf('No loader is registered for the "%s" format when loading the "%s" resource.', $resource[0], $resource[1])); |
| 212 | } |
| 213 | throw new RuntimeException(\sprintf('No loader is registered for the "%s" format.', $resource[0])); |
| 214 | } |
| 215 | $this->catalogues[$locale]->addCatalogue($this->loaders[$resource[0]]->load($resource[1], $locale, $resource[2])); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | private function loadFallbackCatalogues(string $locale) : void |
| 220 | { |
| 221 | $current = $this->catalogues[$locale]; |
| 222 | foreach ($this->computeFallbackLocales($locale) as $fallback) { |
| 223 | if (!isset($this->catalogues[$fallback])) { |
| 224 | $this->initializeCatalogue($fallback); |
| 225 | } |
| 226 | $fallbackCatalogue = new MessageCatalogue($fallback, $this->getAllMessages($this->catalogues[$fallback])); |
| 227 | foreach ($this->catalogues[$fallback]->getResources() as $resource) { |
| 228 | $fallbackCatalogue->addResource($resource); |
| 229 | } |
| 230 | $current->addFallbackCatalogue($fallbackCatalogue); |
| 231 | $current = $fallbackCatalogue; |
| 232 | } |
| 233 | } |
| 234 | protected function computeFallbackLocales(string $locale) |
| 235 | { |
| 236 | if (null === $this->parentLocales) { |
| 237 | $this->parentLocales = \json_decode(\file_get_contents(__DIR__ . '/Resources/data/parents.json'), \true); |
| 238 | } |
| 239 | $originLocale = $locale; |
| 240 | $locales = []; |
| 241 | while ($locale) { |
| 242 | $parent = $this->parentLocales[$locale] ?? null; |
| 243 | if ($parent) { |
| 244 | $locale = 'root' !== $parent ? $parent : null; |
| 245 | } elseif (\function_exists('locale_parse')) { |
| 246 | $localeSubTags = \locale_parse($locale); |
| 247 | $locale = null; |
| 248 | if (1 < \count($localeSubTags)) { |
| 249 | \array_pop($localeSubTags); |
| 250 | $locale = \locale_compose($localeSubTags) ?: null; |
| 251 | } |
| 252 | } elseif ($i = \strrpos($locale, '_') ?: \strrpos($locale, '-')) { |
| 253 | $locale = \substr($locale, 0, $i); |
| 254 | } else { |
| 255 | $locale = null; |
| 256 | } |
| 257 | if (null !== $locale) { |
| 258 | $locales[] = $locale; |
| 259 | } |
| 260 | } |
| 261 | foreach ($this->fallbackLocales as $fallback) { |
| 262 | if ($fallback === $originLocale) { |
| 263 | continue; |
| 264 | } |
| 265 | $locales[] = $fallback; |
| 266 | } |
| 267 | return \array_unique($locales); |
| 268 | } |
| 269 | protected function assertValidLocale(string $locale) |
| 270 | { |
| 271 | if (!\preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) { |
| 272 | throw new InvalidArgumentException(\sprintf('Invalid "%s" locale.', $locale)); |
| 273 | } |
| 274 | } |
| 275 | private function getConfigCacheFactory() : ConfigCacheFactoryInterface |
| 276 | { |
| 277 | if (!$this->configCacheFactory) { |
| 278 | $this->configCacheFactory = new ConfigCacheFactory($this->debug); |
| 279 | } |
| 280 | return $this->configCacheFactory; |
| 281 | } |
| 282 | private function getAllMessages(MessageCatalogueInterface $catalogue) : array |
| 283 | { |
| 284 | $allMessages = []; |
| 285 | foreach ($catalogue->all() as $domain => $messages) { |
| 286 | if ($intlMessages = $catalogue->all($domain . MessageCatalogue::INTL_DOMAIN_SUFFIX)) { |
| 287 | $allMessages[$domain . MessageCatalogue::INTL_DOMAIN_SUFFIX] = $intlMessages; |
| 288 | $messages = \array_diff_key($messages, $intlMessages); |
| 289 | } |
| 290 | if ($messages) { |
| 291 | $allMessages[$domain] = $messages; |
| 292 | } |
| 293 | } |
| 294 | return $allMessages; |
| 295 | } |
| 296 | } |
| 297 |