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