PluginProbe
Independent Analytics – WordPress Analytics Plugin / 2.11.0
Independent Analytics – WordPress Analytics Plugin v2.11.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / vendor / symfony / translation / Loader / IcuResFileLoader.php
IcuResFileLoader.php
83 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 IAWPSCOPED\Symfony\Component\Translation\Loader;
12
13 use IAWPSCOPED\Symfony\Component\Config\Resource\DirectoryResource;
14 use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidResourceException;
15 use IAWPSCOPED\Symfony\Component\Translation\Exception\NotFoundResourceException;
16 use IAWPSCOPED\Symfony\Component\Translation\MessageCatalogue;
17 /**
18 * IcuResFileLoader loads translations from a resource bundle.
19 *
20 * @author stealth35
21 * @internal
22 */
23 class IcuResFileLoader implements LoaderInterface
24 {
25 /**
26 * {@inheritdoc}
27 */
28 public function load($resource, string $locale, string $domain = 'messages')
29 {
30 if (!\stream_is_local($resource)) {
31 throw new InvalidResourceException(\sprintf('This is not a local file "%s".', $resource));
32 }
33 if (!\is_dir($resource)) {
34 throw new NotFoundResourceException(\sprintf('File "%s" not found.', $resource));
35 }
36 try {
37 $rb = new \ResourceBundle($locale, $resource);
38 } catch (\Exception $e) {
39 $rb = null;
40 }
41 if (!$rb) {
42 throw new InvalidResourceException(\sprintf('Cannot load resource "%s".', $resource));
43 } elseif (\intl_is_failure($rb->getErrorCode())) {
44 throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
45 }
46 $messages = $this->flatten($rb);
47 $catalogue = new MessageCatalogue($locale);
48 $catalogue->add($messages, $domain);
49 if (\class_exists(DirectoryResource::class)) {
50 $catalogue->addResource(new DirectoryResource($resource));
51 }
52 return $catalogue;
53 }
54 /**
55 * Flattens an ResourceBundle.
56 *
57 * The scheme used is:
58 * key { key2 { key3 { "value" } } }
59 * Becomes:
60 * 'key.key2.key3' => 'value'
61 *
62 * This function takes an array by reference and will modify it
63 *
64 * @param \ResourceBundle $rb The ResourceBundle that will be flattened
65 * @param array $messages Used internally for recursive calls
66 * @param string|null $path Current path being parsed, used internally for recursive calls
67 *
68 * @return array
69 */
70 protected function flatten(\ResourceBundle $rb, array &$messages = [], ?string $path = null)
71 {
72 foreach ($rb as $key => $value) {
73 $nodePath = $path ? $path . '.' . $key : $key;
74 if ($value instanceof \ResourceBundle) {
75 $this->flatten($value, $messages, $nodePath);
76 } else {
77 $messages[$nodePath] = $value;
78 }
79 }
80 return $messages;
81 }
82 }
83