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