ArrayLoader.php
2 months ago
CsvFileLoader.php
2 months ago
FileLoader.php
2 months ago
IcuDatFileLoader.php
2 months ago
IcuResFileLoader.php
2 months ago
IniFileLoader.php
2 months ago
JsonFileLoader.php
2 months ago
LoaderInterface.php
2 months ago
MoFileLoader.php
2 months ago
PhpFileLoader.php
2 months ago
PoFileLoader.php
2 months ago
QtFileLoader.php
2 months ago
XliffFileLoader.php
2 months ago
YamlFileLoader.php
2 months ago
ArrayLoader.php
56 lines
| 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 | namespace ProfilePressVendor\Symfony\Component\Translation\Loader; |
| 12 | |
| 13 | use ProfilePressVendor\Symfony\Component\Translation\MessageCatalogue; |
| 14 | /** |
| 15 | * ArrayLoader loads translations from a PHP array. |
| 16 | * |
| 17 | * @author Fabien Potencier <fabien@symfony.com> |
| 18 | */ |
| 19 | class ArrayLoader implements LoaderInterface |
| 20 | { |
| 21 | /** |
| 22 | * {@inheritdoc} |
| 23 | */ |
| 24 | public function load($resource, string $locale, string $domain = 'messages') |
| 25 | { |
| 26 | $resource = $this->flatten($resource); |
| 27 | $catalogue = new MessageCatalogue($locale); |
| 28 | $catalogue->add($resource, $domain); |
| 29 | return $catalogue; |
| 30 | } |
| 31 | /** |
| 32 | * Flattens an nested array of translations. |
| 33 | * |
| 34 | * The scheme used is: |
| 35 | * 'key' => ['key2' => ['key3' => 'value']] |
| 36 | * Becomes: |
| 37 | * 'key.key2.key3' => 'value' |
| 38 | */ |
| 39 | private function flatten(array $messages): array |
| 40 | { |
| 41 | $result = []; |
| 42 | foreach ($messages as $key => $value) { |
| 43 | if (\is_array($value)) { |
| 44 | foreach ($this->flatten($value) as $k => $v) { |
| 45 | if (null !== $v) { |
| 46 | $result[$key . '.' . $k] = $v; |
| 47 | } |
| 48 | } |
| 49 | } elseif (null !== $value) { |
| 50 | $result[$key] = $value; |
| 51 | } |
| 52 | } |
| 53 | return $result; |
| 54 | } |
| 55 | } |
| 56 |