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