-
Notifications
You must be signed in to change notification settings - Fork 84
/
TranslateMacros.php
109 lines (81 loc) · 2.63 KB
/
TranslateMacros.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka (filip@prochazka.su)
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/
namespace Kdyby\Translation\Latte;
use Kdyby;
use Latte;
use Latte\Compiler;
use Latte\MacroNode;
use Latte\PhpWriter;
use Latte\Macros\MacroSet;
/**
* @author Filip Procházka <filip@prochazka.su>
*/
class TranslateMacros extends MacroSet
{
public static function install(Compiler $compiler)
{
$me = new static($compiler);
/** @var TranslateMacros $me */
$me->addMacro('_', [$me, 'macroTranslate'], [$me, 'macroTranslate']);
$me->addMacro('translator', [$me, 'macroDomain'], [$me, 'macroDomainEnd']);
return $me;
}
/**
* {_$var |modifiers}
* {_$var, $count |modifiers}
* {_"Sample message", $count |modifiers}
* {_some.string.id, $count |modifiers}
*/
public function macroTranslate(MacroNode $node, PhpWriter $writer)
{
if ($node->closing) {
return $writer->write('$_fi = new LR\FilterInfo(\Latte\Engine::CONTENT_TEXT); echo $this->filters->filterContent("translate", $_fi, ob_get_clean())');
} elseif ($node->isEmpty = ($node->args !== '')) {
if ($this->containsOnlyOneWord($node)) {
return $writer->write('$_fi = new LR\FilterInfo(\Latte\Engine::CONTENT_TEXT); echo $this->filters->filterContent("translate", $_fi, %node.word)');
} else {
return $writer->write('$_fi = new LR\FilterInfo(\Latte\Engine::CONTENT_TEXT); echo $this->filters->filterContent("translate", $_fi, %node.word, %node.args)');
}
} else {
return 'ob_start()';
}
}
/**
* @param MacroNode $node
* @param PhpWriter $writer
*/
public function macroDomain(MacroNode $node, PhpWriter $writer)
{
if ($node->isEmpty) {
throw new Latte\CompileException("Expected message prefix, none given");
}
$node->isEmpty = $node->isEmpty || (substr($node->args, -1) === '/');
return $writer->write('$_translator = \Kdyby\Translation\PrefixedTranslator::register($template, %node.word);');
}
/**
* @param MacroNode $node
* @param PhpWriter $writer
*/
public function macroDomainEnd(MacroNode $node, PhpWriter $writer)
{
if ($node->content !== NULL) {
return $writer->write('$_translator->unregister($template);');
}
}
private function containsOnlyOneWord(MacroNode $node)
{
if (method_exists($node->tokenizer, 'fetchUntil')) {
$result = trim($node->tokenizer->fetchUntil(',')) === trim($node->args);
} else {
$result = trim($node->tokenizer->joinUntil(',')) === trim($node->args);
}
$node->tokenizer->reset();
return $result;
}
}