Skip to content

Commit 6f6d497

Browse files
committed
Add support for inheritance in neon translation files
1 parent 196e246 commit 6f6d497

6 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/Loader/NeonFileLoader.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
class NeonFileLoader extends \Symfony\Component\Translation\Loader\ArrayLoader implements \Symfony\Component\Translation\Loader\LoaderInterface
2020
{
2121

22+
/**
23+
* @internal
24+
*/
25+
const INCLUDES_KEY = 'includes';
26+
2227
/**
2328
* {@inheritdoc}
2429
*/
@@ -43,6 +48,8 @@ public function load($resource, $locale, $domain = 'messages')
4348
$messages = [];
4449
}
4550

51+
$messages = $this->recursiveLoadResources($resource, $messages);
52+
4653
if (!is_array($messages)) {
4754
throw new \Symfony\Component\Translation\Exception\InvalidResourceException(sprintf('The file "%s" must contain a Neon array.', $resource));
4855
}
@@ -53,4 +60,28 @@ public function load($resource, $locale, $domain = 'messages')
5360
return $catalogue;
5461
}
5562

63+
/**
64+
* @param string $resource
65+
* @param array $messages
66+
* @return array
67+
*/
68+
private function recursiveLoadResources($resource, array $messages)
69+
{
70+
if (isset($messages[self::INCLUDES_KEY])) {
71+
foreach ($messages[self::INCLUDES_KEY] as $include) {
72+
if (!preg_match('#([a-z]:)?[/\\\\]#Ai', $include)) {
73+
$include = dirname($resource) . '/' . $include;
74+
}
75+
76+
$parent = array_filter(Neon::decode(file_get_contents($include)));
77+
$parent = $this->recursiveLoadResources($include, $parent);
78+
$messages = array_merge($parent, array_filter($messages));
79+
}
80+
81+
unset($messages[self::INCLUDES_KEY]);
82+
}
83+
84+
return $messages;
85+
}
86+
5687
}

tests/KdybyTests/Translation/ExtensionTest.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ExtensionTest extends \KdybyTests\Translation\TestCase
2929
Assert::true($translator instanceof SymfonyTranslator);
3030

3131
Assert::same('Ahoj světe', $translator->translate('homepage.hello', NULL, [], 'front', 'cs'));
32+
Assert::same('Default ahoj světe', $translator->translate('homepage.hello2', NULL, [], 'front', 'cs'));
3233
Assert::same('Hello world', $translator->translate('homepage.hello', NULL, [], 'front', 'en'));
3334

3435
Assert::same('front.not.found', $translator->translate('front.not.found'));

tests/KdybyTests/Translation/TranslateMacrosTest.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ Hello Peter|Helloes Peter
6363
6464
front.missingKey.namedHelloCounting
6565
front.missingKey.namedHelloCounting
66-
front.missingKey.namedHelloCounting' . "\n", $this->template->__toString());
66+
front.missingKey.namedHelloCounting
67+
68+
Default ahoj světe
69+
Default ahoj Peter
70+
Default Helloes Peter' . "\n", $this->template->__toString());
6771
}
6872

6973
public function testRenderTranslateNoescape()

tests/KdybyTests/Translation/data/files/Homepage.default.latte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
{_front.missingKey.namedHelloCounting, 3, NULL, NULL, 'en'}
1818
{_front.missingKey.namedHelloCounting, 3, array('name' => 'Peter'), NULL, 'en'}
1919
{_front.missingKey.namedHelloCounting, array('name' => 'Peter'), NULL, 'en'}
20+
21+
{_front.homepage.hello2, 3}
22+
{_front.homepage.namedHello2, [name => 'Peter']}
23+
{_front.homepage.namedHelloCounting2, 3, array('name' => 'Peter'), NULL, 'en'}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
homepage:
2+
hello2: Default ahoj světe
3+
namedHello2: default ahoj %name%
4+
namedHelloCounting2: Default ahoj %name%|Hola %name%

tests/KdybyTests/Translation/lang/front.cs_CZ.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
includes:
2+
- default.cs_CZ.neon
3+
14
homepage:
25
hello: Ahoj světe
36
namedHello: Ahoj %name%

0 commit comments

Comments
 (0)