8000 [WIP][Translation][catalogue factory] allow custom Message Catalogue. by aitboudad · Pull Request #13982 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP][Translation][catalogue factory] allow custom Message Catalogue. #13982

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
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
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<argument key="debug">%kernel.debug%</argument>
</argument>
<argument type="collection" /> <!-- translation resources -->
<argument type="service" id="translation.catalogue.factory" />
</service>

<service id="translator.logging" class="Symfony\Component\Translation\LoggingTranslator" public="false">
Expand All @@ -60,46 +61,57 @@
<service id="translator.selector" class="%translator.selector.class%" public="false" />

<service id="translation.loader.php" class="%translation.loader.php.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="php" />
</service>

<service id="translation.loader.yml" class="%translation.loader.yml.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="yml" />
</service>

<service id="translation.loader.xliff" class="%translation.loader.xliff.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="xlf" legacy-alias="xliff" />
</service>

<service id="translation.loader.po" class="%translation.loader.po.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="po" />
</service>

<service id="translation.loader.mo" class="%translation.loader.mo.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="mo" />
</service>

<service id="translation.loader.qt" class="%translation.loader.qt.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="ts" />
</service>

<service id="translation.loader.csv" class="%translation.loader.csv.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="csv" />
</service>

<service id="translation.loader.res" class="%translation.loader.res.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="res" />
</service>

<service id="translation.loader.dat" class="%translation.loader.dat.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="dat" />
</service>

<service id="translation.loader.ini" class="%translation.loader.ini.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="ini" />
</service>

<service id="translation.loader.json" class="%translation.loader.json.class%">
<argument type="service" id="translation.catalogue.factory" />
<tag name="translation.loader" alias="json" />
</service>

Expand Down Expand Up @@ -151,6 +163,8 @@

<service id="translation.extractor" class="%translation.extractor.class%"/>

<service id="translation.catalogue.factory" class="Symfony\Component\Translation\Catalogue\CatalogueFactory"/>

<service id="translation.writer" class="%translation.writer.class%"/>
</services>
</container>
5 changes: 3 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,21 @@ 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.');
}

$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();
}
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/Translation/Catalogue/CatalogueFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Catalogue;

use Symfony\Component\Translation\MessageCatalogue;

/**
* @author Abdellatif Ait Boudad <a.aitboudad@gmail.com>
*/
class CatalogueFactory implements CatalogueFactoryInterface
{
/**
* {@inheritdoc}
*/
public function create($locale, array $messages = array())
{
return new MessageCatalogue($locale, $messages);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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\Catalogue;

/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
interface CatalogueFactoryInterface
{
/**
* @param string $locale The locale
* @param array $messages An array of messages classified by domain
*/
public function create($locale, array $messages = array());
}
34 changes: 34 additions & 0 deletions src/Symfony/Component/Translation/Loader/AbstractLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Loader;

use Symfony\Component\Translation\Catalogue\CatalogueFactoryInterface;
use Symfony\Component\Translation\Catalogue\CatalogueFactory;

/**
* @author Abdellatif Ait Boudad <a.aitboudad@gmail.com>
*/
abstract class AbstractLoader implements LoaderInterface
{
/**
* @var CatalogueFactoryInterface
*/
protected $catalogueFactory;

/**
* @param CatalogueFactoryInterface $catalogueFactory
*/
public function __construct(CatalogueFactoryInterface $catalogueFactory = null)
{
$this->catalogueFactory = $catalogueFactory ?: new CatalogueFactory();
}
}
6 changes: 2 additions & 4 deletions src/Symfony/Component/Translation/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@

namespace Symfony\Component\Translation\Loader;

use Symfony\Component\Translation\MessageCatalogue;

/**
* ArrayLoader loads translations from a PHP array.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class ArrayLoader implements LoaderInterface
class ArrayLoader extends AbstractLoader implements LoaderInterface
{
/**
* {@inheritdoc}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));

Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Translation/Loader/IcuResFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +20,7 @@
*
* @author stealth35
*/
class IcuResFileLoader implements LoaderInterface
class IcuResFileLoader extends AbstractLoader implements LoaderInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -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));

Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Translation/Loader/QtFileLoader.php
E091
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +23,7 @@
*
* @api
*/
class QtFileLoader implements LoaderInterface
class QtFileLoader extends AbstractLoader implements LoaderInterface
{
/**
* {@inheritdoc}
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +23,7 @@
*
* @api
*/
class XliffFileLoader implements LoaderInterface
class XliffFileLoader extends AbstractLoader implements LoaderInterface
{
/**
* {@inheritdoc}
Expand All @@ -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();

Expand Down
Loading
0