-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
[RFC] Translator tweaks #14265
Changes from all commits
File filter
Filter by extension
Conversations
Jump to 8000
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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...? | ||
} | ||
|
||
$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 | ||
|
||
|
@@ -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()); | ||
|
@@ -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) | ||
|
@@ -455,6 +407,11 @@ private function getResourcesHash($locale) | |
return sha1(serialize($this->resources[$locale])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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]; | ||
} | ||
|
@@ -486,19 +445,36 @@ private function loadFallbackCatalogues($locale) | |
< 8000 /td> | protected function computeFallbackLocales($locale) | |
{ | ||
$locales = array(); | ||
$candidateLocales = array(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not using here |
||
$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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can delete it as it useless https://github.com/webfactory/symfony/blob/translator-tweaks/src/Symfony/Component/Translation/Translator.php#L353-L355