From 6f6d497a96d0cab37f98e884cc6bd3f7abffb929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Oli=C5=A1ar?= Date: Fri, 16 Jun 2017 22:28:11 +0200 Subject: [PATCH] Add support for inheritance in neon translation files --- src/Loader/NeonFileLoader.php | 31 +++++++++++++++++++ .../KdybyTests/Translation/ExtensionTest.phpt | 1 + .../Translation/TranslateMacrosTest.phpt | 6 +++- .../data/files/Homepage.default.latte | 4 +++ .../Translation/lang/default.cs_CZ.neon | 4 +++ .../Translation/lang/front.cs_CZ.neon | 3 ++ 6 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/KdybyTests/Translation/lang/default.cs_CZ.neon diff --git a/src/Loader/NeonFileLoader.php b/src/Loader/NeonFileLoader.php index cdba3c92..2018d6c7 100644 --- a/src/Loader/NeonFileLoader.php +++ b/src/Loader/NeonFileLoader.php @@ -19,6 +19,11 @@ class NeonFileLoader extends \Symfony\Component\Translation\Loader\ArrayLoader implements \Symfony\Component\Translation\Loader\LoaderInterface { + /** + * @internal + */ + const INCLUDES_KEY = 'includes'; + /** * {@inheritdoc} */ @@ -43,6 +48,8 @@ public function load($resource, $locale, $domain = 'messages') $messages = []; } + $messages = $this->recursiveLoadResources($resource, $messages); + if (!is_array($messages)) { throw new \Symfony\Component\Translation\Exception\InvalidResourceException(sprintf('The file "%s" must contain a Neon array.', $resource)); } @@ -53,4 +60,28 @@ public function load($resource, $locale, $domain = 'messages') return $catalogue; } + /** + * @param string $resource + * @param array $messages + * @return array + */ + private function recursiveLoadResources($resource, array $messages) + { + if (isset($messages[self::INCLUDES_KEY])) { + foreach ($messages[self::INCLUDES_KEY] as $include) { + if (!preg_match('#([a-z]:)?[/\\\\]#Ai', $include)) { + $include = dirname($resource) . '/' . $include; + } + + $parent = array_filter(Neon::decode(file_get_contents($include))); + $parent = $this->recursiveLoadResources($include, $parent); + $messages = array_merge($parent, array_filter($messages)); + } + + unset($messages[self::INCLUDES_KEY]); + } + + return $messages; + } + } diff --git a/tests/KdybyTests/Translation/ExtensionTest.phpt b/tests/KdybyTests/Translation/ExtensionTest.phpt index 686170e7..1cf1eea6 100644 --- a/tests/KdybyTests/Translation/ExtensionTest.phpt +++ b/tests/KdybyTests/Translation/ExtensionTest.phpt @@ -29,6 +29,7 @@ class ExtensionTest extends \KdybyTests\Translation\TestCase Assert::true($translator instanceof SymfonyTranslator); Assert::same('Ahoj světe', $translator->translate('homepage.hello', NULL, [], 'front', 'cs')); + Assert::same('Default ahoj světe', $translator->translate('homepage.hello2', NULL, [], 'front', 'cs')); Assert::same('Hello world', $translator->translate('homepage.hello', NULL, [], 'front', 'en')); Assert::same('front.not.found', $translator->translate('front.not.found')); diff --git a/tests/KdybyTests/Translation/TranslateMacrosTest.phpt b/tests/KdybyTests/Translation/TranslateMacrosTest.phpt index 13acf24b..b9c3abb0 100644 --- a/tests/KdybyTests/Translation/TranslateMacrosTest.phpt +++ b/tests/KdybyTests/Translation/TranslateMacrosTest.phpt @@ -63,7 +63,11 @@ Hello Peter|Helloes Peter front.missingKey.namedHelloCounting front.missingKey.namedHelloCounting -front.missingKey.namedHelloCounting' . "\n", $this->template->__toString()); +front.missingKey.namedHelloCounting + +Default ahoj světe +Default ahoj Peter +Default Helloes Peter' . "\n", $this->template->__toString()); } public function testRenderTranslateNoescape() diff --git a/tests/KdybyTests/Translation/data/files/Homepage.default.latte b/tests/KdybyTests/Translation/data/files/Homepage.default.latte index 4b86afad..dad5e513 100644 --- a/tests/KdybyTests/Translation/data/files/Homepage.default.latte +++ b/tests/KdybyTests/Translation/data/files/Homepage.default.latte @@ -17,3 +17,7 @@ {_front.missingKey.namedHelloCounting, 3, NULL, NULL, 'en'} {_front.missingKey.namedHelloCounting, 3, array('name' => 'Peter'), NULL, 'en'} {_front.missingKey.namedHelloCounting, array('name' => 'Peter'), NULL, 'en'} + +{_front.homepage.hello2, 3} +{_front.homepage.namedHello2, [name => 'Peter']} +{_front.homepage.namedHelloCounting2, 3, array('name' => 'Peter'), NULL, 'en'} diff --git a/tests/KdybyTests/Translation/lang/default.cs_CZ.neon b/tests/KdybyTests/Translation/lang/default.cs_CZ.neon new file mode 100644 index 00000000..a8f68044 --- /dev/null +++ b/tests/KdybyTests/Translation/lang/default.cs_CZ.neon @@ -0,0 +1,4 @@ +homepage: + hello2: Default ahoj světe + namedHello2: default ahoj %name% + namedHelloCounting2: Default ahoj %name%|Hola %name% \ No newline at end of file diff --git a/tests/KdybyTests/Translation/lang/front.cs_CZ.neon b/tests/KdybyTests/Translation/lang/front.cs_CZ.neon index 66d6a57d..3637d7bb 100644 --- a/tests/KdybyTests/Translation/lang/front.cs_CZ.neon +++ b/tests/KdybyTests/Translation/lang/front.cs_CZ.neon @@ -1,3 +1,6 @@ +includes: + - default.cs_CZ.neon + homepage: hello: Ahoj světe namedHello: Ahoj %name%