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; |
15
|
|
|
use Nette\Utils\Strings; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Filip Procházka <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class PrefixedTranslator implements ITranslator |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
use Kdyby\StrictObjects\Scream; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Kdyby\Translation\ITranslator|\Kdyby\Translation\Translator|\Kdyby\Translation\PrefixedTranslator |
29
|
|
|
*/ |
30
|
|
|
private $translator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $prefix; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $prefix |
41
|
|
|
* @param \Kdyby\Translation\ITranslator $translator |
42
|
|
|
* @throws \Kdyby\Translation\InvalidArgumentException |
43
|
|
|
*/ |
44
|
|
|
public function __construct($prefix, ITranslator $translator) |
45
|
|
|
{ |
46
|
|
View Code Duplication |
if (!$translator instanceof Translator && !$translator instanceof PrefixedTranslator) { |
|
|
|
|
47
|
|
|
throw new InvalidArgumentException(sprintf( |
48
|
|
|
'The given translator must be instance of Kdyby\Translation\Translator or Kdyby\Translation\PrefixedTranslator, bug %s was given', |
49
|
|
|
get_class($translator) |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($translator instanceof PrefixedTranslator) { // todo: this is just an experiment |
54
|
|
|
$translator = $translator->unwrap(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->translator = $translator; |
58
|
|
|
$this->prefix = rtrim($prefix, '.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string|\Kdyby\Translation\Phrase $message |
65
|
|
|
* @param int|array|NULL $count |
66
|
|
|
* @param array|string|NULL $parameters |
67
|
|
|
* @param string|NULL $domain |
68
|
|
|
* @param string|NULL $locale |
69
|
|
|
* @return string|\Nette\Utils\IHtmlString|\Latte\Runtime\IHtmlString |
70
|
|
|
*/ |
71
|
|
|
public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL) |
72
|
|
|
{ |
73
|
|
|
$translationString = ($message instanceof Phrase ? $message->message : $message); |
74
|
|
|
$prefix = $this->prefix . '.'; |
75
|
|
|
|
76
|
|
|
if (Strings::startsWith($message, '//')) { |
77
|
|
|
$prefix = NULL; |
78
|
|
|
$translationString = Strings::substring($translationString, 2); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($message instanceof Phrase) { |
82
|
|
|
return $this->translator->translate(new Phrase($prefix . $translationString, $message->count, $message->parameters, $message->domain, $message->locale)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
View Code Duplication |
if (is_array($count)) { |
|
|
|
|
86
|
|
|
$locale = $domain !== NULL ? (string) $domain : NULL; |
87
|
|
|
$domain = $parameters !== NULL && !empty($parameters) ? (string) $parameters : NULL; |
88
|
|
|
$parameters = $count; |
89
|
|
|
$count = NULL; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->translator->translate($prefix . $translationString, $count, (array) $parameters, $domain, $locale); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return \Kdyby\Translation\ITranslator |
99
|
|
|
*/ |
100
|
|
|
public function unwrap() |
101
|
|
|
{ |
102
|
|
|
return $this->translator; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param Latte\Runtime\Template $template |
109
|
|
|
* @return \Kdyby\Translation\ITranslator |
110
|
|
|
*/ |
111
|
|
|
public function unregister(Latte\Runtime\Template $template) |
112
|
|
|
{ |
113
|
|
|
$translator = $this->unwrap(); |
114
|
|
|
self::overrideTemplateTranslator($template, $translator); |
115
|
|
|
return $translator; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Latte\Runtime\Template $template |
122
|
|
|
* @param string $prefix |
123
|
|
|
* @throws \Kdyby\Translation\InvalidArgumentException |
124
|
|
|
* @return \Kdyby\Translation\ITranslator |
125
|
|
|
*/ |
126
|
|
|
public static function register(Latte\Runtime\Template $template, $prefix) |
127
|
|
|
{ |
128
|
|
|
$translator = new static($prefix, $template->global->translator); |
129
|
|
|
self::overrideTemplateTranslator($template, $translator); |
130
|
|
|
return $translator; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
private static function overrideTemplateTranslator(Latte\Runtime\Template $template, ITranslator $translator) |
136
|
|
|
{ |
137
|
|
|
$template->getEngine()->addFilter('translate', [new TemplateHelpers($translator), 'translate']); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
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.