-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] Remove TranslatorBagInterface to allow for optimized caching in 3.0 #14530
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
Closed
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
65962c3
Add FallbackLocaleAwareInterface to obtain the locale actually used f…
mpdude 2688b09
Keep test methods around as long as we still implement the interface
mpdude 3077d80
Add note for future me.
mpdude fa055ec
For BC, use an adapter class to implement FallbackLocaleAwareInterfac…
mpdude e4bba37
Add deprecation notices to docblocks
mpdude 372c247
Avoid unnecessary code change
mpdude 98b1a74
use strict comparison
mpdude 89df6f5
Use `if` instead of `elseif` because the `if` returns
mpdude 24f0830
Update legacy tests
mpdude 1364559
Improve deprecation notice
mpdude c0f6bf0
Improve docblock
mpdude 4df5021
Fabbot
mpdude 2a72a09
We must not require clients to implement a deprecated interface
mpdude ad53737
Typo pointed out by FabBot
mpdude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
For BC, use an adapter class to implement FallbackLocaleAwareInterfac…
…e on top of TranslatorBagInterface
- Loading branch information
commit fa055ec4d0b0de6a62ea4ba41a40eb35d778802e
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,14 +28,25 @@ class LoggingTranslator implements TranslatorInterface, TranslatorBagInterface, | |
*/ | ||
private $logger; | ||
|
||
/** | ||
* @var FallbackLocaleAwareInterface | ||
*/ | ||
private $fallbackLocaleAware; | ||
|
||
/** | ||
* @param TranslatorInterface $translator The translator must implement TranslatorBagInterface | ||
* @param LoggerInterface $logger | ||
*/ | ||
public function __construct(TranslatorInterface $translator, LoggerInterface $logger) | ||
{ | ||
if (!($translator instanceof TranslatorBagInterface && $translator instanceof FallbackLocaleAwareInterface)) { | ||
throw new \InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and FallbackLocaleAwareInterface.', get_class($translator))); | ||
if (!($translator instanceof TranslatorBagInterface)) { | ||
throw new \InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', get_class($translator))); | ||
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. Same here |
||
} | ||
|
||
if (!($translator instanceof FallbackLocaleAwareInterface)) { | ||
$this->fallbackLocaleAware = new TranslatorBagToFallbackLocaleAwareAdapter($translator); | ||
} else { | ||
$this->fallbackLocaleAware = $translator; | ||
} | ||
|
||
$this->translator = $translator; | ||
|
@@ -69,7 +80,7 @@ public function transChoice($id, $number, array $parameters = array(), $domain = | |
*/ | ||
public function resolveLocale($id, $domain = null, $locale = null) | ||
{ | ||
return $this->translator->resolveLocale($id, $domain, $locale); | ||
return $this->fallbackLocaleAware->resolveLocale($id, $domain, $locale); | ||
} | ||
|
||
/** | ||
|
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/Translation/TranslatorBagToFallbackLocaleAwareAdapter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation; | ||
|
||
/** | ||
* Implements the FallbackLocaleAwareInterface by using the | ||
* TranslatorBagInterface. | ||
* | ||
* It will iterate along the catalogue chain in order to find the | ||
* first locale that contains a given message. | ||
* | ||
* Needed for a BC transition from Symfony 2.8 to 3.0. To be removed | ||
* Symfony 3.0. | ||
* | ||
* @deprecated | ||
*/ | ||
class TranslatorBagToFallbackLocaleAwareAdapter implements FallbackLocaleAwareInterface | ||
{ | ||
private $translatorBag; | ||
|
||
public function __construct(TranslatorBagInterface $translatorBag) | ||
{ | ||
$this->translatorBag = $translatorBag; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolveLocale($id, $domain = null, $locale = null) | ||
{ | ||
$id = (string)$id; | ||
$catalogue = $this->translatorBag->getCatalogue($locale); | ||
$locale = $catalogue->getLocale(); | ||
|
||
while (!$catalogue->defines($id, $domain)) { | ||
if ($cat = $catalogue->getFallbackCatalogue()) { | ||
$catalogue = $cat; | ||
$locale = $catalogue->getLocale(); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
return $locale; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Need to fix that. The interface is deprecated and must not be required.