Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Translation\Loader; Chris@0: Chris@0: use Symfony\Component\Translation\MessageCatalogue; Chris@0: Chris@0: /** Chris@0: * ArrayLoader loads translations from a PHP array. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class ArrayLoader implements LoaderInterface Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function load($resource, $locale, $domain = 'messages') Chris@0: { Chris@0: $this->flatten($resource); Chris@0: $catalogue = new MessageCatalogue($locale); Chris@0: $catalogue->add($resource, $domain); Chris@0: Chris@0: return $catalogue; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Flattens an nested array of translations. Chris@0: * Chris@0: * The scheme used is: Chris@17: * 'key' => ['key2' => ['key3' => 'value']] Chris@0: * Becomes: Chris@0: * 'key.key2.key3' => 'value' Chris@0: * Chris@0: * This function takes an array by reference and will modify it Chris@0: * Chris@0: * @param array &$messages The array that will be flattened Chris@0: * @param array $subnode Current subnode being parsed, used internally for recursive calls Chris@0: * @param string $path Current path being parsed, used internally for recursive calls Chris@0: */ Chris@0: private function flatten(array &$messages, array $subnode = null, $path = null) Chris@0: { Chris@0: if (null === $subnode) { Chris@0: $subnode = &$messages; Chris@0: } Chris@0: foreach ($subnode as $key => $value) { Chris@17: if (\is_array($value)) { Chris@0: $nodePath = $path ? $path.'.'.$key : $key; Chris@0: $this->flatten($messages, $value, $nodePath); Chris@0: if (null === $path) { Chris@0: unset($messages[$key]); Chris@0: } Chris@0: } elseif (null !== $path) { Chris@0: $messages[$path.'.'.$key] = $value; Chris@0: } Chris@0: } Chris@0: } Chris@0: }