Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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