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