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