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, 'macroDomainEnd']); |
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 (class_exists('Latte\Runtime\FilterInfo')) { // Nette 2.4 |
50
|
|
View Code Duplication |
if ($node->closing) { |
|
|
|
|
51
|
|
|
return $writer->write('$_fi = new LR\FilterInfo(%var); echo %modifyContent($this->filters->filterContent("translate", $_fi, ob_get_clean()))', $node->context[0]); |
52
|
|
|
|
53
|
|
|
} elseif ($node->empty = ($node->args !== '')) { |
54
|
|
|
if ($this->containsOnlyOneWord($node)) { |
55
|
|
|
return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word))'); |
56
|
|
|
|
57
|
|
|
} else { |
58
|
|
|
return $writer->write('echo %modify(call_user_func($this->filters->translate, %node.word, %node.args))'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} else { |
62
|
|
|
return 'ob_start(function () {})'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
} else { // <= Nette 2.3 |
|
|
|
|
66
|
|
|
if ($node->closing) { |
67
|
|
|
return $writer->write('echo %modify($template->translate(ob_get_clean()))'); |
68
|
|
|
|
69
|
|
|
} elseif ($node->isEmpty = ($node->args !== '')) { |
|
|
|
|
70
|
|
|
if ($this->containsOnlyOneWord($node)) { |
71
|
|
|
return $writer->write('echo %modify($template->translate(%node.word))'); |
72
|
|
|
|
73
|
|
|
} else { |
74
|
|
|
return $writer->write('echo %modify($template->translate(%node.word, %node.args))'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
return 'ob_start()'; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param MacroNode $node |
87
|
|
|
* @param PhpWriter $writer |
88
|
|
|
*/ |
89
|
|
|
public function macroDomain(MacroNode $node, PhpWriter $writer) |
90
|
|
|
{ |
91
|
|
|
if ($node->isEmpty) { |
|
|
|
|
92
|
|
|
throw new Latte\CompileException("Expected message prefix, none given"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$node->isEmpty = $node->isEmpty || (substr($node->args, -1) === '/'); |
|
|
|
|
96
|
|
|
|
97
|
|
|
if (method_exists('Latte\Engine', 'addProvider')) { // Nette 2.4 |
98
|
|
|
return $writer->write('$_translator = \Kdyby\Translation\PrefixedTranslator::register($template, %node.word);'); |
99
|
|
|
} else { |
100
|
|
|
return $writer->write('$_translator = \Kdyby\Translation\PrefixedTranslator::register23($template, %node.word);'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param MacroNode $node |
108
|
|
|
* @param PhpWriter $writer |
109
|
|
|
*/ |
110
|
|
|
public function macroDomainEnd(MacroNode $node, PhpWriter $writer) |
111
|
|
|
{ |
112
|
|
|
if ($node->content !== NULL) { |
113
|
|
|
return $writer->write('$_translator->unregister($template);'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
private function containsOnlyOneWord(MacroNode $node) |
120
|
|
|
{ |
121
|
|
|
if (method_exists($node->tokenizer, 'fetchUntil')) { |
122
|
|
|
$result = trim($node->tokenizer->fetchUntil(',')) === trim($node->args); |
123
|
|
|
|
124
|
|
|
} else { |
125
|
|
|
$result = trim($node->tokenizer->joinUntil(',')) === trim($node->args); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$node->tokenizer->reset(); |
129
|
|
|
return $result; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.