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
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Object wrapper for message that can store default parameters and related information for translation. |
19
|
|
|
* |
20
|
|
|
* @author Filip Procházka <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Phrase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
use Kdyby\StrictObjects\Scream; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $message; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int|NULL |
34
|
|
|
*/ |
35
|
|
|
public $count; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
public $parameters; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string|NULL |
44
|
|
|
*/ |
45
|
|
|
public $domain; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|NULL |
49
|
|
|
*/ |
50
|
|
|
public $locale; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Translator|NULL |
54
|
|
|
*/ |
55
|
|
|
private $translator; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $message |
61
|
|
|
* @param int|array|NULL $count |
62
|
|
|
* @param string|array|NULL $parameters |
63
|
|
|
* @param string|NULL $domain |
64
|
|
|
* @param string|NULL $locale |
65
|
|
|
*/ |
66
|
|
|
public function __construct($message, $count = NULL, $parameters = NULL, $domain = NULL, $locale = NULL) |
67
|
|
|
{ |
68
|
|
|
$this->message = $message; |
69
|
|
|
|
70
|
|
View Code Duplication |
if (is_array($count)) { |
|
|
|
|
71
|
|
|
$locale = ($domain !== NULL) ? (string) $domain : NULL; |
72
|
|
|
$domain = ($parameters !== NULL) ? (string) $parameters : NULL; |
73
|
|
|
$parameters = $count; |
74
|
|
|
$count = NULL; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->count = $count !== NULL ? (int) $count : NULL; |
78
|
|
|
$this->parameters = (array) $parameters; |
79
|
|
|
$this->domain = $domain; |
80
|
|
|
$this->locale = $locale; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Translator $translator |
87
|
|
|
* @param int|NULL $count |
88
|
|
|
* @param array $parameters |
89
|
|
|
* @param string|NULL $domain |
90
|
|
|
* @param string|NULL $locale |
91
|
|
|
* @return string|\Nette\Utils\IHtmlString|\Latte\Runtime\IHtmlString |
92
|
|
|
*/ |
93
|
|
|
public function translate(Translator $translator, $count = NULL, array $parameters = [], $domain = NULL, $locale = NULL) |
94
|
|
|
{ |
95
|
|
|
if (!is_string($this->message)) { |
96
|
|
|
throw new InvalidStateException("Message is not a string, type " . gettype($this->message) . ' given.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$count = ($count !== NULL) ? (int) $count : $this->count; |
100
|
|
|
$parameters = !empty($parameters) ? $parameters : $this->parameters; |
101
|
|
|
$domain = ($domain !== NULL) ? $domain : $this->domain; |
102
|
|
|
$locale = ($locale !== NULL) ? $locale : $this->locale; |
103
|
|
|
|
104
|
|
|
return $translator->translate($this->message, $count, (array) $parameters, $domain, $locale); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @internal |
111
|
|
|
* @param \Kdyby\Translation\Translator $translator |
112
|
|
|
*/ |
113
|
|
|
public function setTranslator(Translator $translator) |
114
|
|
|
{ |
115
|
|
|
$this->translator = $translator; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
|
120
|
|
|
public function __toString() |
121
|
|
|
{ |
122
|
|
|
if ($this->translator === NULL) { |
123
|
|
|
return $this->message; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
return (string) $this->translate($this->translator); |
128
|
|
|
|
129
|
|
|
} catch (\Exception $e) { |
130
|
|
|
trigger_error($e->getMessage(), E_USER_ERROR); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return ''; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
|
138
|
|
|
public function __sleep() |
139
|
|
|
{ |
140
|
|
|
$this->translator = NULL; |
141
|
|
|
return ['message', 'count', 'parameters', 'domain', 'locale']; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
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.