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