Skip to content

Commit 2cd9393

Browse files
authored
Merge pull request #28 from voda/custom-translate-mcaro
LatteFilter: add support for custom macros and Nette 0.9 '!' noescape
2 parents 6dd6e97 + bedaa3d commit 2cd9393

4 files changed

Lines changed: 61 additions & 2 deletions

File tree

src/Filters/LatteFilter.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ private function findMacroName(string $text, array $functions): ?string {
9595
}
9696

9797
private function trimMacroValue(string $name, string $value): string {
98-
$offset = strlen(ltrim($name, '!_'));
99-
return substr($value, $offset);
98+
if (strpos($name, '!') === 0) {
99+
// exclamation mark is never removed
100+
return trim(substr($value, strlen($name)));
101+
}
102+
103+
if (strpos($name, '_') === 0) {
104+
// only underscore is removed
105+
$offset = strlen(ltrim($name, '_'));
106+
return substr($value, $offset);
107+
}
108+
109+
return $value;
100110
}
101111
}

tests/unit/GettextExtractor/Filters/LatteFilterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,47 @@ public function testExtract(): void {
4141
GE\Extractor::PLURAL => 'I see %d little indians!',
4242
GE\Extractor::CONTEXT => 'context'
4343
), $messages);
44+
45+
self::assertContains(array(
46+
GE\Extractor::LINE => 8,
47+
GE\Extractor::SINGULAR => 'A message!'
48+
), $messages);
49+
50+
self::assertContains(array(
51+
GE\Extractor::LINE => 9,
52+
GE\Extractor::SINGULAR => 'Another message!',
53+
GE\Extractor::CONTEXT => 'context'
54+
), $messages);
55+
56+
self::assertContains(array(
57+
GE\Extractor::LINE => 10,
58+
GE\Extractor::SINGULAR => 'I see %d little indian!',
59+
GE\Extractor::PLURAL => 'I see %d little indians!'
60+
), $messages);
61+
62+
self::assertContains(array(
63+
GE\Extractor::LINE => 11,
64+
GE\Extractor::SINGULAR => 'I see %d little indian!',
65+
GE\Extractor::PLURAL => 'I see %d little indians!',
66+
GE\Extractor::CONTEXT => 'context'
67+
), $messages);
68+
}
69+
70+
public function testCustomMacros(): void {
71+
$this->object->addFunction('custom', 1);
72+
$this->object->addFunction('!custom', 1);
73+
74+
$messages = $this->object->extract(__DIR__ . '/../../data/latte/custom.latte');
75+
76+
self::assertContains(array(
77+
GE\Extractor::LINE => 1,
78+
GE\Extractor::SINGULAR => 'A custom message!'
79+
), $messages);
80+
81+
self::assertContains(array(
82+
GE\Extractor::LINE => 2,
83+
GE\Extractor::SINGULAR => 'An unescaped custom message!'
84+
), $messages);
4485
}
4586

4687
public function testNoValidMessages(): void {

tests/unit/data/latte/custom.latte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{custom 'A custom message!'}
2+
{!custom 'An unescaped custom message!'}

tests/unit/data/latte/default.latte

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
{_p'context', 'Another message!'}
44
{_n'I see %d little indian!', 'I see %d little indians!', 3|printf:3}
55
{_np'context', 'I see %d little indian!', 'I see %d little indians!', $number |printf:$number}
6+
7+
{* Nette 0.9 noescape format *}
8+
{!_'A message!'}
9+
{!_p'context', 'Another message!'}
10+
{!_n'I see %d little indian!', 'I see %d little indians!', 3|printf:3}
11+
{!_np'context', 'I see %d little indian!', 'I see %d little indians!', $number |printf:$number}

0 commit comments

Comments
 (0)