1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Translatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Spatie\Translatable\Events\TranslationHasBeenSet; |
7
|
|
|
use Spatie\Translatable\Exceptions\AttributeIsNotTranslatable; |
8
|
|
|
|
9
|
|
|
trait HasTranslations |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param string $key |
13
|
|
|
* |
14
|
|
|
* @return mixed |
15
|
|
|
*/ |
16
|
|
|
public function getAttributeValue($key) |
17
|
|
|
{ |
18
|
|
|
if (!$this->isTranslatableAttribute($key)) { |
19
|
|
|
return parent::getAttributeValue($key); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return $this->getTranslation($key, config('app.locale')); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $key |
27
|
|
|
* @param string $locale |
28
|
|
|
* |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
public function translate(string $key, string $locale = '') |
32
|
|
|
{ |
33
|
|
|
return $this->getTranslation($key, $locale); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/*** |
37
|
|
|
* @param string $key |
38
|
|
|
* @param string $locale |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
public function getTranslation(string $key, string $locale) |
43
|
|
|
{ |
44
|
|
|
$translations = $this->getTranslations($key); |
45
|
|
|
|
46
|
|
|
$translation = $translations[$locale] ?? ''; |
47
|
|
|
|
48
|
|
|
if ($this->hasGetMutator($key)) { |
|
|
|
|
49
|
|
|
return $this->mutateAttribute($key, $translation); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $translation; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getTranslations($key) : array |
56
|
|
|
{ |
57
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
58
|
|
|
|
59
|
|
|
return json_decode($this->getAttributes()[$key] ?? '{}', true); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setTranslation(string $key, string $locale, $value) |
63
|
|
|
{ |
64
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
65
|
|
|
|
66
|
|
|
$translations = $this->getTranslations($key); |
67
|
|
|
|
68
|
|
|
$oldValue = $translations[$locale] ?? ''; |
69
|
|
|
|
70
|
|
|
if ($this->hasSetMutator($key)) { |
|
|
|
|
71
|
|
|
$method = 'set' . Str::studly($key) . 'Attribute'; |
72
|
|
|
$value = $this->{$method}($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$translations[$locale] = $value; |
76
|
|
|
|
77
|
|
|
$this->attributes[$key] = $this->asJson($translations); |
|
|
|
|
78
|
|
|
|
79
|
|
|
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setTranslations(string $key, array $translations) |
85
|
|
|
{ |
86
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
87
|
|
|
|
88
|
|
|
foreach ($translations as $locale => $translation) { |
89
|
|
|
$this->setTranslation($key, $locale, $translation); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function forgetTranslation(string $key, string $locale) |
96
|
|
|
{ |
97
|
|
|
$translations = $this->getTranslations($key); |
98
|
|
|
|
99
|
|
|
unset($translations[$locale]); |
100
|
|
|
|
101
|
|
|
$this->setAttribute($key, $translations); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getTranslatedLocales(string $key) : array |
107
|
|
|
{ |
108
|
|
|
return array_keys($this->getTranslations($key)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function isTranslatableAttribute(string $key) : bool |
112
|
|
|
{ |
113
|
|
|
return in_array($key, $this->getTranslatableAttributes()); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function guardAgainstUntranslatableAttribute(string $key) |
117
|
|
|
{ |
118
|
|
|
if (!$this->isTranslatableAttribute($key)) { |
119
|
|
|
throw AttributeIsNotTranslatable::make($key, $this); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getCasts() |
124
|
|
|
{ |
125
|
|
|
return array_merge( |
126
|
|
|
parent::getCasts(), |
127
|
|
|
array_fill_keys($this->getTranslatableAttributes(), 'array') |
|
|
|
|
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.