1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Translatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Config; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Spatie\Translatable\Events\TranslationHasBeenSet; |
8
|
|
|
use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable; |
9
|
|
|
|
10
|
|
|
trait HasTranslations |
11
|
|
|
{ |
12
|
|
|
public function getAttributeValue($key) |
13
|
|
|
{ |
14
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
15
|
|
|
return parent::getAttributeValue($key); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
return $this->getTranslation($key, $this->getLocale()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getRawOriginal($key = null, $default = null) |
22
|
|
|
{ |
23
|
|
|
if (is_null($key)) { |
24
|
|
|
return Arr::get($this->original, $key, $default); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (! $this->isTranslatableAttribute($key) ) { |
28
|
|
|
return Arr::get($this->original, $key, $default); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $this->getRawOriginalTranslation($key, $this->getLocale()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setAttribute($key, $value) |
35
|
|
|
{ |
36
|
|
|
// Pass arrays and untranslatable attributes to the parent method. |
37
|
|
|
if (! $this->isTranslatableAttribute($key) || is_array($value)) { |
38
|
|
|
return parent::setAttribute($key, $value); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// If the attribute is translatable and not already translated, set a |
42
|
|
|
// translation for the current app locale. |
43
|
|
|
return $this->setTranslation($key, $this->getLocale(), $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function translate(string $key, string $locale = '', bool $useFallbackLocale = true): string |
47
|
|
|
{ |
48
|
|
|
return $this->getTranslation($key, $locale, $useFallbackLocale); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getRawOriginalTranslation(string $key, string $locale, bool $useFallbackLocale = true){ |
52
|
|
|
|
53
|
|
|
$locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
54
|
|
|
|
55
|
|
|
$translations = $this->getTranslations($key); |
56
|
|
|
|
57
|
|
|
$translation = $translations[$locale] ?? ''; |
58
|
|
|
|
59
|
|
|
return $translation; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
63
|
|
|
{ |
64
|
|
|
$translation = $this->getRawOriginalTranslation($key, $locale, $useFallbackLocale); |
65
|
|
|
|
66
|
|
|
if ($this->hasGetMutator($key)) { |
67
|
|
|
return $this->mutateAttribute($key, $translation); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $translation; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getTranslationWithFallback(string $key, string $locale): string |
74
|
|
|
{ |
75
|
|
|
return $this->getTranslation($key, $locale, true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getTranslationWithoutFallback(string $key, string $locale) |
79
|
|
|
{ |
80
|
|
|
return $this->getTranslation($key, $locale, false); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getTranslations(string $key = null): array |
84
|
|
|
{ |
85
|
|
|
if ($key !== null) { |
86
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
87
|
|
|
|
88
|
|
|
return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) { |
89
|
|
|
return $value !== null && $value !== ''; |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return array_reduce($this->getTranslatableAttributes(), function ($result, $item) { |
94
|
|
|
$result[$item] = $this->getTranslations($item); |
95
|
|
|
|
96
|
|
|
return $result; |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setTranslation(string $key, string $locale, $value): self |
101
|
|
|
{ |
102
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
103
|
|
|
|
104
|
|
|
$translations = $this->getTranslations($key); |
105
|
|
|
|
106
|
|
|
$oldValue = $translations[$locale] ?? ''; |
107
|
|
|
|
108
|
|
|
if ($this->hasSetMutator($key)) { |
109
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
110
|
|
|
|
111
|
|
|
$this->{$method}($value, $locale); |
112
|
|
|
|
113
|
|
|
$value = $this->attributes[$key]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$translations[$locale] = $value; |
117
|
|
|
|
118
|
|
|
$this->attributes[$key] = $this->asJson($translations); |
119
|
|
|
|
120
|
|
|
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setTranslations(string $key, array $translations): self |
126
|
|
|
{ |
127
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
128
|
|
|
|
129
|
|
|
foreach ($translations as $locale => $translation) { |
130
|
|
|
$this->setTranslation($key, $locale, $translation); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function forgetTranslation(string $key, string $locale): self |
137
|
|
|
{ |
138
|
|
|
$translations = $this->getTranslations($key); |
139
|
|
|
|
140
|
|
|
unset( |
141
|
|
|
$translations[$locale], |
142
|
|
|
$this->$key |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$this->setTranslations($key, $translations); |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function forgetAllTranslations(string $locale): self |
151
|
|
|
{ |
152
|
|
|
collect($this->getTranslatableAttributes())->each(function (string $attribute) use ($locale) { |
153
|
|
|
$this->forgetTranslation($attribute, $locale); |
154
|
|
|
}); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getTranslatedLocales(string $key): array |
160
|
|
|
{ |
161
|
|
|
return array_keys($this->getTranslations($key)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function isTranslatableAttribute(string $key): bool |
165
|
|
|
{ |
166
|
|
|
return in_array($key, $this->getTranslatableAttributes()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function hasTranslation(string $key, string $locale = null): bool |
170
|
|
|
{ |
171
|
|
|
$locale = $locale ?: $this->getLocale(); |
172
|
|
|
|
173
|
|
|
return isset($this->getTranslations($key)[$locale]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function guardAgainstNonTranslatableAttribute(string $key) |
177
|
|
|
{ |
178
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
179
|
|
|
throw AttributeIsNotTranslatable::make($key, $this); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale): string |
184
|
|
|
{ |
185
|
|
|
if (in_array($locale, $this->getTranslatedLocales($key))) { |
186
|
|
|
return $locale; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (! $useFallbackLocale) { |
190
|
|
|
return $locale; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if (! is_null($fallbackLocale = config('translatable.fallback_locale'))) { |
194
|
|
|
return $fallbackLocale; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if (! is_null($fallbackLocale = config('app.fallback_locale'))) { |
198
|
|
|
return $fallbackLocale; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $locale; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function getLocale(): string |
205
|
|
|
{ |
206
|
|
|
return config('app.locale'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function getTranslatableAttributes(): array |
210
|
|
|
{ |
211
|
|
|
return is_array($this->translatable) |
212
|
|
|
? $this->translatable |
213
|
|
|
: []; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function getTranslationsAttribute(): array |
217
|
|
|
{ |
218
|
|
|
return collect($this->getTranslatableAttributes()) |
219
|
|
|
->mapWithKeys(function (string $key) { |
220
|
|
|
return [$key => $this->getTranslations($key)]; |
221
|
|
|
}) |
222
|
|
|
->toArray(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function getCasts(): array |
226
|
|
|
{ |
227
|
|
|
return array_merge( |
228
|
|
|
parent::getCasts(), |
229
|
|
|
array_fill_keys($this->getTranslatableAttributes(), 'array') |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
<?php |
|
|
|
|
234
|
|
|
|
235
|
|
|
namespace Spatie\Translatable; |
236
|
|
|
|
237
|
|
|
use Illuminate\Support\Facades\Config; |
238
|
|
|
use Illuminate\Support\Str; |
239
|
|
|
use Spatie\Translatable\Events\TranslationHasBeenSet; |
240
|
|
|
use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable; |
241
|
|
|
|
242
|
|
|
trait HasTranslations |
243
|
|
|
{ |
244
|
|
|
public function getAttributeValue($key) |
245
|
|
|
{ |
246
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
247
|
|
|
return parent::getAttributeValue($key); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this->getTranslation($key, $this->getLocale()); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function getRawOriginal($key = null, $default = null) |
254
|
|
|
{ |
255
|
|
|
if (is_null($key)) { |
256
|
|
|
return Arr::get($this->original, $key, $default); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if (! $this->isTranslatableAttribute($key) ) { |
260
|
|
|
return Arr::get($this->original, $key, $default); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $this->getRawOriginalTranslation($key, $this->getLocale()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function setAttribute($key, $value) |
267
|
|
|
{ |
268
|
|
|
// Pass arrays and untranslatable attributes to the parent method. |
269
|
|
|
if (! $this->isTranslatableAttribute($key) || is_array($value)) { |
270
|
|
|
return parent::setAttribute($key, $value); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
// If the attribute is translatable and not already translated, set a |
274
|
|
|
// translation for the current app locale. |
275
|
|
|
return $this->setTranslation($key, $this->getLocale(), $value); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function translate(string $key, string $locale = '', bool $useFallbackLocale = true): string |
279
|
|
|
{ |
280
|
|
|
return $this->getTranslation($key, $locale, $useFallbackLocale); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function getRawOriginalTranslation(string $key, string $locale, bool $useFallbackLocale = true){ |
284
|
|
|
|
285
|
|
|
$locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
286
|
|
|
|
287
|
|
|
$translations = $this->getTranslations($key); |
288
|
|
|
|
289
|
|
|
$translation = $translations[$locale] ?? ''; |
290
|
|
|
|
291
|
|
|
return $translation; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
295
|
|
|
{ |
296
|
|
|
$translation = $this->getRawOriginalTranslation($key, $locale, $useFallbackLocale); |
297
|
|
|
|
298
|
|
|
if ($this->hasGetMutator($key)) { |
299
|
|
|
return $this->mutateAttribute($key, $translation); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return $translation; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function getTranslationWithFallback(string $key, string $locale): string |
306
|
|
|
{ |
307
|
|
|
return $this->getTranslation($key, $locale, true); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function getTranslationWithoutFallback(string $key, string $locale) |
311
|
|
|
{ |
312
|
|
|
return $this->getTranslation($key, $locale, false); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getTranslations(string $key = null): array |
316
|
|
|
{ |
317
|
|
|
if ($key !== null) { |
318
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
319
|
|
|
|
320
|
|
|
return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) { |
321
|
|
|
return $value !== null && $value !== ''; |
322
|
|
|
}); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return array_reduce($this->getTranslatableAttributes(), function ($result, $item) { |
326
|
|
|
$result[$item] = $this->getTranslations($item); |
327
|
|
|
|
328
|
|
|
return $result; |
329
|
|
|
}); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public function setTranslation(string $key, string $locale, $value): self |
333
|
|
|
{ |
334
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
335
|
|
|
|
336
|
|
|
$translations = $this->getTranslations($key); |
337
|
|
|
|
338
|
|
|
$oldValue = $translations[$locale] ?? ''; |
339
|
|
|
|
340
|
|
|
if ($this->hasSetMutator($key)) { |
341
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
342
|
|
|
|
343
|
|
|
$this->{$method}($value, $locale); |
344
|
|
|
|
345
|
|
|
$value = $this->attributes[$key]; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$translations[$locale] = $value; |
349
|
|
|
|
350
|
|
|
$this->attributes[$key] = $this->asJson($translations); |
351
|
|
|
|
352
|
|
|
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
353
|
|
|
|
354
|
|
|
return $this; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
public function setTranslations(string $key, array $translations): self |
358
|
|
|
{ |
359
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
360
|
|
|
|
361
|
|
|
foreach ($translations as $locale => $translation) { |
362
|
|
|
$this->setTranslation($key, $locale, $translation); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function forgetTranslation(string $key, string $locale): self |
369
|
|
|
{ |
370
|
|
|
$translations = $this->getTranslations($key); |
371
|
|
|
|
372
|
|
|
unset( |
373
|
|
|
$translations[$locale], |
374
|
|
|
$this->$key |
375
|
|
|
); |
376
|
|
|
|
377
|
|
|
$this->setTranslations($key, $translations); |
378
|
|
|
|
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function forgetAllTranslations(string $locale): self |
383
|
|
|
{ |
384
|
|
|
collect($this->getTranslatableAttributes())->each(function (string $attribute) use ($locale) { |
385
|
|
|
$this->forgetTranslation($attribute, $locale); |
386
|
|
|
}); |
387
|
|
|
|
388
|
|
|
return $this; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
public function getTranslatedLocales(string $key): array |
392
|
|
|
{ |
393
|
|
|
return array_keys($this->getTranslations($key)); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
public function isTranslatableAttribute(string $key): bool |
397
|
|
|
{ |
398
|
|
|
return in_array($key, $this->getTranslatableAttributes()); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
public function hasTranslation(string $key, string $locale = null): bool |
402
|
|
|
{ |
403
|
|
|
$locale = $locale ?: $this->getLocale(); |
404
|
|
|
|
405
|
|
|
return isset($this->getTranslations($key)[$locale]); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
protected function guardAgainstNonTranslatableAttribute(string $key) |
409
|
|
|
{ |
410
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
411
|
|
|
throw AttributeIsNotTranslatable::make($key, $this); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale): string |
416
|
|
|
{ |
417
|
|
|
if (in_array($locale, $this->getTranslatedLocales($key))) { |
418
|
|
|
return $locale; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
if (! $useFallbackLocale) { |
422
|
|
|
return $locale; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
if (! is_null($fallbackLocale = config('translatable.fallback_locale'))) { |
426
|
|
|
return $fallbackLocale; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
if (! is_null($fallbackLocale = config('app.fallback_locale'))) { |
430
|
|
|
return $fallbackLocale; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $locale; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
protected function getLocale(): string |
437
|
|
|
{ |
438
|
|
|
return config('app.locale'); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
public function getTranslatableAttributes(): array |
442
|
|
|
{ |
443
|
|
|
return is_array($this->translatable) |
444
|
|
|
? $this->translatable |
445
|
|
|
: []; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
public function getTranslationsAttribute(): array |
449
|
|
|
{ |
450
|
|
|
return collect($this->getTranslatableAttributes()) |
451
|
|
|
->mapWithKeys(function (string $key) { |
452
|
|
|
return [$key => $this->getTranslations($key)]; |
453
|
|
|
}) |
454
|
|
|
->toArray(); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
public function getCasts(): array |
458
|
|
|
{ |
459
|
|
|
return array_merge( |
460
|
|
|
parent::getCasts(), |
461
|
|
|
array_fill_keys($this->getTranslatableAttributes(), 'array') |
462
|
|
|
); |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
<?php |
466
|
|
|
|
467
|
|
|
namespace Spatie\Translatable; |
468
|
|
|
|
469
|
|
|
use Illuminate\Support\Facades\Config; |
470
|
|
|
use Illuminate\Support\Str; |
471
|
|
|
use Spatie\Translatable\Events\TranslationHasBeenSet; |
472
|
|
|
use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable; |
473
|
|
|
|
474
|
|
|
trait HasTranslations |
475
|
|
|
{ |
476
|
|
|
public function getAttributeValue($key) |
477
|
|
|
{ |
478
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
479
|
|
|
return parent::getAttributeValue($key); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
return $this->getTranslation($key, $this->getLocale()); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
public function getRawOriginal($key = null, $default = null) |
486
|
|
|
{ |
487
|
|
|
if (is_null($key)) { |
488
|
|
|
return Arr::get($this->original, $key, $default); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
if (! $this->isTranslatableAttribute($key) ) { |
492
|
|
|
return Arr::get($this->original, $key, $default); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
return $this->getRawOriginalTranslation($key, $this->getLocale()); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
public function setAttribute($key, $value) |
499
|
|
|
{ |
500
|
|
|
// Pass arrays and untranslatable attributes to the parent method. |
501
|
|
|
if (! $this->isTranslatableAttribute($key) || is_array($value)) { |
502
|
|
|
return parent::setAttribute($key, $value); |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
// If the attribute is translatable and not already translated, set a |
506
|
|
|
// translation for the current app locale. |
507
|
|
|
return $this->setTranslation($key, $this->getLocale(), $value); |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
public function translate(string $key, string $locale = '', bool $useFallbackLocale = true): string |
511
|
|
|
{ |
512
|
|
|
return $this->getTranslation($key, $locale, $useFallbackLocale); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
public function getRawOriginalTranslation(string $key, string $locale, bool $useFallbackLocale = true){ |
516
|
|
|
|
517
|
|
|
$locale = $this->normalizeLocale($key, $locale, $useFallbackLocale); |
518
|
|
|
|
519
|
|
|
$translations = $this->getTranslations($key); |
520
|
|
|
|
521
|
|
|
$translation = $translations[$locale] ?? ''; |
522
|
|
|
|
523
|
|
|
return $translation; |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
527
|
|
|
{ |
528
|
|
|
$translation = $this->getRawOriginalTranslation($key, $locale, $useFallbackLocale); |
529
|
|
|
|
530
|
|
|
if ($this->hasGetMutator($key)) { |
531
|
|
|
return $this->mutateAttribute($key, $translation); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
return $translation; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
public function getTranslationWithFallback(string $key, string $locale): string |
538
|
|
|
{ |
539
|
|
|
return $this->getTranslation($key, $locale, true); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
public function getTranslationWithoutFallback(string $key, string $locale) |
543
|
|
|
{ |
544
|
|
|
return $this->getTranslation($key, $locale, false); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
public function getTranslations(string $key = null): array |
548
|
|
|
{ |
549
|
|
|
if ($key !== null) { |
550
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
551
|
|
|
|
552
|
|
|
return array_filter(json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: [], function ($value) { |
553
|
|
|
return $value !== null && $value !== ''; |
554
|
|
|
}); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
return array_reduce($this->getTranslatableAttributes(), function ($result, $item) { |
558
|
|
|
$result[$item] = $this->getTranslations($item); |
559
|
|
|
|
560
|
|
|
return $result; |
561
|
|
|
}); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
public function setTranslation(string $key, string $locale, $value): self |
565
|
|
|
{ |
566
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
567
|
|
|
|
568
|
|
|
$translations = $this->getTranslations($key); |
569
|
|
|
|
570
|
|
|
$oldValue = $translations[$locale] ?? ''; |
571
|
|
|
|
572
|
|
|
if ($this->hasSetMutator($key)) { |
573
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
574
|
|
|
|
575
|
|
|
$this->{$method}($value, $locale); |
576
|
|
|
|
577
|
|
|
$value = $this->attributes[$key]; |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
$translations[$locale] = $value; |
581
|
|
|
|
582
|
|
|
$this->attributes[$key] = $this->asJson($translations); |
583
|
|
|
|
584
|
|
|
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
585
|
|
|
|
586
|
|
|
return $this; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
public function setTranslations(string $key, array $translations): self |
590
|
|
|
{ |
591
|
|
|
$this->guardAgainstNonTranslatableAttribute($key); |
592
|
|
|
|
593
|
|
|
foreach ($translations as $locale => $translation) { |
594
|
|
|
$this->setTranslation($key, $locale, $translation); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
return $this; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
public function forgetTranslation(string $key, string $locale): self |
601
|
|
|
{ |
602
|
|
|
$translations = $this->getTranslations($key); |
603
|
|
|
|
604
|
|
|
unset( |
605
|
|
|
$translations[$locale], |
606
|
|
|
$this->$key |
607
|
|
|
); |
608
|
|
|
|
609
|
|
|
$this->setTranslations($key, $translations); |
610
|
|
|
|
611
|
|
|
return $this; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
public function forgetAllTranslations(string $locale): self |
615
|
|
|
{ |
616
|
|
|
collect($this->getTranslatableAttributes())->each(function (string $attribute) use ($locale) { |
617
|
|
|
$this->forgetTranslation($attribute, $locale); |
618
|
|
|
}); |
619
|
|
|
|
620
|
|
|
return $this; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
public function getTranslatedLocales(string $key): array |
624
|
|
|
{ |
625
|
|
|
return array_keys($this->getTranslations($key)); |
626
|
|
|
} |
627
|
|
|
|
628
|
|
|
public function isTranslatableAttribute(string $key): bool |
629
|
|
|
{ |
630
|
|
|
return in_array($key, $this->getTranslatableAttributes()); |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
public function hasTranslation(string $key, string $locale = null): bool |
634
|
|
|
{ |
635
|
|
|
$locale = $locale ?: $this->getLocale(); |
636
|
|
|
|
637
|
|
|
return isset($this->getTranslations($key)[$locale]); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
protected function guardAgainstNonTranslatableAttribute(string $key) |
641
|
|
|
{ |
642
|
|
|
if (! $this->isTranslatableAttribute($key)) { |
643
|
|
|
throw AttributeIsNotTranslatable::make($key, $this); |
644
|
|
|
} |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale): string |
648
|
|
|
{ |
649
|
|
|
if (in_array($locale, $this->getTranslatedLocales($key))) { |
650
|
|
|
return $locale; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
if (! $useFallbackLocale) { |
654
|
|
|
return $locale; |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
if (! is_null($fallbackLocale = config('translatable.fallback_locale'))) { |
658
|
|
|
return $fallbackLocale; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
if (! is_null($fallbackLocale = config('app.fallback_locale'))) { |
662
|
|
|
return $fallbackLocale; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
return $locale; |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
protected function getLocale(): string |
669
|
|
|
{ |
670
|
|
|
return config('app.locale'); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
public function getTranslatableAttributes(): array |
674
|
|
|
{ |
675
|
|
|
return is_array($this->translatable) |
676
|
|
|
? $this->translatable |
677
|
|
|
: []; |
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
public function getTranslationsAttribute(): array |
681
|
|
|
{ |
682
|
|
|
return collect($this->getTranslatableAttributes()) |
683
|
|
|
->mapWithKeys(function (string $key) { |
684
|
|
|
return [$key => $this->getTranslations($key)]; |
685
|
|
|
}) |
686
|
|
|
->toArray(); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
public function getCasts(): array |
690
|
|
|
{ |
691
|
|
|
return array_merge( |
692
|
|
|
parent::getCasts(), |
693
|
|
|
array_fill_keys($this->getTranslatableAttributes(), 'array') |
694
|
|
|
); |
695
|
|
|
} |
696
|
|
|
} |
697
|
|
|
|