Newer
Older
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
<?php
/**
* I18NString class
*/
class I18NString
{
/**
* Text in default content language.
*
* @var string
*/
protected $base;
/**
* Text in additional languages.
*
* @var array|null
*/
protected $lang;
/**
* Database info for id, table, field.
*
* @var array
*/
protected $metadata;
/**
* Holds the language the content is translated into.
*
* @var string
*/
protected static $content_language = null;
/**
* Holds the language the content is translated into by default.
*
* @var string
*/
protected static $default_language = null;
/**
* Initialize a new I18NString instance.
*
* @param string $base Text in default content language.
* @param array $lang Text in additional languages.
* @param array $metadata Database info for id, table, field.
*/
public function __construct($base, $lang = null, $metadata = [])
{
$this->base = $base;
$this->lang = $lang;
Jan-Hendrik Willms
committed
$this->setMetadata($metadata);
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
}
/**
* Return the text representation of this i18n field in selected language.
* The language is selected by self::content_language (with precendence)
* or by $_SESSION['_language'].
*
* @returns string
*/
public function __toString()
{
if (self::$content_language) {
return $this->localized(self::$content_language) ?? (string) $this->base;
} else {
if (isset($_SESSION['_language'])
&& $_SESSION['_language'] != self::getDefaultLanguage()
&& $this->translation($_SESSION['_language'])) {
return $this->translation($_SESSION['_language']);
}
}
return (string) $this->base;
}
/**
* Sets the language the content is translated into.
*
* @param string $language
*/
public static function setContentLanguage($language)
{
self::$content_language = $language;
}
/**
* Returns the language the contnet is translated into.
*
* @return string The language the content is translated into.
*/
public static function getContentLanguage()
{
return self::$content_language ?: self::getDefaultLanguage();
}
/**
* Sets the default language the content is translated into. The default is
* normally defined by the first entry in $GLOBALS['CONTENT_LANGUAGES'] (see
* config_defaults.inc.php).
*
* @param string $language
*/
public static function setDefaultLanguage($language = null)
{
self::$default_language = $language ?: key($GLOBALS['CONTENT_LANGUAGES']);
}
/**
* Returns the language all values are translated into by default. The
* language ist normally defined in $GLOBALS['CONTENT_LANGUAGES'] (see
* config_defaults.inc.php).
*
* @return string The default language all values are translated into.
*/
public static function getDefaultLanguage()
{
return self::$default_language ?: key($GLOBALS['CONTENT_LANGUAGES']);
}
/**
* Sets the original (untranslated) value of this i18n field.
*
* @param string $text The original value.
* @return string The original value.
*/
public function setOriginal($text)
{
return $this->base = $text;
}
/**
* Sets all translations of this i18n field.
*
* @param array $lang An array with languages as keys and translations
* as values.
* @return array The array with translations.
*/
public function setTranslations($lang)
{
return $this->lang = $lang;
}
/**
* Sets the metadata (database info for id, table, field) of this i18n field.
*
* @param array $metadata Database info for id, table, field.
*/
public function setMetadata($metadata)
{
Jan-Hendrik Willms
committed
if (isset($metadata['object_id']) && (is_array($metadata['object_id']) || is_object($metadata['object_id']))) {
throw new Exception('Can not use array or object as object id');
}
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
$this->metadata = $metadata;
}
/**
* Return the string in the default content language.
*
* @return string String in default content language.
*/
public function original()
{
return $this->base;
}
/**
* Return the string in the specified additional language.
*
* @param string The additional language.
* @return string The translated value.
*/
public function translation($lang)
{
return $this->toArray()[$lang];
}
/**
* Returns the string in the specified language (additional languages and
* default languages).
*
* @param string $lang Additional language or default language.
* @return string The localized string.
*/
public function localized($lang)
{
if ($lang == self::getDefaultLanguage()) {
return $this->base;
}
return $this->translation($lang);
}
/**
* Sets the translation for the given language. If the given language is
* the default language, sets the original.
*
* @param type $text The translated or original value.
* @param type $lang The additional or default language.
* @return string The translated or original value.
* @throws InvalidArgumentException
*/
public function setLocalized($text, $lang)
{
if ($lang == self::getDefaultLanguage()) {
return $this->setOriginal($text);
}
if (!Config::get()->CONTENT_LANGUAGES[$lang]) {
throw new InvalidArgumentException('Language not configured.');
}
return $this->lang[$lang] = $text;
}
/**
* Return an array containing the text in all additional languages.
*
* @return array The array with translations.
*/
public function toArray()
{
if (is_null($this->lang)) {
$object_id = $this->metadata['object_id'];
$table = $this->metadata['table'];
$field = $this->metadata['field'];
if (!$table || !$field) {
throw new RuntimeException('fetching translations not possible, metadata is missing');
}
$this->lang = self::fetchDataForField($object_id, $table, $field);
}
return $this->lang;
}
/**
* Trim all language strings
*
* @param string $symbols All symbols to trim.
* @return I18NString Returns this.
*/
public function trim($symbols = " \t\n\r\0\x0B")
{
foreach ($this->lang as &$lang) {
$lang = trim($lang, $symbols);
}
$this->base = trim($this->base, $symbols);
return $this;
}
/**
* Stores the i18n String manually in the database
*
*/
public function storeTranslations()
{
if (is_array($this->lang)) {
$db = DBManager::get();
$object_id = $this->metadata['object_id'];
$table = $this->metadata['table'];
$field = $this->metadata['field'];
if (!$object_id || !$table || !$field) {
throw new RuntimeException('store not possible, metadata is missing');
}
/* Replace translations */
$deleted = $db->execute("DELETE FROM i18n WHERE object_id = ? AND `table` = ? AND field = ?", [$object_id, $table, $field]);
$i18nSQL = $db->prepare("INSERT INTO `i18n` (`object_id`, `table`, `field`, `lang`, `value`) VALUES (?,?,?,?,?)");
foreach ($this->lang as $lang => $value) {
if (mb_strlen($value)) {
$i18nSQL->execute([$object_id, $table, $field, $lang, (string) $value]);
}
}
}
}
/**
* Removes all translations for this I18NString object.
*
*/
public function removeTranslations()
{
$this->lang = [];
$this->storeTranslations();
}
/**
* Returns an I18NString object by given object_id, table and field.
*
* @param string $object_id The id of the object with i18n fields.
* @param string $table The name of the table with the original values.
* @param string $field The name of the i18n field.
* @param string $base Sets the original value or retrieve it from database
* if null.
* @return I18NString The I18NString object.
*/
public static function load($object_id, $table, $field, $base = null)
{
$db = DBManager::get();
if (is_null($base)) {
// Find pk
SimpleORMap::tableScheme($table);
if (count(SimpleORMap::$schemes[$table]['pk']) > 1) {
throw new RuntimeException(sprintf('table %s has multiple primary key, not implemented yet', $table));
} else {
$pk = SimpleORMap::$schemes[$table]['pk'][0];
}
$base = $db->fetchColumn("SELECT `$field` FROM `$table` WHERE `$pk` = ?", [$object_id]);
}
return new self($base, self::fetchDataForField($object_id, $table, $field), compact('object_id', 'table', 'field'));
}
/**
* Retrieves all translations of one field.
*
* @param string $object_id The id of the object with i18n fields.
* @param string $table The name of the table with the original values.
* @param string $field The name of the i18n field.
* @return array An array with language as key and translation as value.
*/
public static function fetchDataForField($object_id, $table, $field)
{
$db = DBManager::get();
$values = $db->fetchPairs("SELECT `lang`, `value` FROM `i18n` WHERE `object_id` = ? AND `table` = ? AND `field` = ?", [$object_id, $table, $field]);
$data = [];
foreach (array_keys(Config::get()->CONTENT_LANGUAGES) as $lang) {
if ($lang != self::getDefaultLanguage()) {
$data[$lang] = mb_strlen($values[$lang]) ? $values[$lang] : null;
}
}
return $data;
}
/**
* Retrieves all translations of all fields for given object (by id) and
* table.
*
* @param string $object_id The id of the object with i18n fields.
* @param string $table The name of the table with the original values.
* @return array An array with all translations of all fields grouped by
* field.
*/
public static function fetchDataForRow($object_id, $table)
{
$db = DBManager::get();
return $db->fetchGrouped("SELECT `field`, `lang`, `value` FROM `i18n` WHERE `object_id` = ? AND `table` = ?", [$object_id, $table]);
}
/**
* Removes all translations by given object id and table name. Accepts the
* language as third parameter to remove only translations to this language.
*
* @param string $object_id The id of the sorm object.
* @param string $table The table name.
* @param string $lang Optional name of language.
* @return int The number of deleted translations.
*/
public static function removeAllTranslations($object_id, $table, $lang = null)
{
$db = DBManager::get();
if ($lang) {
return $db->execute('DELETE FROM `i18n` '
. 'WHERE `object_id` = ? AND `table` = ? AND `lang` = ?',
[$object_id, $table, $lang]);
}
return $db->execute('DELETE FROM `i18n` '
. 'WHERE `object_id` = ? AND `table` = ?',
[$object_id, $table]);
}
}