annotate vendor/symfony/translation/Loader/ArrayLoader.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Translation\Loader;
Chris@0 13
Chris@0 14 use Symfony\Component\Translation\MessageCatalogue;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * ArrayLoader loads translations from a PHP array.
Chris@0 18 *
Chris@0 19 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 20 */
Chris@0 21 class ArrayLoader implements LoaderInterface
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * {@inheritdoc}
Chris@0 25 */
Chris@0 26 public function load($resource, $locale, $domain = 'messages')
Chris@0 27 {
Chris@0 28 $this->flatten($resource);
Chris@0 29 $catalogue = new MessageCatalogue($locale);
Chris@0 30 $catalogue->add($resource, $domain);
Chris@0 31
Chris@0 32 return $catalogue;
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Flattens an nested array of translations.
Chris@0 37 *
Chris@0 38 * The scheme used is:
Chris@17 39 * 'key' => ['key2' => ['key3' => 'value']]
Chris@0 40 * Becomes:
Chris@0 41 * 'key.key2.key3' => 'value'
Chris@0 42 *
Chris@0 43 * This function takes an array by reference and will modify it
Chris@0 44 *
Chris@0 45 * @param array &$messages The array that will be flattened
Chris@0 46 * @param array $subnode Current subnode being parsed, used internally for recursive calls
Chris@0 47 * @param string $path Current path being parsed, used internally for recursive calls
Chris@0 48 */
Chris@0 49 private function flatten(array &$messages, array $subnode = null, $path = null)
Chris@0 50 {
Chris@0 51 if (null === $subnode) {
Chris@0 52 $subnode = &$messages;
Chris@0 53 }
Chris@0 54 foreach ($subnode as $key => $value) {
Chris@17 55 if (\is_array($value)) {
Chris@0 56 $nodePath = $path ? $path.'.'.$key : $key;
Chris@0 57 $this->flatten($messages, $value, $nodePath);
Chris@0 58 if (null === $path) {
Chris@0 59 unset($messages[$key]);
Chris@0 60 }
Chris@0 61 } elseif (null !== $path) {
Chris@0 62 $messages[$path.'.'.$key] = $value;
Chris@0 63 }
Chris@0 64 }
Chris@0 65 }
Chris@0 66 }