merged branch fabpot/translation-metadata (PR #6349)
This PR was merged into the master branch. Commits -------320fb6c
[Translation] changed the MetadataAwareInterface method signatures11ff433
[Translation] fixed CS in unit testsc40db35
[Translation] removed some code that is not done anywhere else in Symfony63719a0
[Translation] created a new interface to avoid breaking BC23e9e65
[Translation] fixed name5607732
[Translation] added Metadata to MessageCatalogue Discussion ---------- [MessageCatalogue] Add Metadata to MessageCatalogue rework of #4399 For improving the Gettext tools (PO, MO File Loader/Dumper) we need at least storage for their meta data. This patch allows for issues below to store and process ie Po Header, Po Header Pluralisation rule. Open - [[WIP]: Allow Drupal to use Translate component](https://github.com/symfony/symfony/pull/4249) - [[2.1][Translator] Symfony translation process & gettext](https://github.com/symfony/symfony/issues/4245) Closed: - [[Translation] Po/MoFileLoader parse plurization rules](https://github.com/symfony/symfony/pull/3023) It has 1 TODO regarding addCatalogue: it now just override old values with new.
This commit is contained in:
commit
a709a02538
@ -20,9 +20,10 @@ use Symfony\Component\Config\Resource\ResourceInterface;
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class MessageCatalogue implements MessageCatalogueInterface
|
||||
class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterface
|
||||
{
|
||||
private $messages = array();
|
||||
private $metadata = array();
|
||||
private $locale;
|
||||
private $resources;
|
||||
private $fallbackCatalogue;
|
||||
@ -175,6 +176,9 @@ class MessageCatalogue implements MessageCatalogueInterface
|
||||
foreach ($catalogue->getResources() as $resource) {
|
||||
$this->addResource($resource);
|
||||
}
|
||||
|
||||
$metadata = $catalogue->getMetadata('', '');
|
||||
$this->addMetadata($metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -231,4 +235,62 @@ class MessageCatalogue implements MessageCatalogueInterface
|
||||
{
|
||||
$this->resources[] = $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadata($key = '', $domain = 'messages')
|
||||
{
|
||||
if (empty($domain)) {
|
||||
return $this->metadata;
|
||||
}
|
||||
|
||||
if (isset($this->metadata[$domain])) {
|
||||
if (!empty($key)) {
|
||||
if (isset($this->metadata[$domain][$key])) {
|
||||
return $this->metadata[$domain][$key];
|
||||
}
|
||||
} else {
|
||||
return $this->metadata[$domain];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setMetadata($key, $value, $domain = 'messages')
|
||||
{
|
||||
$this->metadata[$domain][$key] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function deleteMetadata($key = '', $domain = 'messages')
|
||||
{
|
||||
if (empty($domain)) {
|
||||
$this->metadata = array();
|
||||
}
|
||||
|
||||
if (empty($key)) {
|
||||
unset($this->metadata[$domain]);
|
||||
}
|
||||
|
||||
unset($this->metadata[$domain][$key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds current values with the new values.
|
||||
*
|
||||
* @param array $values Values to add
|
||||
*/
|
||||
private function addMetadata(array $values)
|
||||
{
|
||||
foreach ($values as $domain => $keys) {
|
||||
foreach ($keys as $key => $value) {
|
||||
$this->setMetadata($key, $value, $domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
47
src/Symfony/Component/Translation/MetadataAwareInterface.php
Normal file
47
src/Symfony/Component/Translation/MetadataAwareInterface.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\Config\Resource\ResourceInterface;
|
||||
|
||||
/**
|
||||
* MetadataAwareInterface.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
interface MetadataAwareInterface
|
||||
{
|
||||
/**
|
||||
* Gets meta data for given domain and key.
|
||||
*
|
||||
* @param string $domain The domain name
|
||||
* @param string $key Key
|
||||
*/
|
||||
public function getMetadata($key = '', $domain = 'messages');
|
||||
|
||||
/**
|
||||
* Adds meta data to a message domain.
|
||||
*
|
||||
* @param string $key Key
|
||||
* @param string|array $value Value
|
||||
* @param string $domain The domain name
|
||||
*/
|
||||
public function setMetadata($key, $value, $domain = 'messages');
|
||||
|
||||
/**
|
||||
* Deletes meta data for given key and domain.
|
||||
*
|
||||
* @param string $domain The domain name
|
||||
* @param string $key Key
|
||||
*/
|
||||
public function deleteMetadata($key = '', $domain = 'messages');
|
||||
}
|
@ -170,4 +170,43 @@ class MessageCatalogueTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals(array($r, $r1), $catalogue->getResources());
|
||||
}
|
||||
|
||||
public function testMetadataDelete()
|
||||
{
|
||||
$catalogue = new MessageCatalogue('en');
|
||||
$this->assertEquals(array(), $catalogue->getMetadata('', ''), 'Metadata is empty');
|
||||
$catalogue->deleteMetadata('key', 'messages');
|
||||
$catalogue->deleteMetadata('', 'messages');
|
||||
$catalogue->deleteMetadata();
|
||||
}
|
||||
|
||||
public function testMetadataSetGetDelete()
|
||||
{
|
||||
$catalogue = new MessageCatalogue('en');
|
||||
$catalogue->setMetadata('key', 'value');
|
||||
$this->assertEquals('value', $catalogue->getMetadata('key', 'messages'), "Metadata 'key' = 'value'");
|
||||
|
||||
$catalogue->setMetadata('key2', array());
|
||||
$this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
|
||||
|
||||
$catalogue->deleteMetadata('key2', 'messages');
|
||||
$this->assertEquals(null, $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
|
||||
|
||||
$catalogue->deleteMetadata('key2', 'domain');
|
||||
$this->assertEquals(null, $catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
|
||||
}
|
||||
|
||||
public function testMetadataMerge()
|
||||
{
|
||||
$cat1 = new MessageCatalogue('en');
|
||||
$cat1->setMetadata('a', 'b');
|
||||
$this->assertEquals(array('messages' => array('a' => 'b')), $cat1->getMetadata('', ''), 'Cat1 contains messages metadata.');
|
||||
|
||||
$cat2 = new MessageCatalogue('en');
|
||||
$cat2->setMetadata('b', 'c', 'domain');
|
||||
$this->assertEquals(array('domain' => array('b' => 'c')), $cat2->getMetadata('', ''), 'Cat2 contains domain metadata.');
|
||||
|
||||
$cat1->addCatalogue($cat2);
|
||||
$this->assertEquals(array('messages' => array('a' => 'b'), 'domain' => array('b' => 'c')), $cat1->getMetadata('', ''), 'Cat1 contains merged metadata.');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user