8000 feature #13981 [Translation] merge all fallback catalogues messages i… · symfony/symfony@37c137a · GitHub
[go: up one dir, main page]

Skip to content

Commit 37c137a

Browse files
committed
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.
2 parents 78b1a68 + 6eb5e73 commit 37c137a

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

src/Symfony/Component/Translation/Translator.php

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ protected function initializeCatalogue($locale)
346346

347347
/**
348348
* @param string $locale
349-
* @param bool $forceRefresh
349+
* @param bool $forceRefresh
350350
*/
351351
private function initializeCacheCatalogue($locale, $forceRefresh = false)
352352
{
@@ -358,29 +358,7 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false)
358358
$cache = new ConfigCache($this->cacheDir.'/catalogue.'.$locale.'.php', $this->debug);
359359
if ($forceRefresh || !$cache->isFresh()) {
360360
$this->initializeCatalogue($locale);
361-
362-
$fallbackContent = '';
363-
$current = '';
364-
$replacementPattern = '/[^a-z0-9_]/i';
365-
foreach ($this->computeFallbackLocales($locale) as $fallback) {
366-
$fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
367-
$currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
368-
369-
$fallbackContent .= sprintf(<<<EOF
370-
\$catalogue%s = new MessageCatalogue('%s', %s);
371-
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
372-
373-
374-
EOF
375-
,
376-
$fallbackSuffix,
377-
$fallback,
378-
var_export($this->catalogues[$fallback]->all(), true),
379-
$currentSuffix,
380-
$fallbackSuffix
381-
);
382-
$current = $fallback;
383-
}
361+
$fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);
384362

385363
$content = sprintf(<<<EOF
386364
<?php
@@ -408,7 +386,7 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false)
408386

409387
$catalogue = include $cache;
410388

411-
/**
389+
/*
412390
* Old cache returns only the catalogue, without resourcesHash
413391
*/
414392
$resourcesHash = null;
@@ -423,6 +401,51 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false)
423401
$this->catalogues[$locale] = $catalogue;
424402
}
425403

404+
private function getFallbackContent(MessageCatalogue $catalogue)
405+
{
406+
if (!$this->debug) {
407+
// merge all fallback catalogues messages into $catalogue
408+
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
409+
$messages = $catalogue->all();
410+
while ($fallbackCatalogue) {
411+
$messages = array_replace_recursive($fallbackCatalogue->all(), $messages);
412+
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
413+
}
414+
foreach ($messages as $domain => $domainMessages) {
415+
$catalogue->add($domainMessages, $domain);
416+
}
417+
418+
return '';
419+
}
420+
421+
$fallbackContent = '';
422+
$current = '';
423+
$replacementPattern = '/[^a-z0-9_]/i';
424+
$fallbackCatalogue = $catalogue->getFallbackCatalogue();
425+
while ($fallbackCatalogue) {
426+
$fallback = $fallbackCatalogue->getLocale();
427+
$fallbackSuffix = ucfirst(preg_replace($replacementPattern, '_', $fallback));
428+
$currentSuffix = ucfirst(preg_replace($replacementPattern, '_', $current));
429+
430+
$fallbackContent .= sprintf(<<<EOF
431+
\$catalogue%s = new MessageCatalogue('%s', %s);
432+
\$catalogue%s->addFallbackCatalogue(\$catalogue%s);
433+
434+
EOF
435+
,
436+
$fallbackSuffix,
437+
$fallback,
438+
var_export($fallbackCatalogue->all(), true),
439+
$currentSuffix,
440+
$fallbackSuffix
441+
);
442+
$current = $fallbackCatalogue->getLocale();
443+
$fallbackCatalogue = $fallbackCatalogue->getFallbackCatalogue();
444+
}
445+
446+
return $fallbackContent;
447+
}
448+
426449
private function getResourcesHash($locale)
427450
{
428451
if (!isset($this->resources[$locale])) {

0 commit comments

Comments
 (0)
0