From 6eb5e7395cfb631240804319645b4592cf4fff8f Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Thu, 19 Mar 2015 17:40:41 +0000 Subject: [PATCH] [Translation] merge all fallback catalogues messages into current catalogue. --- .../Component/Translation/Translator.php | 73 ++++++++++++------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index fda8a33e40..001788479f 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -346,7 +346,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface /** * @param string $locale - * @param bool $forceRefresh + * @param bool $forceRefresh */ private function initializeCacheCatalogue($locale, $forceRefresh = false) { @@ -358,29 +358,7 @@ class Translator implements TranslatorInterface, TranslatorBagInterface $cache = new ConfigCache($this->cacheDir.'/catalogue.'.$locale.'.php', $this->debug); if ($forceRefresh || !$cache->isFresh()) { $this->initializeCatalogue($locale); - - $fallbackContent = ''; - $current = ''; - $replacementPattern = '/[^a-z0-9_]/i'; - foreach ($this->computeFallbackLocales($locale) as $fallback) { - $fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback)); - $currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current)); - - $fallbackContent .= sprintf(<<addFallbackCatalogue(\$catalogue%s); - - -EOF - , - $fallbackSuffix, - $fallback, - var_export($this->catalogues[$fallback]->all(), true), - $currentSuffix, - $fallbackSuffix - ); - $current = $fallback; - } + $fallbackContent = $this->getFallbackContent($this->catalogues[$locale]); $content = sprintf(<<catalogues[$locale] = $catalogue; } + private function getFallbackContent(MessageCatalogue $catalogue) + { + if (!$this->debug) { + // merge all fallback catalogues messages into $catalogue + $fallbackCatalogue = $catalogue->getFallbackCatalogue(); + $messages = $catalogue->all(); + while ($fallbackCatalogue) { + $messages = array_replace_recursive($fallbackCatalogue->all(), $messages); + $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); + } + foreach ($messages as $domain => $domainMessages) { + $catalogue->add($domainMessages, $domain); + } + + return ''; + } + + $fallbackContent = ''; + $current = ''; + $replacementPattern = '/[^a-z0-9_]/i'; + $fallbackCatalogue = $catalogue->getFallbackCatalogue(); + while ($fallbackCatalogue) { + $fallback = $fallbackCatalogue->getLocale(); + $fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback)); + $currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current)); + + $fallbackContent .= sprintf(<<addFallbackCatalogue(\$catalogue%s); + +EOF + , + $fallbackSuffix, + $fallback, + var_export($fallbackCatalogue->all(), true), + $currentSuffix, + $fallbackSuffix + ); + $current = $fallbackCatalogue->getLocale(); + $fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue(); + } + + return $fallbackContent; + } + private function getResourcesHash($locale) { if (!isset($this->resources[$locale])) {