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