2f8037fb164d8a70ad2210f4e21130f92d1039fe
[yaffs-website] / vendor / symfony / translation / Loader / IcuResFileLoader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Loader;
13
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;
18
19 /**
20  * IcuResFileLoader loads translations from a resource bundle.
21  *
22  * @author stealth35
23  */
24 class IcuResFileLoader implements LoaderInterface
25 {
26     /**
27      * {@inheritdoc}
28      */
29     public function load($resource, $locale, $domain = 'messages')
30     {
31         if (!stream_is_local($resource)) {
32             throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
33         }
34
35         if (!is_dir($resource)) {
36             throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
37         }
38
39         try {
40             $rb = new \ResourceBundle($locale, $resource);
41         } catch (\Exception $e) {
42             // HHVM compatibility: constructor throws on invalid resource
43             $rb = null;
44         }
45
46         if (!$rb) {
47             throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
48         } elseif (intl_is_failure($rb->getErrorCode())) {
49             throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
50         }
51
52         $messages = $this->flatten($rb);
53         $catalogue = new MessageCatalogue($locale);
54         $catalogue->add($messages, $domain);
55
56         if (class_exists('Symfony\Component\Config\Resource\DirectoryResource')) {
57             $catalogue->addResource(new DirectoryResource($resource));
58         }
59
60         return $catalogue;
61     }
62
63     /**
64      * Flattens an ResourceBundle.
65      *
66      * The scheme used is:
67      *   key { key2 { key3 { "value" } } }
68      * Becomes:
69      *   'key.key2.key3' => 'value'
70      *
71      * This function takes an array by reference and will modify it
72      *
73      * @param \ResourceBundle $rb       the ResourceBundle that will be flattened
74      * @param array           $messages used internally for recursive calls
75      * @param string          $path     current path being parsed, used internally for recursive calls
76      *
77      * @return array the flattened ResourceBundle
78      */
79     protected function flatten(\ResourceBundle $rb, array &$messages = array(), $path = null)
80     {
81         foreach ($rb as $key => $value) {
82             $nodePath = $path ? $path.'.'.$key : $key;
83             if ($value instanceof \ResourceBundle) {
84                 $this->flatten($value, $messages, $nodePath);
85             } else {
86                 $messages[$nodePath] = $value;
87             }
88         }
89
90         return $messages;
91     }
92 }