Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen 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\Config\Resource\DirectoryResource;
15
use Symfony\Component\Translation\Exception\InvalidResourceException;
16
use Symfony\Component\Translation\Exception\NotFoundResourceException;
17
use Symfony\Component\Translation\MessageCatalogue;
18
 
19
/**
20
 * IcuResFileLoader loads translations from a resource bundle.
21
 *
22
 * @author stealth35
23
 */
24
class IcuResFileLoader implements LoaderInterface
25
{
26
    public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
27
    {
28
        if (!stream_is_local($resource)) {
29
            throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
30
        }
31
 
32
        if (!is_dir($resource)) {
33
            throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
34
        }
35
 
36
        try {
37
            $rb = new \ResourceBundle($locale, $resource);
38
        } catch (\Exception) {
39
            $rb = null;
40
        }
41
 
42
        if (!$rb) {
43
            throw new InvalidResourceException(sprintf('Cannot load resource "%s".', $resource));
44
        } elseif (intl_is_failure($rb->getErrorCode())) {
45
            throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
46
        }
47
 
48
        $messages = $this->flatten($rb);
49
        $catalogue = new MessageCatalogue($locale);
50
        $catalogue->add($messages, $domain);
51
 
52
        if (class_exists(DirectoryResource::class)) {
53
            $catalogue->addResource(new DirectoryResource($resource));
54
        }
55
 
56
        return $catalogue;
57
    }
58
 
59
    /**
60
     * Flattens an ResourceBundle.
61
     *
62
     * The scheme used is:
63
     *   key { key2 { key3 { "value" } } }
64
     * Becomes:
65
     *   'key.key2.key3' => 'value'
66
     *
67
     * This function takes an array by reference and will modify it
68
     *
69
     * @param \ResourceBundle $rb       The ResourceBundle that will be flattened
70
     * @param array           $messages Used internally for recursive calls
71
     * @param string          $path     Current path being parsed, used internally for recursive calls
72
     */
73
    protected function flatten(\ResourceBundle $rb, array &$messages = [], string $path = null): array
74
    {
75
        foreach ($rb as $key => $value) {
76
            $nodePath = $path ? $path.'.'.$key : $key;
77
            if ($value instanceof \ResourceBundle) {
78
                $this->flatten($value, $messages, $nodePath);
79
            } else {
80
                $messages[$nodePath] = $value;
81
            }
82
        }
83
 
84
        return $messages;
85
    }
86
}