[Translation] ResourceBundleLoader to IcuRes/DatFileLoader
This commit is contained in:
parent
7474ad5075
commit
8e569dd976
|
@ -15,7 +15,8 @@
|
|||
<parameter key="translation.loader.mo.class">Symfony\Component\Translation\Loader\MoFileLoader</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.loader.res.class">Symfony\Component\Translation\Loader\IcuResFileLoader</parameter>
|
||||
<parameter key="translation.loader.dar.class">Symfony\Component\Translation\Loader\IcuDatFileLoader</parameter>
|
||||
<parameter key="translation.loader.ini.class">Symfony\Component\Translation\Loader\IniFileLoader</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>
|
||||
|
@ -25,6 +26,7 @@
|
|||
<parameter key="translation.dumper.qt.class">Symfony\Component\Translation\Dumper\QtFileDumper</parameter>
|
||||
<parameter key="translation.dumper.csv.class">Symfony\Component\Translation\Dumper\CsvFileDumper</parameter>
|
||||
<parameter key="translation.dumper.ini.class">Symfony\Component\Translation\Dumper\IniFileDumper</parameter>
|
||||
<parameter key="translation.dumper.res.class">Symfony\Component\Translation\Dumper\IcuResFileDumper</parameter>
|
||||
<parameter key="translation.extractor.php.class">Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor</parameter>
|
||||
<parameter key="translation.loader.class">Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader</parameter>
|
||||
<parameter key="translation.extractor.class">Symfony\Component\Translation\Extractor\ChainExtractor</parameter>
|
||||
|
@ -76,8 +78,12 @@
|
|||
<tag name="translation.loader" alias="csv" />
|
||||
</service>
|
||||
|
||||
<service id="translation.loader.rb" class="%translation.loader.rb.class%">
|
||||
<tag name="translation.loader" alias="rb" />
|
||||
<service id="translation.loader.res" class="%translation.loader.res.class%">
|
||||
<tag name="translation.loader" alias="res" />
|
||||
</service>
|
||||
|
||||
<service id="translation.loader.dat" class="%translation.loader.res.class%">
|
||||
<tag name="translation.loader" alias="dat" />
|
||||
</service>
|
||||
|
||||
<service id="translation.loader.ini" class="%translation.loader.ini.class%">
|
||||
|
@ -116,6 +122,10 @@
|
|||
<tag name="translation.dumper" alias="ini" />
|
||||
</service>
|
||||
|
||||
<service id="translation.dumper.res" class="%translation.dumper.res.class%">
|
||||
<tag name="translation.dumper" alias="res" />
|
||||
</service>
|
||||
|
||||
<service id="translation.extractor.php" class="%translation.extractor.php.class%">
|
||||
<tag name="translation.extractor" alias="php" />
|
||||
</service>
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
<?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\FileResource;
|
||||
|
||||
/**
|
||||
* IcuResFileLoader loads translations from a resource bundle.
|
||||
*
|
||||
* @author stealth35
|
||||
*/
|
||||
class IcuDatFileLoader extends IcuResFileLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($resource, $locale, $domain = 'messages')
|
||||
{
|
||||
$rb = new \ResourceBundle($locale, $resource);
|
||||
|
||||
if (!$rb) {
|
||||
throw new \RuntimeException("cannot load this resource : $resource");
|
||||
} 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);
|
||||
$catalogue->addResource(new FileResource($resource.'.dat'));
|
||||
|
||||
return $catalogue;
|
||||
}
|
||||
}
|
|
@ -13,14 +13,13 @@ 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.
|
||||
* IcuResFileLoader loads translations from a resource bundle.
|
||||
*
|
||||
* @author stealth35
|
||||
*/
|
||||
class ResourceBundleLoader implements LoaderInterface
|
||||
class IcuResFileLoader implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -38,12 +37,7 @@ class ResourceBundleLoader implements LoaderInterface
|
|||
$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'));
|
||||
}
|
||||
$catalogue->addResource(new DirectoryResource($resource));
|
||||
|
||||
return $catalogue;
|
||||
}
|
||||
|
@ -58,13 +52,13 @@ class ResourceBundleLoader implements LoaderInterface
|
|||
*
|
||||
* This function takes an array by reference and will modify it
|
||||
*
|
||||
* @param \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
|
||||
* @param \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)
|
||||
protected function flatten(\ResourceBundle $rb, array &$messages = array(), $path = null)
|
||||
{
|
||||
foreach ($rb as $key => $value) {
|
||||
$nodePath = $path ? $path.'.'.$key : $key;
|
|
@ -11,30 +11,18 @@
|
|||
|
||||
namespace Symfony\Tests\Component\Translation\Loader;
|
||||
|
||||
use Symfony\Component\Translation\Loader\ResourceBundleLoader;
|
||||
use Symfony\Component\Translation\Loader\IcuDatFileLoader;
|
||||
use Symfony\Component\Config\Resource\DirectoryResource;
|
||||
use Symfony\Component\Config\Resource\FileResource;
|
||||
|
||||
class ResourceBundleFileLoaderTest extends LocalizedTestCase
|
||||
class IcuDatFileLoaderTest extends LocalizedTestCase
|
||||
{
|
||||
public function testLoad()
|
||||
{
|
||||
// resource is build using genrb command
|
||||
$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()
|
||||
{
|
||||
// bundled resource is build using pkgdata command which at leas in ICU 4.2 comes in extremely! buggy form
|
||||
// you must specify an temporary build directory which is not the same as current directory and
|
||||
// MUST reside on the same partition. pkgdata -p resources -T /srv -d . packagelist.txt
|
||||
$loader = new ResourceBundleLoader();
|
||||
$loader = new IcuDatFileLoader();
|
||||
$resource = __DIR__.'/../fixtures/resourcebundle/dat/resources';
|
||||
$catalogue = $loader->load($resource, 'en', 'domain1');
|
||||
|
||||
|
@ -45,7 +33,7 @@ class ResourceBundleFileLoaderTest extends LocalizedTestCase
|
|||
|
||||
public function testDatFrenchLoad()
|
||||
{
|
||||
$loader = new ResourceBundleLoader();
|
||||
$loader = new IcuDatFileLoader();
|
||||
$resource = __DIR__.'/../fixtures/resourcebundle/dat/resources';
|
||||
$catalogue = $loader->load($resource, 'fr', 'domain1');
|
||||
|
||||
|
@ -59,7 +47,7 @@ class ResourceBundleFileLoaderTest extends LocalizedTestCase
|
|||
*/
|
||||
public function testLoadInvalidResource()
|
||||
{
|
||||
$loader = new ResourceBundleLoader();
|
||||
$loader = new IcuDatFileLoader();
|
||||
$catalogue = $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1');
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?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\IcuResFileLoader;
|
||||
use Symfony\Component\Config\Resource\DirectoryResource;
|
||||
use Symfony\Component\Config\Resource\FileResource;
|
||||
|
||||
class IcuResFileLoaderTest extends LocalizedTestCase
|
||||
{
|
||||
public function testLoad()
|
||||
{
|
||||
// resource is build using genrb command
|
||||
$loader = new IcuResFileLoader();
|
||||
$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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testLoadInvalidResource()
|
||||
{
|
||||
$loader = new IcuResFileLoader();
|
||||
$catalogue = $loader->load(__DIR__.'/../fixtures/resourcebundle/res/en.txt', 'en', 'domain1');
|
||||
}
|
||||
}
|
Reference in New Issue