Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Loader/NeonFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
class NeonFileLoader extends \Symfony\Component\Translation\Loader\ArrayLoader implements \Symfony\Component\Translation\Loader\LoaderInterface
{

/**
* @internal
*/
const INCLUDES_KEY = 'includes';

/**
* {@inheritdoc}
*/
Expand All @@ -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));
}
Expand All @@ -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;
}

}
1 change: 1 addition & 0 deletions tests/KdybyTests/Translation/ExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
6 changes: 5 additions & 1 deletion tests/KdybyTests/Translation/TranslateMacrosTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
4 changes: 4 additions & 0 deletions tests/KdybyTests/Translation/lang/default.cs_CZ.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
homepage:
hello2: Default ahoj světe
namedHello2: default ahoj %name%
namedHelloCounting2: Default ahoj %name%|Hola %name%
3 changes: 3 additions & 0 deletions tests/KdybyTests/Translation/lang/front.cs_CZ.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- default.cs_CZ.neon

homepage:
hello: Ahoj světe
namedHello: Ahoj %name%
Expand Down