4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Loader
;
14 use Symfony\Component\Translation\MessageCatalogue
;
15 use Symfony\Component\Translation\Exception\InvalidResourceException
;
16 use Symfony\Component\Translation\Exception\NotFoundResourceException
;
17 use Symfony\Component\Config\
Resource\DirectoryResource
;
20 * IcuResFileLoader loads translations from a resource bundle.
24 class IcuResFileLoader
implements LoaderInterface
29 public function load($resource, $locale, $domain = 'messages')
31 if (!stream_is_local($resource)) {
32 throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
35 if (!is_dir($resource)) {
36 throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
39 $rb = new \
ResourceBundle($locale, $resource);
42 throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
43 } elseif (intl_is_failure($rb->getErrorCode())) {
44 throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
47 $messages = $this->flatten($rb);
48 $catalogue = new MessageCatalogue($locale);
49 $catalogue->add($messages, $domain);
50 $catalogue->addResource(new DirectoryResource($resource));
56 * Flattens an ResourceBundle
59 * key { key2 { key3 { "value" } } }
61 * 'key.key2.key3' => 'value'
63 * This function takes an array by reference and will modify it
65 * @param \ResourceBundle $rb the ResourceBundle that will be flattened
66 * @param array $messages used internally for recursive calls
67 * @param string $path current path being parsed, used internally for recursive calls
69 * @return array the flattened ResourceBundle
71 protected function flatten(\ResourceBundle
$rb, array &$messages = array(), $path = null)
73 foreach ($rb as $key => $value) {
74 $nodePath = $path ? $path.'.'.$key : $key;
75 if ($value instanceof \ResourceBundle
) {
76 $this->flatten($value, $messages, $nodePath);
78 $messages[$nodePath] = $value;