Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add return ''; here and remove the else.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


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