Skip to content

Commit

Permalink
feature #13981 [Translation] merge all fallback catalogues messages i…
Browse files Browse the repository at this point in the history
…nto 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.
  • Loading branch information
fabpot committed Mar 25, 2015
2 parents 78b1a68 + 6eb5e73 commit 37c137a
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions src/Symfony/Component/Translation/Translator.php
Expand Up @@ -346,7 +346,7 @@ protected function initializeCatalogue($locale)

/**
* @param string $locale
* @param bool $forceRefresh
* @param bool $forceRefresh
*/
private function initializeCacheCatalogue($locale, $forceRefresh = false)
{
Expand All @@ -358,29 +358,7 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false)
$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
Expand Down Expand Up @@ -408,7 +386,7 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false)

$catalogue = include $cache;

/**
/*
* Old cache returns only the catalogue, without resourcesHash
*/
$resourcesHash = null;
Expand All @@ -423,6 +401,51 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false)
$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])) {
Expand Down

0 comments on commit 37c137a

Please sign in to comment.