8000 [RFC] Translator tweaks by mpdude · Pull Request #14265 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[RFC] Translator tweaks #14265

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Translation\Tests;

use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageSelector;
Expand Down Expand Up @@ -164,6 +165,34 @@ public function testLoadCatalogueWithCachingWithInvalidLocale()
}
}

public function testFallbackCatalogueIsCachedIndependently()
{
$translator = new Translator('a', null, $this->tmpDir);
$translator->setFallbackLocales(array('b', 'c'));

$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', array('foo' => 'foo (a)'), 'a');
$translator->addResource('array', array('foo' => 'foo (b)'), 'b');
$translator->addResource('array', array('bar' => 'bar (b)'), 'b');
$translator->addResource('array', array('baz' => 'baz (c)'), 'c');

$translator->trans('foo'); // Prime the cache with the "a", "b" and "c" catalogues

/*
* Now, a fresh translator should be able to re-use the cached "b" catalogue.
* It does not need to have a loader registered, it can use the cache.
*/
$translator = new Translator('b', null, $this->tmpDir);
$this->assertEquals('bar (b)', $translator->trans('bar'));

/*
* Also, as this second translator does not have "c" as a fallback locale, "baz" from the "c" catalogue
* must not be available. (It was a registered fallback locale *at the time the cache was
* primed*).
*/
$this->assertEquals('baz', $translator->trans('baz'));
}

protected function getCatalogue($locale, $messages)
{
$catalogue = new MessageCatalogue($locale);
Expand Down
96 changes: 36 additions & 60 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,13 @@ protected function initializeCatalogue($locale)
private function initializeCacheCatalogue($locale, $forceRefresh = false)
{
if (isset($this->catalogues[$locale])) {
return;
return; // don't see how this could ever happen...?
Copy link
Contributor

Choose a reason for hiding this comment

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

}

$this->assertValidLocale($locale);
$cache = new ConfigCache($this->cacheDir.'/catalogue.'.$locale.'.php', $this->debug);
if ($forceRefresh || !$cache->isFresh()) {
$this->initializeCatalogue($locale);
$fallbackContent = $this->getFallbackContent($this->catalogues[$locale]);

$content = sprintf(<<<EOF
<?php

Expand All @@ -368,15 +366,13 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false)
\$resourcesHash = '%s';
\$catalogue = new MessageCatalogue('%s', %s);

%s
return array(\$catalogue, \$resourcesHash);

EOF
,
$this->getResourcesHash($locale),
$locale,
var_export($this->catalogues[$locale]->all(), true),
$fallbackContent
var_export($this->catalogues[$locale]->all(), true)
);

$cache->write($content, $this->catalogues[$locale]->getResources());
Expand All @@ -399,51 +395,7 @@ 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;
$this->loadFallbackCatalogues($locale);
}

private function getResourcesHash($locale)
Expand All @@ -455,6 +407,11 @@ private function getResourcesHash($locale)
return sha1(serialize($this->resources[$locale]));
Copy link
Contributor

Choose a reason for hiding this comment

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

this part is important can your revert it ? see #13981

}

/**
* Initializes $this->catalogues[$locale] by running the appropriate loaders.
*
* @param $locale The locale to load the catalogue for
*/
private function doLoadCatalogue($locale)
{
$this->catalogues[$locale] = new MessageCatalogue($locale);
Expand All @@ -471,13 +428,15 @@ private function doLoadCatalogue($locale)

private function loadFallbackCatalogues($locale)
{
$current = $this->catalogues[$locale];

foreach ($this->computeFallbackLocales($locale) as $fallback) {
if (!isset($this->catalogues[$fallback])) {
$this->doLoadCatalogue($fallback);
$this->loadCatalogue($fallback);
}
}

$current = $this->catalogues[$locale];

foreach ($this->computeFallbackLocales($locale) as $fallback) {
$current->addFallbackCatalogue($this->catalogues[$fallback]);
$current = $this->catalogues[$fallback];
}
Expand All @@ -486,19 +445,36 @@ private function loadFallbackCatalogues($locale)
< 8000 /td> protected function computeFallbackLocales($locale)
{
$locales = array();
$candidateLocales = array();
Copy link
Contributor

Choose a reason for hiding this comment

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

is there any benefit of changing the old way ?


if (strrchr($this->locale, '_') !== false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not using here strpos()?

$candidateLocales[] = substr($this->locale, 0, -strlen(strrchr($this->locale, '_')));
}

foreach ($this->fallbackLocales as $fallback) {
$candidateLocales[] = $fallback;
if (strrchr($fallback, '_') !== false) {
$candidateLocales[] = substr($fallback, 0, -strlen(strrchr($fallback, '_')));
}
}

6DAF $candidateLocales = array_unique($candidateLocales);

$findSecondLevelFallback = in_array($locale, $candidateLocales);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why calling loop 2+ times instead of check in loops above directly?

$seenLocale = false;

foreach ($candidateLocales as $fallback) {
if ($fallback === $locale) {
$seenLocale = true;
continue;
}

$locales[] = $fallback;
}

if (strrchr($locale, '_') !== false) {
array_unshift($locales, substr($locale, 0, -strlen(strrchr($locale, '_'))));
if ($seenLocale || !$findSecondLevelFallback) {
$locales[] = $fallback;
}
}

return array_unique($locales);
return $locales;
}

/**
Expand Down
0