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>
21 class ArrayLoader implements LoaderInterface
26 public function load($resource, $locale, $domain = 'messages')
28 $this->flatten($resource);
29 $catalogue = new MessageCatalogue($locale);
30 $catalogue->add($resource, $domain);
36 * Flattens an nested array of translations.
39 * 'key' => array('key2' => array('key3' => 'value'))
41 * 'key.key2.key3' => 'value'
43 * This function takes an array by reference and will modify it
45 * @param array &$messages The array that will be flattened
46 * @param array $subnode Current subnode being parsed, used internally for recursive calls
47 * @param string $path Current path being parsed, used internally for recursive calls
49 private function flatten(array &$messages, array $subnode = null, $path = null)
51 if (null === $subnode) {
52 $subnode = &$messages;
54 foreach ($subnode as $key => $value) {
55 if (is_array($value)) {
56 $nodePath = $path ? $path.'.'.$key : $key;
57 $this->flatten($messages, $value, $nodePath);
59 unset($messages[$key]);
61 } elseif (null !== $path) {
62 $messages[$path.'.'.$key] = $value;