diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml index 1c6eb323b0fb4..fb7dba41fce33 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml @@ -45,6 +45,7 @@ %kernel.debug% + @@ -60,46 +61,57 @@ + + + + + + + + + + + @@ -151,6 +163,8 @@ + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php index 8e9843931d500..d18fa6ddb8d40 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php @@ -14,6 +14,7 @@ use Symfony\Component\Translation\Translator as BaseTranslator; use Symfony\Component\Translation\MessageSelector; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Translation\Catalogue\CatalogueFactoryInterface; /** * Translator. @@ -47,7 +48,7 @@ class Translator extends BaseTranslator * * @throws \InvalidArgumentException */ - public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array()) + public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array(), CatalogueFactoryInterface $catalogueFactory = null) { $this->container = $container; $this->loaderIds = $loaderIds; @@ -60,7 +61,7 @@ public function __construct(ContainerInterface $container, MessageSelector $sele $this->options = array_merge($this->options, $options); - parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug']); + parent::__construct(null, $selector, $this->options['cache_dir'], $this->options['debug'], $catalogueFactory); } /** diff --git a/src/Symfony/Component/Translation/Catalogue/AbstractOperation.php b/src/Symfony/Component/Translation/Catalogue/AbstractOperation.php index 062056b7317c4..a12ed37a7972a 100644 --- a/src/Symfony/Component/Translation/Catalogue/AbstractOperation.php +++ b/src/Symfony/Component/Translation/Catalogue/AbstractOperation.php @@ -49,10 +49,11 @@ abstract class AbstractOperation implements OperationInterface /** * @param MessageCatalogueInterface $source * @param MessageCatalogueInterface $target + * @param CatalogueFactoryInterface $catalogueFactory * * @throws \LogicException */ - public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target) + public function __construct(MessageCatalogueInterface $source, MessageCatalogueInterface $target, CatalogueFactoryInterface $catalogueFactory = null) { if ($source->getLocale() !== $target->getLocale()) { throw new \LogicException('Operated catalogues must belong to the same locale.'); @@ -60,7 +61,9 @@ public function __construct(MessageCatalogueInterface $source, MessageCatalogueI $this->source = $source; $this->target = $target; - $this->result = new MessageCatalogue($source->getLocale()); + + $catalogueFactory = $catalogueFactory ?: new CatalogueFactory(); + $this->result = $catalogueFactory->create($source->getLocale()); $this->domains = null; $this->messages = array(); } diff --git a/src/Symfony/Component/Translation/Catalogue/CatalogueFactory.php b/src/Symfony/Component/Translation/Catalogue/CatalogueFactory.php new file mode 100644 index 0000000000000..b8d23bae7402d --- /dev/null +++ b/src/Symfony/Component/Translation/Catalogue/CatalogueFactory.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Catalogue; + +use Symfony\Component\Translation\MessageCatalogue; + +/** + * @author Abdellatif Ait Boudad + */ +class CatalogueFactory implements CatalogueFactoryInterface +{ + /** + * {@inheritdoc} + */ + public function create($locale, array $messages = array()) + { + return new MessageCatalogue($locale, $messages); + } +} diff --git a/src/Symfony/Component/Translation/Catalogue/CatalogueFactoryInterface.php b/src/Symfony/Component/Translation/Catalogue/CatalogueFactoryInterface.php new file mode 100644 index 0000000000000..6bd488295a761 --- /dev/null +++ b/src/Symfony/Component/Translation/Catalogue/CatalogueFactoryInterface.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Catalogue; + +/** + * @author Abdellatif Ait boudad + */ +interface CatalogueFactoryInterface +{ + /** + * @param string $locale The locale + * @param array $messages An array of messages classified by domain + */ + public function create($locale, array $messages = array()); +} diff --git a/src/Symfony/Component/Translation/Loader/AbstractLoader.php b/src/Symfony/Component/Translation/Loader/AbstractLoader.php new file mode 100644 index 0000000000000..d336ae15eae4f --- /dev/null +++ b/src/Symfony/Component/Translation/Loader/AbstractLoader.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Loader; + +use Symfony\Component\Translation\Catalogue\CatalogueFactoryInterface; +use Symfony\Component\Translation\Catalogue\CatalogueFactory; + +/** + * @author Abdellatif Ait Boudad + */ +abstract class AbstractLoader implements LoaderInterface +{ + /** + * @var CatalogueFactoryInterface + */ + protected $catalogueFactory; + + /** + * @param CatalogueFactoryInterface $catalogueFactory + */ + public function __construct(CatalogueFactoryInterface $catalogueFactory = null) + { + $this->catalogueFactory = $catalogueFactory ?: new CatalogueFactory(); + } +} diff --git a/src/Symfony/Component/Translation/Loader/ArrayLoader.php b/src/Symfony/Component/Translation/Loader/ArrayLoader.php index ba4003bfc1a21..af3a039aaaba7 100644 --- a/src/Symfony/Component/Translation/Loader/ArrayLoader.php +++ b/src/Symfony/Component/Translation/Loader/ArrayLoader.php @@ -11,8 +11,6 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\MessageCatalogue; - /** * ArrayLoader loads translations from a PHP array. * @@ -20,7 +18,7 @@ * * @api */ -class ArrayLoader implements LoaderInterface +class ArrayLoader extends AbstractLoader implements LoaderInterface { /** * {@inheritdoc} @@ -30,7 +28,7 @@ class ArrayLoader implements LoaderInterface public function load($resource, $locale, $domain = 'messages') { $this->flatten($resource); - $catalogue = new MessageCatalogue($locale); + $catalogue = $this->catalogueFactory->create($locale); $catalogue->add($resource, $domain); return $catalogue; diff --git a/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php b/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php index a36cd76e71d3b..958571cba4ac9 100644 --- a/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; @@ -50,7 +49,7 @@ public function load($resource, $locale, $domain = 'messages') } $messages = $this->flatten($rb); - $catalogue = new MessageCatalogue($locale); + $catalogue = $this->catalogueFactory->create($locale); $catalogue->add($messages, $domain); $catalogue->addResource(new FileResource($resource.'.dat')); diff --git a/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php b/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php index d864c7bd0c32c..c51fe400738a9 100644 --- a/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Translation\Loader; -use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\DirectoryResource; @@ -21,7 +20,7 @@ * * @author stealth35 */ -class IcuResFileLoader implements LoaderInterface +class IcuResFileLoader extends AbstractLoader implements LoaderInterface { /** * {@inheritdoc} @@ -50,7 +49,7 @@ public function load($resource, $locale, $domain = 'messages') } $messages = $this->flatten($rb); - $catalogue = new MessageCatalogue($locale); + $catalogue = $this->catalogueFactory->create($locale); $catalogue->add($messages, $domain); $catalogue->addResource(new DirectoryResource($resource)); diff --git a/src/Symfony/Component/Translation/Loader/QtFileLoader.php b/src/Symfony/Component/Translation/Loader/QtFileLoader.php index aacfb4a55e59f..03a73308e193e 100644 --- a/src/Symfony/Component/Translation/Loader/QtFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/QtFileLoader.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Config\Util\XmlUtils; -use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; @@ -24,7 +23,7 @@ * * @api */ -class QtFileLoader implements LoaderInterface +class QtFileLoader extends AbstractLoader implements LoaderInterface { /** * {@inheritdoc} @@ -53,7 +52,7 @@ public function load($resource, $locale, $domain = 'messages') $xpath = new \DOMXPath($dom); $nodes = $xpath->evaluate('//TS/context/name[text()="'.$domain.'"]'); - $catalogue = new MessageCatalogue($locale); + $catalogue = $this->catalogueFactory->create($locale); if ($nodes->length == 1) { $translations = $nodes->item(0)->nextSibling->parentNode->parentNode->getElementsByTagName('message'); foreach ($translations as $translation) { diff --git a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php index c3a50cd8bb77b..fa0261abc6a86 100644 --- a/src/Symfony/Component/Translation/Loader/XliffFileLoader.php +++ b/src/Symfony/Component/Translation/Loader/XliffFileLoader.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Translation\Loader; use Symfony\Component\Config\Util\XmlUtils; -use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\Exception\InvalidResourceException; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\Resource\FileResource; @@ -24,7 +23,7 @@ * * @api */ -class XliffFileLoader implements LoaderInterface +class XliffFileLoader extends AbstractLoader implements LoaderInterface { /** * {@inheritdoc} @@ -44,7 +43,7 @@ public function load($resource, $locale, $domain = 'messages') list($xml, $encoding) = $this->parseFile($resource); $xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2'); - $catalogue = new MessageCatalogue($locale); + $catalogue = $this->catalogueFactory->create($locale); foreach ($xml->xpath('//xliff:trans-unit') as $translation) { $attributes = $translation->attributes(); diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index fda8a33e4053c..5b1fc58290560 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -14,6 +14,8 @@ use Symfony\Component\Translation\Loader\LoaderInterface; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Config\ConfigCache; +use Symfony\Component\Translation\Catalogue\CatalogueFactoryInterface; +use Symfony\Component\Translation\Catalogue\CatalogueFactory; /** * Translator. @@ -65,23 +67,28 @@ class Translator implements TranslatorInterface, TranslatorBagInterface private $debug; /** - * Constructor. - * - * @param string $locale The locale - * @param MessageSelector|null $selector The message selector for pluralization - * @param string|null $cacheDir The directory to use for the cache - * @param bool $debug Use cache in debug mode ? + * @var CatalogueFactoryInterface + */ + private $catalogueFactory; + + /** + * @param string $locale The locale + * @param MessageSelector|null $selector The message selector for pluralization + * @param string|null $cacheDir The directory to use for the cache + * @param bool $debug Use cache in debug mode ? + * @param CatalogueFactoryInterface $catalogueFactory The catalogue factory * * @throws \InvalidArgumentException If a locale contains invalid characters * * @api */ - public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false) + public function __construct($locale, MessageSelector $selector = null, $cacheDir = null, $debug = false, CatalogueFactoryInterface $catalogueFactory = null) { $this->setLocale($locale); $this->selector = $selector ?: new MessageSelector(); $this->cacheDir = $cacheDir; $this->debug = $debug; + $this->catalogueFactory = $catalogueFactory ?: new CatalogueFactory(); } /** @@ -346,7 +353,7 @@ protected function initializeCatalogue($locale) /** * @param string $locale - * @param bool $forceRefresh + * @param bool $forceRefresh */ private function initializeCacheCatalogue($locale, $forceRefresh = false) { @@ -408,7 +415,7 @@ private function initializeCacheCatalogue($locale, $forceRefresh = false) $catalogue = include $cache; - /** + /* * Old cache returns only the catalogue, without resourcesHash */ $resourcesHash = null; @@ -434,7 +441,7 @@ private function getResourcesHash($locale) private function doLoadCatalogue($locale) { - $this->catalogues[$locale] = new MessageCatalogue($locale); + $this->catalogues[$locale] = $this->catalogueFactory->create($locale); if (isset($this->resources[$locale])) { foreach ($this->resources[$locale] as $resource) {