-
Notifications
You must be signed in to change notification settings - Fork 82
/
TranslateMacros.php
106 lines (87 loc) · 3.11 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
<?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\Translation\PrefixedTranslator;
use Latte\Compiler;
use Latte\Engine;
use Latte\MacroNode;
use Latte\PhpWriter;
class TranslateMacros extends \Latte\Macros\MacroSet
{
use \Kdyby\StrictObjects\Scream;
public static function install(Compiler $compiler)
{
$me = new static($compiler);
/** @var \Kdyby\Translation\Latte\TranslateMacros $me */
$me->addMacro('_', [$me, 'macroTranslate'], [$me, 'macroTranslate']);
$me->addMacro('translator', [$me, 'macroDomain'], [$me, 'macroDomain']);
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) {
if (strpos($node->content, '<?php') === FALSE) {
$value = var_export($node->content, TRUE);
$node->content = '';
} else {
$node->openingCode = '<?php ob_start(function () {}) ?>' . $node->openingCode;
$value = 'ob_get_clean()';
}
if (!defined(Engine::class . '::VERSION_ID') || Engine::VERSION_ID < 20804) {
return $writer->write('$_fi = new LR\FilterInfo(%var); echo %modifyContent($this->filters->filterContent("translate", $_fi, %raw))', $node->context[0], $value);
}
if (Engine::VERSION_ID >= 20900 && Engine::VERSION_ID < 20902) {
return $writer->write('$__fi = new LR\FilterInfo(%var); echo %modifyContent($this->filters->filterContent("translate", $__fi, %raw))', $node->context[0], $value);
}
return $writer->write('$ʟ_fi = new LR\FilterInfo(%var); echo %modifyContent($this->filters->filterContent("translate", $ʟ_fi, %raw))', $node->context[0], $value);
} elseif ($node->args !== '') {
$node->empty = TRUE;
if ($this->containsOnlyOneWord($node)) {
return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word))');
} else {
return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word, %node.args))');
}
}
}
/**
* @param \Latte\MacroNode $node
* @param \Latte\PhpWriter $writer
* @throws \Latte\CompileException for invalid domain
* @return string|NULL
*/
public function macroDomain(MacroNode $node, PhpWriter $writer)
{
if ($node->closing) {
if ($node->content !== NULL && $node->content !== '') {
return $writer->write('$_translator->unregister($this);');
}
} else {
if ($node->empty) {
throw new \Latte\CompileException('Expected message prefix, none given');
}
return $writer->write('$_translator = ' . PrefixedTranslator::class . '::register($this, %node.word);');
}
}
/**
* @param \Latte\MacroNode $node
* @return bool
*/
private function containsOnlyOneWord(MacroNode $node)
{
$result = trim($node->tokenizer->joinUntil(',')) === trim($node->args);
$node->tokenizer->reset();
return $result;
}
}