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
;
17 * ArrayLoader loads translations from a PHP array.
19 * @author Fabien Potencier <fabien@symfony.com>
23 class ArrayLoader
implements LoaderInterface
30 public function load($resource, $locale, $domain = 'messages')
32 $this->flatten($resource);
33 $catalogue = new MessageCatalogue($locale);
34 $catalogue->add($resource, $domain);
40 * Flattens an nested array of translations
43 * 'key' => array('key2' => array('key3' => 'value'))
45 * 'key.key2.key3' => 'value'
47 * This function takes an array by reference and will modify it
49 * @param array &$messages The array that will be flattened
50 * @param array $subnode Current subnode being parsed, used internally for recursive calls
51 * @param string $path Current path being parsed, used internally for recursive calls
53 private function flatten(array &$messages, array $subnode = null, $path = null)
55 if (null === $subnode) {
56 $subnode =& $messages;
58 foreach ($subnode as $key => $value) {
59 if (is_array($value)) {
60 $nodePath = $path ? $path.'.'.$key : $key;
61 $this->flatten($messages, $value, $nodePath);
63 unset($messages[$key]);
65 } elseif (null !== $path) {
66 $messages[$path.'.'.$key] = $value;