-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGettextTranslation.php
246 lines (223 loc) · 7.54 KB
/
GettextTranslation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* Manipulation with one gettext translation
* @author Pavel Železný <[email protected]>
*/
class GettextTranslation {
/** @var array Singular and optionaly plural orginal untranslated string */
private $original = array();
/** @var string Context of translation */
private $context = NULL;
/** @var array Translated strings */
private $translation = array();
/** @var array Comments */
private $comments = array();
/**
* Constructor
* @author Pavel Železný <[email protected]>
* @param string|array $original singular and optionaly plural form of orginal untranslated string
* @param string|array $context context of untranslated string
* @param string|array $translation singular and optionaly plural form of translated string
* @return void
* @throws \InvalidArgumentException
*/
public function __construct($original = NULL, $context = NULL, $translation = NULL) {
if ($original !== NULL) {
$this->setOriginal($original);
}
if ($context !== NULL) {
$this->setContext($context);
}
if ($translation !== NULL) {
if (is_array($translation)) {
$this->setTranslations(array_values($translation));
} else {
$this->setTranslation($translation, 0);
}
}
}
/**
* Original getter
* @author Pavel Železný <[email protected]>
* @return array
*/
public function getOriginal() {
return $this->original;
}
/**
* Context getter
* @author Pavel Železný <[email protected]>
* @return string
*/
public function getContext() {
return $this->context;
}
/**
* Translations getter
* @author Pavel Železný <[email protected]>
* @return array
*/
public function getTranslations() {
return $this->translation;
}
/**
* Translations getter
* @author Pavel Železný <[email protected]>
* @param int $index Index of translation
* @return array
* @throws \InvalidArgumentException
*/
public function getTranslation($index) {
if (!isset($this->translation[$index])) {
throw new \InvalidArgumentException('Defined index of translation is not exist.');
}
return $this->translation[$index];
}
/**
* Comments getter
* @author Pavel Železný <[email protected]>
* @return array
*/
public function getComments() {
return $this->comments;
}
/**
* Comment getter
* @author Pavel Železný <[email protected]>
* @param string $type Type of returned comment
* @return array
* @throws \InvalidArgumentException
*/
public function getComment($type) {
if (in_array($type, $this->getAllowedCommentTypes()) === FALSE) {
throw new \InvalidArgumentException('Unsupported comment type. Supported type is one from following: ' . implode(', ', $this->getAllowedCommentTypes()) . '.');
} elseif (!isset($this->comment[$type])) {
throw new \InvalidArgumentException('Defined type of comment is not exist.');
}
return $this->comment[$type];
}
/**
* Allowed comment types
* @author Pavel Železný <[email protected]>
* @return array
*/
private function getAllowedCommentTypes() {
return array(
'comment',
'extracted-comment',
'reference',
'flag',
'previous-context',
'previous-untranslated-string',
);
}
/**
* Original setter
* @author Pavel Železný <[email protected]>
* @param string|array $original singular and optionaly plural form of orginal untranslated string
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
protected function setOriginal($original) {
if (trim(is_array($original) ? $original[0] : $original) == '') {
throw new \InvalidArgumentException('Untranslated string cannot be empty.');
} elseif (count($this->original) > 0) {
throw new \InvalidArgumentException('Unable to change original untranslated string. Make new translation instead.');
}
$this->original = (array) $original;
return $this;
}
/**
* Context setter
* @author Pavel Železný <[email protected]>
* @param string $original context of untranslated string
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function setContext($context) {
if (is_string($context) === FALSE) {
throw new \InvalidArgumentException('Context have to be string.');
} elseif ($this->context !== NULL) {
throw new \InvalidArgumentException('Unable to change context of original untranslated string. Make new translation instead.');
}
$this->context = $context;
return $this;
}
/**
* Singular and plural variant of translation setter
* @author Pavel Železný <[email protected]>
* @param array $translation singular and optionaly plural form of translated string
* @return \GettextTranslation provides a fluent interface
* @throw \InvalidArgumentException
*/
public function setTranslations($translations) {
foreach (array_values($translations) as $index => $translation) {
$this->setTranslation($translation, $index);
}
}
/**
* One translation setter (singular or plural)
* @author Pavel Železný <[email protected]>
* @param string|array $translation singular and optionaly plural form of translated string
* @param string $index optionaly we can set index of plural
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function setTranslation($translation, $index = 0) {
if (is_string($translation) === FALSE) {
throw new \InvalidArgumentException('Translation have to be string.');
} elseif (is_int($index) === FALSE) {
throw new \InvalidArgumentException('Index of translation have to be integer.');
} elseif (($index > 0) && (count($this->original) < 2)) {
throw new \InvalidArgumentException('Translation with plurals need to have plural definition.');
}
$this->translation[$index] = (string) $translation;
return $this;
}
/**
* Many comments setter
* @author Pavel Železný <[email protected]>
* @param array $comments key mean type and value mean text of comment
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function setComments($comments) {
foreach ($comments as $type => $comment) {
$this->setComment($type, $comment);
}
return $this;
}
/**
* One comment setter
* @author Pavel Železný <[email protected]>
* @param string $type type of comment
* @param string $comment text of comment
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function setComment($type, $comment) {
if (in_array($type, $this->getAllowedCommentTypes()) === FALSE) {
throw new \InvalidArgumentException('Unsupported comment type. Supported type is one from following: ' . implode(', ', $this->getAllowedCommentTypes()) . '.');
}
$this->comments[$type] = $comment;
return $this;
}
/**
* Plural form of original untranslated string setter
* @author Pavel Železný <[email protected]>
* @param string $plural plural form of untranslated string
* @return \GettextTranslation provides a fluent interface
* @throws \InvalidArgumentException
*/
public function setPlural($plural) {
if (count($this->original) == 0) {
throw new \InvalidArgumentException('Plural form cannot be set without singular variant.');
} elseif (count($this->original) > 1) {
throw new \InvalidArgumentException('Unable to change plural form of original untranslated string. Make new translation instead.');
} elseif (is_string($plural) === FALSE) {
throw new \InvalidArgumentException('Plural have to be string.');
}
$this->original[1] = $plural;
return $this;
}
}