1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dimsav\Translatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
8
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
9
|
|
|
use Dimsav\Translatable\Exception\LocalesNotDefinedException; |
10
|
|
|
|
11
|
|
|
trait Translatable |
12
|
|
|
{ |
13
|
|
|
protected $defaultLocale; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Alias for getTranslation(). |
17
|
|
|
* |
18
|
|
|
* @param string|null $locale |
19
|
|
|
* @param bool $withFallback |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
22
|
|
|
*/ |
23
|
|
|
public function translate($locale = null, $withFallback = false) |
24
|
|
|
{ |
25
|
|
|
return $this->getTranslation($locale, $withFallback); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Alias for getTranslation(). |
30
|
|
|
* |
31
|
|
|
* @param string $locale |
32
|
|
|
* |
33
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
34
|
|
|
*/ |
35
|
|
|
public function translateOrDefault($locale) |
36
|
|
|
{ |
37
|
|
|
return $this->getTranslation($locale, true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Alias for getTranslationOrNew(). |
42
|
|
|
* |
43
|
|
|
* @param string $locale |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
46
|
|
|
*/ |
47
|
|
|
public function translateOrNew($locale) |
48
|
|
|
{ |
49
|
|
|
return $this->getTranslationOrNew($locale); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string|null $locale |
54
|
|
|
* @param bool $withFallback |
55
|
|
|
* |
56
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
57
|
|
|
*/ |
58
|
|
|
public function getTranslation($locale = null, $withFallback = null) |
59
|
|
|
{ |
60
|
|
|
$configFallbackLocale = $this->getFallbackLocale(); |
61
|
|
|
$locale = $locale ?: $this->locale(); |
62
|
|
|
$withFallback = $withFallback === null ? $this->useFallback() : $withFallback; |
63
|
|
|
$fallbackLocale = $this->getFallbackLocale($locale); |
64
|
|
|
|
65
|
|
|
if ($translation = $this->getTranslationByLocaleKey($locale)) { |
66
|
|
|
return $translation; |
67
|
|
|
} |
68
|
|
|
if ($withFallback && $fallbackLocale) { |
69
|
|
|
if ($translation = $this->getTranslationByLocaleKey($fallbackLocale)) { |
70
|
|
|
return $translation; |
71
|
|
|
} |
72
|
|
|
if ($fallbackLocale !== $configFallbackLocale && $translation = $this->getTranslationByLocaleKey($configFallbackLocale)) { |
73
|
|
|
return $translation; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string|null $locale |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function hasTranslation($locale = null) |
86
|
|
|
{ |
87
|
|
|
$locale = $locale ?: $this->locale(); |
88
|
|
|
|
89
|
|
|
foreach ($this->translations as $translation) { |
|
|
|
|
90
|
|
|
if ($translation->getAttribute($this->getLocaleKey()) == $locale) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getTranslationModelName() |
102
|
|
|
{ |
103
|
|
|
return $this->translationModel ?: $this->getTranslationModelNameDefault(); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getTranslationModelNameDefault() |
110
|
|
|
{ |
111
|
|
|
return get_class($this).config('translatable.translation_suffix', 'Translation'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getRelationKey() |
118
|
|
|
{ |
119
|
|
|
if ($this->translationForeignKey) { |
120
|
|
|
$key = $this->translationForeignKey; |
|
|
|
|
121
|
|
|
} elseif ($this->primaryKey !== 'id') { |
|
|
|
|
122
|
|
|
$key = $this->primaryKey; |
123
|
|
|
} else { |
124
|
|
|
$key = $this->getForeignKey(); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $key; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getLocaleKey() |
134
|
|
|
{ |
135
|
|
|
return $this->localeKey ?: config('translatable.locale_key', 'locale'); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
140
|
|
|
*/ |
141
|
|
|
public function translations() |
142
|
|
|
{ |
143
|
|
|
return $this->hasMany($this->getTranslationModelName(), $this->getRelationKey()); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
private function usePropertyFallback() |
150
|
|
|
{ |
151
|
|
|
return $this->useFallback() && config('translatable.use_property_fallback', false); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the attribute value from fallback translation if value of attribute |
156
|
|
|
* is empty and the property fallback is enabled in the configuration. |
157
|
|
|
* in model. |
158
|
|
|
* @param $locale |
159
|
|
|
* @param $attribute |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
|
|
private function getAttributeOrFallback($locale, $attribute) |
163
|
|
|
{ |
164
|
|
|
$value = $this->getTranslation($locale)->$attribute; |
165
|
|
|
|
166
|
|
|
if ( |
167
|
|
|
empty($value) && |
168
|
|
|
$this->usePropertyFallback() && |
169
|
|
|
($fallback = $this->getTranslation($this->getFallbackLocale(), true)) |
170
|
|
|
) { |
171
|
|
|
return $fallback->$attribute; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $key |
179
|
|
|
* |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
public function getAttribute($key) |
183
|
|
|
{ |
184
|
|
|
list($attribute, $locale) = $this->getAttributeAndLocale($key); |
185
|
|
|
|
186
|
|
|
if ($this->isTranslationAttribute($attribute)) { |
187
|
|
|
if ($this->getTranslation($locale) === null) { |
188
|
|
|
return $this->getAttributeValue($attribute); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// If the given $attribute has a mutator, we push it to $attributes and then call getAttributeValue |
192
|
|
|
// on it. This way, we can use Eloquent's checking for Mutation, type casting, and |
193
|
|
|
// Date fields. |
194
|
|
|
if ($this->hasGetMutator($attribute)) { |
|
|
|
|
195
|
|
|
$this->attributes[$attribute] = $this->getAttributeOrFallback($locale, $attribute); |
|
|
|
|
196
|
|
|
|
197
|
|
|
return $this->getAttributeValue($attribute); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->getAttributeOrFallback($locale, $attribute); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return parent::getAttribute($key); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $key |
208
|
|
|
* @param mixed $value |
209
|
|
|
* |
210
|
|
|
* @return $this |
211
|
|
|
*/ |
212
|
|
|
public function setAttribute($key, $value) |
213
|
|
|
{ |
214
|
|
|
list($attribute, $locale) = $this->getAttributeAndLocale($key); |
215
|
|
|
|
216
|
|
|
if ($this->isTranslationAttribute($attribute)) { |
217
|
|
|
$this->getTranslationOrNew($locale)->$attribute = $value; |
218
|
|
|
} else { |
219
|
|
|
return parent::setAttribute($key, $value); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param array $options |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
public function save(array $options = []) |
231
|
|
|
{ |
232
|
|
|
if ($this->exists) { |
|
|
|
|
233
|
|
|
if ($this->isDirty()) { |
|
|
|
|
234
|
|
|
// If $this->exists and dirty, parent::save() has to return true. If not, |
|
|
|
|
235
|
|
|
// an error has occurred. Therefore we shouldn't save the translations. |
236
|
|
|
if (parent::save($options)) { |
237
|
|
|
return $this->saveTranslations(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return false; |
241
|
|
|
} else { |
242
|
|
|
// If $this->exists and not dirty, parent::save() skips saving and returns |
|
|
|
|
243
|
|
|
// false. So we have to save the translations |
244
|
|
|
if ($this->fireModelEvent('saving') === false) { |
|
|
|
|
245
|
|
|
return false; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if ($saved = $this->saveTranslations()) { |
249
|
|
|
$this->fireModelEvent('saved', false); |
|
|
|
|
250
|
|
|
$this->fireModelEvent('updated', false); |
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $saved; |
254
|
|
|
} |
255
|
|
|
} elseif (parent::save($options)) { |
256
|
|
|
// We save the translations only if the instance is saved in the database. |
257
|
|
|
return $this->saveTranslations(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return false; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $locale |
265
|
|
|
* |
266
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
267
|
|
|
*/ |
268
|
|
|
protected function getTranslationOrNew($locale) |
269
|
|
|
{ |
270
|
|
|
if (($translation = $this->getTranslation($locale, false)) === null) { |
271
|
|
|
$translation = $this->getNewTranslation($locale); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
return $translation; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param array $attributes |
279
|
|
|
* |
280
|
|
|
* @throws \Illuminate\Database\Eloquent\MassAssignmentException |
281
|
|
|
* @return $this |
282
|
|
|
*/ |
283
|
|
|
public function fill(array $attributes) |
284
|
|
|
{ |
285
|
|
|
foreach ($attributes as $key => $values) { |
286
|
|
|
if ($this->isKeyALocale($key)) { |
287
|
|
|
$this->getTranslationOrNew($key)->fill($values); |
288
|
|
|
unset($attributes[$key]); |
289
|
|
|
} else { |
290
|
|
|
list($attribute, $locale) = $this->getAttributeAndLocale($key); |
291
|
|
|
if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) { |
|
|
|
|
292
|
|
|
$this->getTranslationOrNew($locale)->fill([$attribute => $values]); |
293
|
|
|
unset($attributes[$key]); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return parent::fill($attributes); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param string $key |
303
|
|
|
*/ |
304
|
|
|
private function getTranslationByLocaleKey($key) |
305
|
|
|
{ |
306
|
|
|
foreach ($this->translations as $translation) { |
307
|
|
|
if ($translation->getAttribute($this->getLocaleKey()) == $key) { |
308
|
|
|
return $translation; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return null; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @param null $locale |
317
|
|
|
* |
318
|
|
|
* @return string |
319
|
|
|
*/ |
320
|
|
|
private function getFallbackLocale($locale = null) |
321
|
|
|
{ |
322
|
|
|
if ($locale && $this->isLocaleCountryBased($locale)) { |
323
|
|
|
if ($fallback = $this->getLanguageFromCountryBasedLocale($locale)) { |
324
|
|
|
return $fallback; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return config('translatable.fallback_locale'); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param $locale |
333
|
|
|
* |
334
|
|
|
* @return bool |
335
|
|
|
*/ |
336
|
|
|
private function isLocaleCountryBased($locale) |
337
|
|
|
{ |
338
|
|
|
return strpos($locale, $this->getLocaleSeparator()) !== false; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* @param $locale |
343
|
|
|
* |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
private function getLanguageFromCountryBasedLocale($locale) |
347
|
|
|
{ |
348
|
|
|
$parts = explode($this->getLocaleSeparator(), $locale); |
349
|
|
|
|
350
|
|
|
return array_get($parts, 0); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @return bool|null |
355
|
|
|
*/ |
356
|
|
|
private function useFallback() |
357
|
|
|
{ |
358
|
|
|
if (isset($this->useTranslationFallback) && $this->useTranslationFallback !== null) { |
359
|
|
|
return $this->useTranslationFallback; |
|
|
|
|
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return config('translatable.use_fallback'); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param string $key |
367
|
|
|
* |
368
|
|
|
* @return bool |
369
|
|
|
*/ |
370
|
|
|
public function isTranslationAttribute($key) |
371
|
|
|
{ |
372
|
|
|
return in_array($key, $this->translatedAttributes); |
|
|
|
|
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @param string $key |
377
|
|
|
* |
378
|
|
|
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
379
|
|
|
* @return bool |
380
|
|
|
*/ |
381
|
|
|
protected function isKeyALocale($key) |
382
|
|
|
{ |
383
|
|
|
$locales = $this->getLocales(); |
384
|
|
|
|
385
|
|
|
return in_array($key, $locales); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException |
390
|
|
|
* @return array |
391
|
|
|
*/ |
392
|
|
|
protected function getLocales() |
393
|
|
|
{ |
394
|
|
|
$localesConfig = (array) config('translatable.locales'); |
395
|
|
|
|
396
|
|
|
if (empty($localesConfig)) { |
397
|
|
|
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '. |
398
|
|
|
' and that the locales configuration is defined.'); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
$locales = []; |
402
|
|
|
foreach ($localesConfig as $key => $locale) { |
403
|
|
|
if (is_array($locale)) { |
404
|
|
|
$locales[] = $key; |
405
|
|
|
foreach ($locale as $countryLocale) { |
406
|
|
|
$locales[] = $key.$this->getLocaleSeparator().$countryLocale; |
407
|
|
|
} |
408
|
|
|
} else { |
409
|
|
|
$locales[] = $locale; |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $locales; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @return string |
418
|
|
|
*/ |
419
|
|
|
protected function getLocaleSeparator() |
420
|
|
|
{ |
421
|
|
|
return config('translatable.locale_separator', '-'); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* @return bool |
426
|
|
|
*/ |
427
|
|
|
protected function saveTranslations() |
428
|
|
|
{ |
429
|
|
|
$saved = true; |
430
|
|
|
foreach ($this->translations as $translation) { |
431
|
|
|
if ($saved && $this->isTranslationDirty($translation)) { |
432
|
|
|
if (! empty($connectionName = $this->getConnectionName())) { |
|
|
|
|
433
|
|
|
$translation->setConnection($connectionName); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
$translation->setAttribute($this->getRelationKey(), $this->getKey()); |
|
|
|
|
437
|
|
|
$saved = $translation->save(); |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return $saved; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @param array |
446
|
|
|
* |
447
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
448
|
|
|
*/ |
449
|
|
|
public function replicateWithTranslations(array $except = null) |
450
|
|
|
{ |
451
|
|
|
$newInstance = parent::replicate($except); |
|
|
|
|
452
|
|
|
|
453
|
|
|
unset($newInstance->translations); |
454
|
|
|
foreach ($this->translations as $translation) { |
455
|
|
|
$newTranslation = $translation->replicate(); |
456
|
|
|
$newInstance->translations->add($newTranslation); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
return $newInstance; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @param \Illuminate\Database\Eloquent\Model $translation |
464
|
|
|
* |
465
|
|
|
* @return bool |
466
|
|
|
*/ |
467
|
|
|
protected function isTranslationDirty(Model $translation) |
468
|
|
|
{ |
469
|
|
|
$dirtyAttributes = $translation->getDirty(); |
470
|
|
|
unset($dirtyAttributes[$this->getLocaleKey()]); |
471
|
|
|
|
472
|
|
|
return count($dirtyAttributes) > 0; |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
/** |
476
|
|
|
* @param string $locale |
477
|
|
|
* |
478
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
479
|
|
|
*/ |
480
|
|
|
public function getNewTranslation($locale) |
481
|
|
|
{ |
482
|
|
|
$modelName = $this->getTranslationModelName(); |
483
|
|
|
$translation = new $modelName(); |
484
|
|
|
$translation->setAttribute($this->getLocaleKey(), $locale); |
485
|
|
|
$this->translations->add($translation); |
486
|
|
|
|
487
|
|
|
return $translation; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* @param $key |
492
|
|
|
* |
493
|
|
|
* @return bool |
494
|
|
|
*/ |
495
|
|
|
public function __isset($key) |
496
|
|
|
{ |
497
|
|
|
return $this->isTranslationAttribute($key) || parent::__isset($key); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
502
|
|
|
* @param string $locale |
503
|
|
|
* |
504
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
505
|
|
|
*/ |
506
|
|
View Code Duplication |
public function scopeTranslatedIn(Builder $query, $locale = null) |
|
|
|
|
507
|
|
|
{ |
508
|
|
|
$locale = $locale ?: $this->locale(); |
509
|
|
|
|
510
|
|
|
return $query->whereHas('translations', function (Builder $q) use ($locale) { |
511
|
|
|
$q->where($this->getLocaleKey(), '=', $locale); |
512
|
|
|
}); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
517
|
|
|
* @param string $locale |
518
|
|
|
* |
519
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
520
|
|
|
*/ |
521
|
|
View Code Duplication |
public function scopeNotTranslatedIn(Builder $query, $locale = null) |
|
|
|
|
522
|
|
|
{ |
523
|
|
|
$locale = $locale ?: $this->locale(); |
524
|
|
|
|
525
|
|
|
return $query->whereDoesntHave('translations', function (Builder $q) use ($locale) { |
526
|
|
|
$q->where($this->getLocaleKey(), '=', $locale); |
527
|
|
|
}); |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
532
|
|
|
* |
533
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
534
|
|
|
*/ |
535
|
|
|
public function scopeTranslated(Builder $query) |
536
|
|
|
{ |
537
|
|
|
return $query->has('translations'); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Adds scope to get a list of translated attributes, using the current locale. |
542
|
|
|
* Example usage: Country::listsTranslations('name')->get()->toArray() |
543
|
|
|
* Will return an array with items: |
544
|
|
|
* [ |
545
|
|
|
* 'id' => '1', // The id of country |
546
|
|
|
* 'name' => 'Griechenland' // The translated name |
547
|
|
|
* ]. |
548
|
|
|
* |
549
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
550
|
|
|
* @param string $translationField |
551
|
|
|
*/ |
552
|
|
|
public function scopeListsTranslations(Builder $query, $translationField) |
553
|
|
|
{ |
554
|
|
|
$withFallback = $this->useFallback(); |
555
|
|
|
$translationTable = $this->getTranslationsTable(); |
556
|
|
|
$localeKey = $this->getLocaleKey(); |
557
|
|
|
|
558
|
|
|
$query |
|
|
|
|
559
|
|
|
->select($this->getTable().'.'.$this->getKeyName(), $translationTable.'.'.$translationField) |
|
|
|
|
560
|
|
|
->leftJoin($translationTable, $translationTable.'.'.$this->getRelationKey(), '=', $this->getTable().'.'.$this->getKeyName()) |
|
|
|
|
561
|
|
|
->where($translationTable.'.'.$localeKey, $this->locale()); |
562
|
|
|
if ($withFallback) { |
563
|
|
|
$query->orWhere(function (Builder $q) use ($translationTable, $localeKey) { |
564
|
|
|
$q->where($translationTable.'.'.$localeKey, $this->getFallbackLocale()) |
565
|
|
|
->whereNotIn($translationTable.'.'.$this->getRelationKey(), function (QueryBuilder $q) use ( |
566
|
|
|
$translationTable, |
567
|
|
|
$localeKey |
568
|
|
|
) { |
569
|
|
|
$q->select($translationTable.'.'.$this->getRelationKey()) |
570
|
|
|
->from($translationTable) |
571
|
|
|
->where($translationTable.'.'.$localeKey, $this->locale()); |
572
|
|
|
}); |
573
|
|
|
}); |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* This scope eager loads the translations for the default and the fallback locale only. |
579
|
|
|
* We can use this as a shortcut to improve performance in our application. |
580
|
|
|
* |
581
|
|
|
* @param Builder $query |
582
|
|
|
*/ |
583
|
|
|
public function scopeWithTranslation(Builder $query) |
584
|
|
|
{ |
585
|
|
|
$query->with([ |
586
|
|
|
'translations' => function (Relation $query) { |
587
|
|
|
if ($this->useFallback()) { |
588
|
|
|
$locale = $this->locale(); |
589
|
|
|
$countryFallbackLocale = $this->getFallbackLocale($locale); // e.g. de-DE => de |
590
|
|
|
$locales = array_unique([$locale, $countryFallbackLocale, $this->getFallbackLocale()]); |
591
|
|
|
|
592
|
|
|
return $query->whereIn($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locales); |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
return $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale()); |
596
|
|
|
}, |
597
|
|
|
]); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* This scope filters results by checking the translation fields. |
602
|
|
|
* |
603
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
604
|
|
|
* @param string $key |
605
|
|
|
* @param string $value |
606
|
|
|
* @param string $locale |
607
|
|
|
* |
608
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
609
|
|
|
*/ |
610
|
|
View Code Duplication |
public function scopeWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
|
|
|
611
|
|
|
{ |
612
|
|
|
return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
613
|
|
|
$query->where($this->getTranslationsTable().'.'.$key, $value); |
614
|
|
|
if ($locale) { |
|
|
|
|
615
|
|
|
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale); |
616
|
|
|
} |
617
|
|
|
}); |
618
|
|
|
} |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* This scope filters results by checking the translation fields. |
622
|
|
|
* |
623
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
624
|
|
|
* @param string $key |
625
|
|
|
* @param string $value |
626
|
|
|
* @param string $locale |
627
|
|
|
* |
628
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
629
|
|
|
*/ |
630
|
|
View Code Duplication |
public function scopeOrWhereTranslation(Builder $query, $key, $value, $locale = null) |
|
|
|
|
631
|
|
|
{ |
632
|
|
|
return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
633
|
|
|
$query->where($this->getTranslationsTable().'.'.$key, $value); |
634
|
|
|
if ($locale) { |
|
|
|
|
635
|
|
|
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $locale); |
636
|
|
|
} |
637
|
|
|
}); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* This scope filters results by checking the translation fields. |
642
|
|
|
* |
643
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
644
|
|
|
* @param string $key |
645
|
|
|
* @param string $value |
646
|
|
|
* @param string $locale |
647
|
|
|
* |
648
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
649
|
|
|
*/ |
650
|
|
View Code Duplication |
public function scopeWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
|
|
|
651
|
|
|
{ |
652
|
|
|
return $query->whereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
653
|
|
|
$query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value); |
654
|
|
|
if ($locale) { |
|
|
|
|
655
|
|
|
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale); |
656
|
|
|
} |
657
|
|
|
}); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
/** |
661
|
|
|
* This scope filters results by checking the translation fields. |
662
|
|
|
* |
663
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
664
|
|
|
* @param string $key |
665
|
|
|
* @param string $value |
666
|
|
|
* @param string $locale |
667
|
|
|
* |
668
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
669
|
|
|
*/ |
670
|
|
View Code Duplication |
public function scopeOrWhereTranslationLike(Builder $query, $key, $value, $locale = null) |
|
|
|
|
671
|
|
|
{ |
672
|
|
|
return $query->orWhereHas('translations', function (Builder $query) use ($key, $value, $locale) { |
673
|
|
|
$query->where($this->getTranslationsTable().'.'.$key, 'LIKE', $value); |
674
|
|
|
if ($locale) { |
|
|
|
|
675
|
|
|
$query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), 'LIKE', $locale); |
676
|
|
|
} |
677
|
|
|
}); |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
/** |
681
|
|
|
* This scope sorts results by the given translation field. |
682
|
|
|
* |
683
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
684
|
|
|
* @param string $key |
685
|
|
|
* @param string $sortmethod |
686
|
|
|
* |
687
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
688
|
|
|
*/ |
689
|
|
|
public function scopeOrderByTranslation(Builder $query, $key, $sortmethod = 'asc') |
690
|
|
|
{ |
691
|
|
|
$translationTable = $this->getTranslationsTable(); |
692
|
|
|
$localeKey = $this->getLocaleKey(); |
693
|
|
|
$table = $this->getTable(); |
|
|
|
|
694
|
|
|
$keyName = $this->getKeyName(); |
|
|
|
|
695
|
|
|
|
696
|
|
|
return $query |
697
|
|
|
->join($translationTable, function (Builder $join) use ($translationTable, $localeKey, $table, $keyName) { |
698
|
|
|
$join |
699
|
|
|
->on($translationTable.'.'.$this->getRelationKey(), '=', $table.'.'.$keyName) |
700
|
|
|
->where($translationTable.'.'.$localeKey, $this->locale()); |
701
|
|
|
}) |
702
|
|
|
->orderBy($translationTable.'.'.$key, $sortmethod) |
703
|
|
|
->select($table.'.*') |
704
|
|
|
->with('translations'); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* @return array |
709
|
|
|
*/ |
710
|
|
|
public function attributesToArray() |
711
|
|
|
{ |
712
|
|
|
$attributes = parent::attributesToArray(); |
713
|
|
|
|
714
|
|
|
if (! $this->relationLoaded('translations') && ! $this->toArrayAlwaysLoadsTranslations()) { |
|
|
|
|
715
|
|
|
return $attributes; |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
$hiddenAttributes = $this->getHidden(); |
|
|
|
|
719
|
|
|
|
720
|
|
|
foreach ($this->translatedAttributes as $field) { |
|
|
|
|
721
|
|
|
if (in_array($field, $hiddenAttributes)) { |
722
|
|
|
continue; |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
if ($translations = $this->getTranslation()) { |
|
|
|
|
726
|
|
|
$attributes[$field] = $translations->$field; |
727
|
|
|
} |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
return $attributes; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* @return array |
735
|
|
|
*/ |
736
|
|
|
public function getTranslationsArray() |
737
|
|
|
{ |
738
|
|
|
$translations = []; |
739
|
|
|
|
740
|
|
|
foreach ($this->translations as $translation) { |
741
|
|
|
foreach ($this->translatedAttributes as $attr) { |
|
|
|
|
742
|
|
|
$translations[$translation->{$this->getLocaleKey()}][$attr] = $translation->{$attr}; |
743
|
|
|
} |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
return $translations; |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* @return string |
751
|
|
|
*/ |
752
|
|
|
private function getTranslationsTable() |
753
|
|
|
{ |
754
|
|
|
return app()->make($this->getTranslationModelName())->getTable(); |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* @return string |
759
|
|
|
*/ |
760
|
|
|
protected function locale() |
761
|
|
|
{ |
762
|
|
|
if ($this->defaultLocale) { |
763
|
|
|
return $this->defaultLocale; |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
return config('translatable.locale') |
767
|
|
|
?: app()->make('translator')->getLocale(); |
768
|
|
|
} |
769
|
|
|
|
770
|
|
|
/** |
771
|
|
|
* Set the default locale on the model. |
772
|
|
|
* |
773
|
|
|
* @param $locale |
774
|
|
|
* |
775
|
|
|
* @return $this |
776
|
|
|
*/ |
777
|
|
|
public function setDefaultLocale($locale) |
778
|
|
|
{ |
779
|
|
|
$this->defaultLocale = $locale; |
780
|
|
|
|
781
|
|
|
return $this; |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
/** |
785
|
|
|
* Get the default locale on the model. |
786
|
|
|
* |
787
|
|
|
* @return mixed |
788
|
|
|
*/ |
789
|
|
|
public function getDefaultLocale() |
790
|
|
|
{ |
791
|
|
|
return $this->defaultLocale; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* Deletes all translations for this model. |
796
|
|
|
* |
797
|
|
|
* @param string|array|null $locales The locales to be deleted (array or single string) |
798
|
|
|
* (e.g., ["en", "de"] would remove these translations). |
799
|
|
|
*/ |
800
|
|
|
public function deleteTranslations($locales = null) |
801
|
|
|
{ |
802
|
|
|
if ($locales === null) { |
803
|
|
|
$translations = $this->translations()->get(); |
804
|
|
|
} else { |
805
|
|
|
$locales = (array) $locales; |
806
|
|
|
$translations = $this->translations()->whereIn($this->getLocaleKey(), $locales)->get(); |
807
|
|
|
} |
808
|
|
|
foreach ($translations as $translation) { |
809
|
|
|
$translation->delete(); |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
// we need to manually "reload" the collection built from the relationship |
813
|
|
|
// otherwise $this->translations()->get() would NOT be the same as $this->translations |
|
|
|
|
814
|
|
|
$this->load('translations'); |
|
|
|
|
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
/** |
818
|
|
|
* @param $key |
819
|
|
|
* |
820
|
|
|
* @return array |
821
|
|
|
*/ |
822
|
|
|
private function getAttributeAndLocale($key) |
823
|
|
|
{ |
824
|
|
|
if (str_contains($key, ':')) { |
825
|
|
|
return explode(':', $key); |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
return [$key, $this->locale()]; |
829
|
|
|
} |
830
|
|
|
|
831
|
|
|
/** |
832
|
|
|
* @return bool |
833
|
|
|
*/ |
834
|
|
|
private function toArrayAlwaysLoadsTranslations() |
835
|
|
|
{ |
836
|
|
|
return config('translatable.to_array_always_loads_translations', true); |
837
|
|
|
} |
838
|
|
|
} |
839
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: