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; |
12
|
|
|
|
13
|
|
|
use Kdyby; |
14
|
|
|
use Latte\Engine; |
15
|
|
|
use Latte\Runtime\FilterInfo; |
16
|
|
|
use Nette; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Filip Procházka <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class TemplateHelpers extends Nette\Object |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ITranslator |
28
|
|
|
*/ |
29
|
|
|
private $translator; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function __construct(ITranslator $translator) |
34
|
|
|
{ |
35
|
|
|
$this->translator = $translator; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public function register(Engine $engine) |
41
|
|
|
{ |
42
|
|
|
if (class_exists('Latte\Runtime\FilterInfo')) { |
43
|
|
|
$engine->addFilter('translate', [$this, 'translateFilterAware']); |
44
|
|
|
} else { |
45
|
|
|
$engine->addFilter('translate', [$this, 'translate']); |
46
|
|
|
} |
47
|
|
|
$engine->addFilter('getTranslator', [$this, 'getTranslator']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return ITranslator |
54
|
|
|
*/ |
55
|
|
|
public function getTranslator() |
56
|
|
|
{ |
57
|
|
|
return $this->translator; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL) |
63
|
|
|
{ |
64
|
|
View Code Duplication |
if (is_array($count)) { |
|
|
|
|
65
|
|
|
$locale = $domain ?: NULL; |
66
|
|
|
$domain = $parameters ?: NULL; |
67
|
|
|
$parameters = $count ?: []; |
68
|
|
|
$count = NULL; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->translator->translate($message, $count, (array) $parameters, $domain, $locale); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
public function translateFilterAware(FilterInfo $filterInfo, $message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL) |
77
|
|
|
{ |
78
|
|
|
return $this->translate($message, $count, $parameters, $domain, $locale); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @deprecated |
85
|
|
|
*/ |
86
|
|
|
public function loader($method) |
87
|
|
|
{ |
88
|
|
|
if (method_exists($this, $method) && strtolower($method) !== 'register') { |
89
|
|
|
return [$this, $method]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
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.