Navigation Menu

Skip to content

Commit

Permalink
[Translation] Added flatten method on ArrayLoader
Browse files Browse the repository at this point in the history
This allows the translations to be deeply nested arrays that will be flattened, allowing for namespacing of translations easily.

The following:
  'key' => array('key2' => array('key3' => 'value'))
Becomes:
  'key.key2.key3' => 'value'

This isn't applied to Xliff since it does not make sense within the scope of the XLIFF standard
  • Loading branch information
Seldaek authored and fabpot committed Nov 16, 2010
1 parent 4e5c99d commit 3cbc99c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
33 changes: 33 additions & 0 deletions src/Symfony/Component/Translation/Loader/ArrayLoader.php
Expand Up @@ -25,9 +25,42 @@ class ArrayLoader implements LoaderInterface
*/
public function load($resource, $locale, $domain = 'messages')
{
$catalogue = $this->flatten($resource);
$catalogue = new MessageCatalogue($locale);
$catalogue->addMessages($resource, $domain);

return $catalogue;
}

/**
* Flattens an nested array of translations
*
* The scheme used is:
* 'key' => array('key2' => array('key3' => 'value'))
* Becomes:
* 'key.key2.key3' => 'value'
*
* This function takes an array by reference and will modify it
*
* @param array $messages the array that will be flattened
* @param array $subnode current subnode being parsed, used internally for recursive calls
* @param string $path current path being parsed, used internally for recursive calls
*/
protected function flatten(array &$messages, array $subnode = null, $path = null)
{
if ($subnode === null) {
$subnode =& $messages;
}
foreach ($subnode as $key => $value) {
if (is_array($value)) {
$nodePath = $path ? $path.'.'.$key : $key;
$this->flatten($messages, $value, $nodePath);
if ($path === null) {
unset($messages[$key]);
}
} elseif ($path !== null) {
$messages[$path.'.'.$key] = $value;
}
}
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Translation/Loader/PhpFileLoader.php
Expand Up @@ -2,7 +2,6 @@

namespace Symfony\Component\Translation\Loader;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Resource\FileResource;

/*
Expand All @@ -19,15 +18,16 @@
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class PhpFileLoader implements LoaderInterface
class PhpFileLoader extends ArrayLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
$catalogue = new MessageCatalogue($locale);
$catalogue->addMessages(require($resource), $domain);
$messages = require($resource);

$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));

return $catalogue;
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/Translation/Loader/YamlFileLoader.php
Expand Up @@ -2,7 +2,6 @@

namespace Symfony\Component\Translation\Loader;

use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;

Expand All @@ -20,7 +19,7 @@
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class YamlFileLoader implements LoaderInterface
class YamlFileLoader extends ArrayLoader implements LoaderInterface
{
/**
* {@inheritdoc}
Expand All @@ -29,8 +28,7 @@ public function load($resource, $locale, $domain = 'messages')
{
$messages = Yaml::load($resource);

$catalogue = new MessageCatalogue($locale);
$catalogue->addMessages($messages, $domain);
$catalogue = parent::load($messages, $locale, $domain);
$catalogue->addResource(new FileResource($resource));

return $catalogue;
Expand Down
35 changes: 35 additions & 0 deletions tests/Symfony/Tests/Component/Translation/TranslatorTest.php
Expand Up @@ -74,6 +74,18 @@ public function testTrans($expected, $id, $translation, $parameters, $locale, $d
$this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
}

/**
* @dataProvider getFlattenedTransTests
*/
public function testFlattenedTrans($expected, $messages, $id)
{
$translator = new Translator('en', new MessageSelector());
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', $messages, 'fr', '');

$this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
}

/**
* @dataProvider getTransChoiceTests
*/
Expand All @@ -94,6 +106,29 @@ public function getTransTests()
);
}

public function getFlattenedTransTests()
{
$messages = array(
'symfony2' => array(
'is' => array(
'great' => 'Symfony2 est super!'
)
),
'foo' => array(
'bar' => array(
'baz' => 'Foo Bar Baz'
),
'baz' => 'Foo Baz',
),
);

return array(
array('Symfony2 est super!', $messages, 'symfony2.is.great'),
array('Foo Bar Baz', $messages, 'foo.bar.baz'),
array('Foo Baz', $messages, 'foo.baz'),
);
}

public function getTransChoiceTests()
{
return array(
Expand Down

0 comments on commit 3cbc99c

Please sign in to comment.