Skip to content

Commit

Permalink
Add support for inheritance in neon translation files
Browse files Browse the repository at this point in the history
  • Loading branch information
Olicek committed Jun 16, 2017
1 parent 196e246 commit a32d9f9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
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 $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;
}
}
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

0 comments on commit a32d9f9

Please sign in to comment.