feature #13981 [Translation] merge all fallback catalogues messages into current catalo... (aitboudad)

This PR was merged into the 2.7 branch.

Discussion
----------

[Translation] merge all fallback catalogues messages into current catalo...

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Fixed tickets  | ~
| Tests pass?   | yes
| License       | MIT

The amount of reduced memory depend on number of defined fallbacks and the size of messages.
For example if we defined 2 fallbacks and we have a large file like ~20 mb we save (~40 mb)
![selection_004](https://cloud.githubusercontent.com/assets/1753742/6737082/3af85be2-ce61-11e4-8104-4330944070cc.png)

Commits
-------

6eb5e73 [Translation] merge all fallback catalogues messages into current catalogue.
This commit is contained in:
Fabien Potencier 2015-03-25 11:10:46 +01:00
commit 37c137acdd

View File

@ -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(<<<EOF
\$catalogue%s = new MessageCatalogue('%s', %s);
\$catalogue%s->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(<<<EOF
<?php
@ -408,7 +386,7 @@ EOF
$catalogue = include $cache;
/**
/*
* Old cache returns only the catalogue, without resourcesHash
*/
$resourcesHash = null;
@ -423,6 +401,51 @@ EOF
$this->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(<<<EOF
\$catalogue%s = new MessageCatalogue('%s', %s);
\$catalogue%s->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])) {