diff --git a/src/Loader/NeonFileLoader.php b/src/Loader/NeonFileLoader.php index cdba3c92..426613d0 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 $resource + * @param $messages + * @return array + */ + private function recursiveLoadResources($resource, array $messages): array + { + 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/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%