Skip to content

[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
wants to merge 14 commits into from
Closed
Prev Previous commit
Next Next commit
For BC, use an adapter class to implement FallbackLocaleAwareInterfac…
…e on top of TranslatorBagInterface
  • Loading branch information
mpdude committed Oct 3, 2015
commit fa055ec4d0b0de6a62ea4ba41a40eb35d778802e
17 changes: 14 additions & 3 deletions src/Symfony/Component/Translation/DataCollectorTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,24 @@ class DataCollectorTranslator implements TranslatorInterface, TranslatorBagInter
*/
private $messages = array();

/**
* @var FallbackLocaleAwareInterface
*/
private $fallbackLocaleAware;

/**
* @param TranslatorInterface $translator The translator must implement TranslatorBagInterface
*/
public function __construct(TranslatorInterface $translator)
{
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)));
Copy link
Contributor Author

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.

}

if (!($translator instanceof FallbackLocaleAwareInterface)) {
$this->fallbackLocaleAware = new TranslatorBagToFallbackLocaleAwareAdapter($translator);
} else {
$this->fallbackLocaleAware = $translator;
}

$this->translator = $translator;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions src/Symfony/Component/Translation/LoggingTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
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;
}
}