merged branch stealth35/trans_rb (PR #2136)

Commits
-------

e6e5146 [Translation] now support ResourceBundle

Discussion
----------

[2.1][Translation] now support ResourceBundle

support `.res` and `.dat` bundles

---------------------------------------------------------------------------

by marijn at 2011/09/08 08:59:39 -0700

There are a few references to `ressource`, I guess that is a typo...

---------------------------------------------------------------------------

by stealth35 at 2011/09/08 09:13:32 -0700

@marijn thank, done

---------------------------------------------------------------------------

by fabpot at 2011/09/11 00:42:37 -0700

Is it possible to add a dumper like we have for all other loaders?

---------------------------------------------------------------------------

by stof at 2011/09/11 03:46:39 -0700

Btw, you need to rebase your branch as it conflicts with master

---------------------------------------------------------------------------

by stealth35 at 2011/09/11 04:04:23 -0700

@fabpot it's more difficult (or the easy way it's to use `exec` with `derb`), I can create the text resources
@stof oki

---------------------------------------------------------------------------

by fabpot at 2011/09/11 23:52:19 -0700

@stealth35: Can you remove the `@api` tags? We will review what is included into the public API later on. thanks.

---------------------------------------------------------------------------

by stealth35 at 2011/09/12 04:18:07 -0700

@fabpot done
This commit is contained in:
Fabien Potencier 2011-09-13 08:49:05 +02:00
commit affb0cbca2
9 changed files with 173 additions and 0 deletions

View File

@ -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>

View File

@ -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;
}
}

View File

@ -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');
}
}
}

View File

@ -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');
}
}

View File

@ -0,0 +1,3 @@
en{
symfony{"Symfony 2 is great"}
}

View File

@ -0,0 +1,3 @@
fr{
symfony{"Symfony 2 est génial"}
}

View File

@ -0,0 +1,3 @@
en {
foo { "bar" }
}