Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Translation] now support ResourceBundle
- Loading branch information
Showing
9 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Symfony/Component/Translation/Loader/ResourceBundleLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
tests/Symfony/Tests/Component/Translation/Loader/LocalizedTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/Symfony/Tests/Component/Translation/Loader/ResourceBundleLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/Symfony/Tests/Component/Translation/fixtures/resourcebundle/dat/en.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
en{ | ||
symfony{"Symfony 2 is great"} | ||
} |
3 changes: 3 additions & 0 deletions
3
tests/Symfony/Tests/Component/Translation/fixtures/resourcebundle/dat/fr.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fr{ | ||
symfony{"Symfony 2 est génial"} | ||
} |
Binary file added
BIN
+352 Bytes
tests/Symfony/Tests/Component/Translation/fixtures/resourcebundle/dat/resources.dat
Binary file not shown.
Binary file added
BIN
+84 Bytes
tests/Symfony/Tests/Component/Translation/fixtures/resourcebundle/res/en.res
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
tests/Symfony/Tests/Component/Translation/fixtures/resourcebundle/res/en.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
en { | ||
foo { "bar" } | ||
} |