Skip to content

Commit

Permalink
[Translation] now support ResourceBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
stealth35 committed Sep 12, 2011
1 parent b99bb1e commit e6e5146
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 0 deletions.
Expand Up @@ -13,6 +13,7 @@
<parameter key="translation.loader.xliff.class">Symfony\Component\Translation\Loader\XliffFileLoader</parameter>
<parameter key="translation.loader.qt.class">Symfony\Component\Translation\Loader\QtTranslationsLoader</parameter>
<parameter key="translation.loader.csv.class">Symfony\Component\Translation\Loader\CsvFileLoader</parameter>
<parameter key="translation.loader.rb.class">Symfony\Component\Translation\Loader\ResourceBundleLoader</parameter>
<parameter key="translation.dumper.php.class">Symfony\Component\Translation\Dumper\PhpFileDumper</parameter>
<parameter key="translation.dumper.xliff.class">Symfony\Component\Translation\Dumper\XliffFileDumper</parameter>
<parameter key="translation.dumper.yml.class">Symfony\Component\Translation\Dumper\YamlFileDumper</parameter>
Expand Down
80 changes: 80 additions & 0 deletions src/Symfony/Component/Translation/Loader/ResourceBundleLoader.php
@@ -0,0 +1,80 @@
<?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\MessageCatalogue;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileResource;

/**
* ResourceBundleLoader loads translations from a resource bundle.
*
* @author stealth35
*/
class ResourceBundleLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function load($resource, $locale, $domain = 'messages')
{
$rb = new \ResourceBundle($locale, $resource);

if (!$rb) {
throw new \RuntimeException("cannot load this resource : $rb");
} elseif (intl_is_failure($rb->getErrorCode())) {
throw new \RuntimeException($rb->getErrorMessage(), $rb->getErrorCode());
}

$messages = $this->flatten($rb);
$catalogue = new MessageCatalogue($locale);
$catalogue->add($messages, $domain);

if (is_dir($resource)) {
$catalogue->addResource(new DirectoryResource($resource));
} elseif (is_file($resource.'.dat')) {
$catalogue->addResource(new FileResource($resource.'.dat'));
}

return $catalogue;
}

/**
* Flattens an ResourceBundle
*
* The scheme used is:
* key { key2 { key3 { "value" } } }
* Becomes:
* 'key.key2.key3' => 'value'
*
* This function takes an array by reference and will modify it
*
* @param array \ResourceBundle $rb the ResourceBundle that will be flattened
* @param array $messages used internally for recursive calls
* @param string $path current path being parsed, used internally for recursive calls
*
* @return array the flattened ResourceBundle
*/
private function flatten(\ResourceBundle $rb, array &$messages = array(), $path = null)
{
foreach ($rb as $key => $value) {
$nodePath = $path ? $path.'.'.$key : $key;
if ($value instanceof \ResourceBundle) {
$this->flatten($value, $messages, $nodePath);
} else {
$messages[$nodePath] = $value;
}
}

return $messages;
}
}
@@ -0,0 +1,22 @@
<?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\Tests\Component\Translation\Loader;

abstract class LocalizedTestCase extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!extension_loaded('intl')) {
$this->markTestSkipped('The "intl" extension is not available');
}
}
}
@@ -0,0 +1,61 @@
<?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\Tests\Component\Translation\Loader;

use Symfony\Component\Translation\Loader\ResourceBundleLoader;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileResource;

class ResourceBundleFileLoaderTest extends LocalizedTestCase
{
public function testLoad()
{
$loader = new ResourceBundleLoader();
$resource = __DIR__.'/../fixtures/resourcebundle/res';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals(array('foo' => 'bar'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new DirectoryResource($resource)), $catalogue->getResources());
}

public function testDatEnglishLoad()
{
$loader = new ResourceBundleLoader();
$resource = __DIR__.'/../fixtures/resourcebundle/dat/resources';
$catalogue = $loader->load($resource, 'en', 'domain1');

$this->assertEquals(array('symfony' => 'Symfony 2 is great'), $catalogue->all('domain1'));
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource.'.dat')), $catalogue->getResources());
}

public function testDatFrenchLoad()
{
$loader = new ResourceBundleLoader();
$resource = __DIR__.'/../fixtures/resourcebundle/dat/resources';
$catalogue = $loader->load($resource, 'fr', 'domain1');

$this->assertEquals(array('symfony' => 'Symfony 2 est génial'), $catalogue->all('domain1'));
$this->assertEquals('fr', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource.'.dat')), $catalogue->getResources());
}

/**
* @expectedException Exception
*/
public function testLoadInvalidResource()
{
$loader = new ResourceBundleLoader();
$catalogue = $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1');
}
}
@@ -0,0 +1,3 @@
en{
symfony{"Symfony 2 is great"}
}
@@ -0,0 +1,3 @@
fr{
symfony{"Symfony 2 est génial"}
}
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
en {
foo { "bar" }
}

0 comments on commit e6e5146

Please sign in to comment.