From 8e569dd976e137edef5e06a01c495a36e2535d85 Mon Sep 17 00:00:00 2001 From: stealth35 Date: Wed, 22 Feb 2012 16:14:47 +0100 Subject: [PATCH] [Translation] ResourceBundleLoader to IcuRes/DatFileLoader --- .../Resources/config/translation.xml | 16 +++++-- .../Translation/Loader/IcuDatFileLoader.php | 44 +++++++++++++++++++ ...eBundleLoader.php => IcuResFileLoader.php} | 20 +++------ ...oaderTest.php => IcuDatFileLoaderTest.php} | 22 +++------- .../Loader/IcuResFileLoaderTest.php | 40 +++++++++++++++++ 5 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php rename src/Symfony/Component/Translation/Loader/{ResourceBundleLoader.php => IcuResFileLoader.php} (68%) rename tests/Symfony/Tests/Component/Translation/Loader/{ResourceBundleLoaderTest.php => IcuDatFileLoaderTest.php} (70%) create mode 100644 tests/Symfony/Tests/Component/Translation/Loader/IcuResFileLoaderTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml index 5108a31dea..6be410883a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml @@ -15,7 +15,8 @@ Symfony\Component\Translation\Loader\MoFileLoader Symfony\Component\Translation\Loader\QtTranslationsLoader Symfony\Component\Translation\Loader\CsvFileLoader - Symfony\Component\Translation\Loader\ResourceBundleLoader + Symfony\Component\Translation\Loader\IcuResFileLoader + Symfony\Component\Translation\Loader\IcuDatFileLoader Symfony\Component\Translation\Loader\IniFileLoader Symfony\Component\Translation\Dumper\PhpFileDumper Symfony\Component\Translation\Dumper\XliffFileDumper @@ -25,6 +26,7 @@ Symfony\Component\Translation\Dumper\QtFileDumper Symfony\Component\Translation\Dumper\CsvFileDumper Symfony\Component\Translation\Dumper\IniFileDumper + Symfony\Component\Translation\Dumper\IcuResFileDumper Symfony\Bundle\FrameworkBundle\Translation\PhpExtractor Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader Symfony\Component\Translation\Extractor\ChainExtractor @@ -76,8 +78,12 @@ - - + + + + + + @@ -116,6 +122,10 @@ + + + + diff --git a/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php b/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php new file mode 100644 index 0000000000..83c8abaebe --- /dev/null +++ b/src/Symfony/Component/Translation/Loader/IcuDatFileLoader.php @@ -0,0 +1,44 @@ + + * + * 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; + } +} diff --git a/src/Symfony/Component/Translation/Loader/ResourceBundleLoader.php b/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php similarity index 68% rename from src/Symfony/Component/Translation/Loader/ResourceBundleLoader.php rename to src/Symfony/Component/Translation/Loader/IcuResFileLoader.php index 726b82b59d..9387596ae6 100644 --- a/src/Symfony/Component/Translation/Loader/ResourceBundleLoader.php +++ b/src/Symfony/Component/Translation/Loader/IcuResFileLoader.php @@ -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; diff --git a/tests/Symfony/Tests/Component/Translation/Loader/ResourceBundleLoaderTest.php b/tests/Symfony/Tests/Component/Translation/Loader/IcuDatFileLoaderTest.php similarity index 70% rename from tests/Symfony/Tests/Component/Translation/Loader/ResourceBundleLoaderTest.php rename to tests/Symfony/Tests/Component/Translation/Loader/IcuDatFileLoaderTest.php index 78f371c0dc..321cba06c7 100644 --- a/tests/Symfony/Tests/Component/Translation/Loader/ResourceBundleLoaderTest.php +++ b/tests/Symfony/Tests/Component/Translation/Loader/IcuDatFileLoaderTest.php @@ -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'); } } diff --git a/tests/Symfony/Tests/Component/Translation/Loader/IcuResFileLoaderTest.php b/tests/Symfony/Tests/Component/Translation/Loader/IcuResFileLoaderTest.php new file mode 100644 index 0000000000..90f615230c --- /dev/null +++ b/tests/Symfony/Tests/Component/Translation/Loader/IcuResFileLoaderTest.php @@ -0,0 +1,40 @@ + + * + * 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'); + } +}