Skip to content

Commit

Permalink
Merge branch '2.2'
Browse files Browse the repository at this point in the history
* 2.2: (70 commits)
  change wrapped exception message to be more usefull
  updated VERSION for 2.0.23
  update CONTRIBUTORS for 2.0.23
  updated CHANGELOG for 2.0.23
  [Form] fixed failing test
  [DomCrawler] added support for query string with slash
  Fixed invalid file path for hiddeninput.exe on Windows.
  fix xsd definition for strict-requirements
  [WebProfilerBundle] Fixed the toolbar styles to apply them in IE8
  [ClassLoader] fixed heredocs handling
  fixed handling of heredocs
  Add a public modifier to an interface method
  removing xdebug extension
  [HttpRequest] fixes Request::getLanguages() bug
  [HttpCache] added a test (cached content should be kept after purging)
  [DoctrineBridge] Fixed non-utf-8 recognition
  [Security] fixed HttpUtils class tests
  replaced new occurences of 'Request::create()' with '::create()'
  changed sub-requests creation to '::create()'
  fixed merge issue
  ...

Conflicts:
	src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php
	src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar.html.twig
	src/Symfony/Component/DomCrawler/Link.php
	src/Symfony/Component/Translation/Translator.php
  • Loading branch information
fabpot committed Mar 20, 2013
2 parents 6388040 + cdbeaa8 commit c139710
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 53 deletions.
20 changes: 13 additions & 7 deletions Dumper/XliffFileDumper.php
Expand Up @@ -27,24 +27,30 @@ protected function format(MessageCatalogue $messages, $domain)
{ {
$dom = new \DOMDocument('1.0', 'utf-8'); $dom = new \DOMDocument('1.0', 'utf-8');
$dom->formatOutput = true; $dom->formatOutput = true;

$xliff = $dom->appendChild($dom->createElement('xliff')); $xliff = $dom->appendChild($dom->createElement('xliff'));
$xliff->setAttribute('version', '1.2'); $xliff->setAttribute('version', '1.2');
$xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2'); $xliff->setAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');

$xliffFile = $xliff->appendChild($dom->createElement('file')); $xliffFile = $xliff->appendChild($dom->createElement('file'));
$xliffFile->setAttribute('source-language', $messages->getLocale()); $xliffFile->setAttribute('source-language', $messages->getLocale());
$xliffFile->setAttribute('datatype', 'plaintext'); $xliffFile->setAttribute('datatype', 'plaintext');
$xliffFile->setAttribute('original', 'file.ext'); $xliffFile->setAttribute('original', 'file.ext');

$xliffBody = $xliffFile->appendChild($dom->createElement('body')); $xliffBody = $xliffFile->appendChild($dom->createElement('body'));
$id = 1;
foreach ($messages->all($domain) as $source => $target) { foreach ($messages->all($domain) as $source => $target) {
$trans = $dom->createElement('trans-unit'); $translation = $dom->createElement('trans-unit');
$trans->setAttribute('id', $id);
$s = $trans->appendChild($dom->createElement('source')); $translation->setAttribute('id', md5($source));
$translation->setAttribute('resname', $source);

$s = $translation->appendChild($dom->createElement('source'));
$s->appendChild($dom->createTextNode($source)); $s->appendChild($dom->createTextNode($source));
$t = $trans->appendChild($dom->createElement('target'));
$t = $translation->appendChild($dom->createElement('target'));
$t->appendChild($dom->createTextNode($target)); $t->appendChild($dom->createTextNode($target));
$xliffBody->appendChild($trans);
$id++; $xliffBody->appendChild($translation);
} }


return $dom->saveXML(); return $dom->saveXML();
Expand Down
4 changes: 2 additions & 2 deletions IdentityTranslator.php
Expand Up @@ -59,7 +59,7 @@ public function getLocale()
*/ */
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{ {
return strtr($id, $parameters); return strtr((string) $id, $parameters);
} }


/** /**
Expand All @@ -69,6 +69,6 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
*/ */
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null) public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
{ {
return strtr($this->selector->choose($id, (int) $number, $locale), $parameters); return strtr($this->selector->choose((string) $id, (int) $number, $locale), $parameters);
} }
} }
10 changes: 8 additions & 2 deletions Loader/XliffFileLoader.php
Expand Up @@ -45,10 +45,14 @@ public function load($resource, $locale, $domain = 'messages')


$catalogue = new MessageCatalogue($locale); $catalogue = new MessageCatalogue($locale);
foreach ($xml->xpath('//xliff:trans-unit') as $translation) { foreach ($xml->xpath('//xliff:trans-unit') as $translation) {
if (!isset($translation->source) || !isset($translation->target)) { $attributes = $translation->attributes();

if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) {
continue; continue;
} }
$catalogue->set((string) $translation->source, (string) $translation->target, $domain);
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source;
$catalogue->set((string) $source, (string) $translation->target, $domain);
} }
$catalogue->addResource(new FileResource($resource)); $catalogue->addResource(new FileResource($resource));


Expand All @@ -60,6 +64,8 @@ public function load($resource, $locale, $domain = 'messages')
* *
* @param string $file * @param string $file
* *
* @throws \RuntimeException
*
* @return \SimpleXMLElement * @return \SimpleXMLElement
* *
* @throws InvalidResourceException * @throws InvalidResourceException
Expand Down
41 changes: 20 additions & 21 deletions MessageCatalogue.php
Expand Up @@ -24,8 +24,8 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
{ {
private $messages = array(); private $messages = array();
private $metadata = array(); private $metadata = array();
private $resources = array();
private $locale; private $locale;
private $resources;
private $fallbackCatalogue; private $fallbackCatalogue;
private $parent; private $parent;


Expand All @@ -41,7 +41,6 @@ public function __construct($locale, array $messages = array())
{ {
$this->locale = $locale; $this->locale = $locale;
$this->messages = $messages; $this->messages = $messages;
$this->resources = array();
} }


/** /**
Expand Down Expand Up @@ -177,8 +176,10 @@ public function addCatalogue(MessageCatalogueInterface $catalogue)
$this->addResource($resource); $this->addResource($resource);
} }


$metadata = $catalogue->getMetadata('', ''); if ($catalogue instanceof MetadataAwareInterface) {
$this->addMetadata($metadata); $metadata = $catalogue->getMetadata('', '');
$this->addMetadata($metadata);
}
} }


/** /**
Expand All @@ -205,9 +206,7 @@ public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
} }


/** /**
* Gets the fallback catalogue. * {@inheritdoc}
*
* @return MessageCatalogueInterface A MessageCatalogueInterface instance
* *
* @api * @api
*/ */
Expand All @@ -223,7 +222,7 @@ public function getFallbackCatalogue()
*/ */
public function getResources() public function getResources()
{ {
return array_values(array_unique($this->resources)); return array_values($this->resources);
} }


/** /**
Expand All @@ -233,27 +232,29 @@ public function getResources()
*/ */
public function addResource(ResourceInterface $resource) public function addResource(ResourceInterface $resource)
{ {
$this->resources[] = $resource; $this->resources[$resource->__toString()] = $resource;
} }


/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getMetadata($key = '', $domain = 'messages') public function getMetadata($key = '', $domain = 'messages')
{ {
if (empty($domain)) { if ('' == $domain) {
return $this->metadata; return $this->metadata;
} }


if (isset($this->metadata[$domain])) { if (isset($this->metadata[$domain])) {
if (!empty($key)) { if ('' == $key) {
if (isset($this->metadata[$domain][$key])) {
return $this->metadata[$domain][$key];
}
} else {
return $this->metadata[$domain]; return $this->metadata[$domain];
} }

if (isset($this->metadata[$domain][$key])) {
return $this->metadata[$domain][$key];
}
} }

return null;
} }


/** /**
Expand All @@ -269,15 +270,13 @@ public function setMetadata($key, $value, $domain = 'messages')
*/ */
public function deleteMetadata($key = '', $domain = 'messages') public function deleteMetadata($key = '', $domain = 'messages')
{ {
if (empty($domain)) { if ('' == $domain) {
$this->metadata = array(); $this->metadata = array();
} } elseif ('' == $key) {

if (empty($key)) {
unset($this->metadata[$domain]); unset($this->metadata[$domain]);
} else {
unset($this->metadata[$domain][$key]);
} }

unset($this->metadata[$domain][$key]);
} }


/** /**
Expand Down
6 changes: 3 additions & 3 deletions MessageCatalogueInterface.php
Expand Up @@ -103,7 +103,7 @@ public function get($id, $domain = 'messages');
/** /**
* Sets translations for a given domain. * Sets translations for a given domain.
* *
* @param string $messages An array of translations * @param array $messages An array of translations
* @param string $domain The domain name * @param string $domain The domain name
* *
* @api * @api
Expand All @@ -113,7 +113,7 @@ public function replace($messages, $domain = 'messages');
/** /**
* Adds translations for a given domain. * Adds translations for a given domain.
* *
* @param string $messages An array of translations * @param array $messages An array of translations
* @param string $domain The domain name * @param string $domain The domain name
* *
* @api * @api
Expand Down Expand Up @@ -146,7 +146,7 @@ public function addFallbackCatalogue(MessageCatalogueInterface $catalogue);
/** /**
* Gets the fallback catalogue. * Gets the fallback catalogue.
* *
* @return MessageCatalogueInterface A MessageCatalogueInterface instance * @return MessageCatalogueInterface|null A MessageCatalogueInterface instance or null when no fallback has been set
* *
* @api * @api
*/ */
Expand Down
25 changes: 17 additions & 8 deletions MetadataAwareInterface.php
Expand Up @@ -19,27 +19,36 @@
interface MetadataAwareInterface interface MetadataAwareInterface
{ {
/** /**
* Gets meta data for given domain and key. * Gets metadata for the given domain and key.
*
* Passing an empty domain will return an array with all metadata indexed by
* domain and then by key. Passing an empty key will return an array with all
* metadata for the given domain.
* *
* @param string $domain The domain name * @param string $domain The domain name
* @param string $key Key * @param string $key The key
*
* @return mixed The value that was set or an array with the domains/keys or null
*/ */
public function getMetadata($key = '', $domain = 'messages'); public function getMetadata($key = '', $domain = 'messages');


/** /**
* Adds meta data to a message domain. * Adds metadata to a message domain.
* *
* @param string $key Key * @param string $key The key
* @param string|array $value Value * @param mixed $value The value
* @param string $domain The domain name * @param string $domain The domain name
*/ */
public function setMetadata($key, $value, $domain = 'messages'); public function setMetadata($key, $value, $domain = 'messages');


/** /**
* Deletes meta data for given key and domain. * Deletes metadata for the given key and domain.
*
* Passing an empty domain will delete all metadata. Passing an empty key will
* delete all metadata for the given domain.
* *
* @param string $domain The domain name * @param string $domain The domain name
* @param string $key Key * @param string $key The key
*/ */
public function deleteMetadata($key = '', $domain = 'messages'); public function deleteMetadata($key = '', $domain = 'messages');
} }
8 changes: 8 additions & 0 deletions Tests/Loader/XliffFileLoaderTest.php
Expand Up @@ -33,6 +33,14 @@ public function testLoad()
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources()); $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
} }


public function testLoadWithResname()
{
$loader = new XliffFileLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');

$this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1'));
}

public function testIncompleteResource() public function testIncompleteResource()
{ {
$loader = new XliffFileLoader(); $loader = new XliffFileLoader();
Expand Down
19 changes: 19 additions & 0 deletions Tests/fixtures/resname.xlf
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1" resname="foo">
<source></source>
<target>bar</target>
</trans-unit>
<trans-unit id="2" resname="bar">
<source>bar source</source>
<target>baz</target>
</trans-unit>
<trans-unit id="3">
<source>baz</source>
<target>foo</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 2 additions & 2 deletions Tests/fixtures/resources-clean.xlf
Expand Up @@ -2,11 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2"> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" datatype="plaintext" original="file.ext"> <file source-language="en" datatype="plaintext" original="file.ext">
<body> <body>
<trans-unit id="1"> <trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
<source>foo</source> <source>foo</source>
<target>bar</target> <target>bar</target>
</trans-unit> </trans-unit>
<trans-unit id="2"> <trans-unit id="3c6e0b8a9c15224a8228b9a98ca1531d" resname="key">
<source>key</source> <source>key</source>
<target></target> <target></target>
</trans-unit> </trans-unit>
Expand Down
12 changes: 6 additions & 6 deletions Translator.php
Expand Up @@ -56,15 +56,15 @@ class Translator implements TranslatorInterface
/** /**
* Constructor. * Constructor.
* *
* @param string $locale The locale * @param string $locale The locale
* @param MessageSelector $selector The message selector for pluralization * @param MessageSelector|null $selector The message selector for pluralization
* *
* @api * @api
*/ */
public function __construct($locale, MessageSelector $selector = null) public function __construct($locale, MessageSelector $selector = null)
{ {
$this->locale = $locale; $this->locale = $locale;
$this->selector = null === $selector ? new MessageSelector() : $selector; $this->selector = $selector ?: new MessageSelector();
} }


/** /**
Expand Down Expand Up @@ -165,7 +165,7 @@ public function getFallbackLocales()
*/ */
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{ {
if (!isset($locale)) { if (null === $locale) {
$locale = $this->getLocale(); $locale = $this->getLocale();
} }


Expand All @@ -183,7 +183,7 @@ public function trans($id, array $parameters = array(), $domain = 'messages', $l
*/ */
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null) public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
{ {
if (!isset($locale)) { if (null === $locale) {
$locale = $this->getLocale(); $locale = $this->getLocale();
} }


Expand All @@ -203,7 +203,7 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
} }
} }


return strtr($this->selector->choose($catalogue->get($id, $domain), (float) $number, $locale), $parameters); return strtr($this->selector->choose($catalogue->get($id, $domain), (int) $number, $locale), $parameters);
} }


protected function loadCatalogue($locale) protected function loadCatalogue($locale)
Expand Down
4 changes: 2 additions & 2 deletions TranslatorInterface.php
Expand Up @@ -23,7 +23,7 @@ interface TranslatorInterface
/** /**
* Translates the given message. * Translates the given message.
* *
* @param string $id The message id * @param string $id The message id (may also be an object that can be cast to string)
* @param array $parameters An array of parameters for the message * @param array $parameters An array of parameters for the message
* @param string $domain The domain for the message * @param string $domain The domain for the message
* @param string $locale The locale * @param string $locale The locale
Expand All @@ -37,7 +37,7 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
/** /**
* Translates the given choice message by choosing a translation according to a number. * Translates the given choice message by choosing a translation according to a number.
* *
* @param string $id The message id * @param string $id The message id (may also be an object that can be cast to string)
* @param integer $number The number to use to find the indice of the message * @param integer $number The number to use to find the indice of the message
* @param array $parameters An array of parameters for the message * @param array $parameters An array of parameters for the message
* @param string $domain The domain for the message * @param string $domain The domain for the message
Expand Down

0 comments on commit c139710

Please sign in to comment.