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 $key |
27
|
|
|
* @param $value |
28
|
|
|
* |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function setAttribute($key, $value) |
32
|
|
|
{ |
33
|
|
|
if (!$this->isTranslatableAttribute($key)) { |
34
|
|
|
return parent::setAttribute($key, $value); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (is_string($value) && !json_decode($value)) |
38
|
|
|
{ |
39
|
|
|
return $this->setTranslation($key, config('app.locale'), $value); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if(is_string($value) && json_decode($value)) { |
43
|
|
|
return $this->setTranslations($key, json_decode($value, true)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $key |
51
|
|
|
* @param string $locale |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function translate(string $key, string $locale = '') |
56
|
|
|
{ |
57
|
|
|
return $this->getTranslation($key, $locale); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/*** |
61
|
|
|
* @param string $key |
62
|
|
|
* @param string $locale |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function getTranslation(string $key, string $locale) |
67
|
|
|
{ |
68
|
|
|
$locale = $this->normalizeLocale($key, $locale); |
69
|
|
|
|
70
|
|
|
$translations = $this->getTranslations($key); |
71
|
|
|
|
72
|
|
|
$translation = $translations[$locale] ?? ''; |
73
|
|
|
|
74
|
|
|
if ($this->hasGetMutator($key)) { |
|
|
|
|
75
|
|
|
return $this->mutateAttribute($key, $translation); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $translation; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getTranslations($key) : array |
82
|
|
|
{ |
83
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
84
|
|
|
|
85
|
|
|
return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $key |
90
|
|
|
* @param string $locale |
91
|
|
|
* @param $value |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function setTranslation(string $key, string $locale, $value) |
96
|
|
|
{ |
97
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
98
|
|
|
|
99
|
|
|
$translations = $this->getTranslations($key); |
100
|
|
|
|
101
|
|
|
$oldValue = $translations[$locale] ?? ''; |
102
|
|
|
|
103
|
|
|
if ($this->hasSetMutator($key)) { |
|
|
|
|
104
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
105
|
|
|
$value = $this->{$method}($value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$translations[$locale] = $value; |
109
|
|
|
|
110
|
|
|
$this->attributes[$key] = $this->asJson($translations); |
|
|
|
|
111
|
|
|
|
112
|
|
|
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $key |
119
|
|
|
* @param array $translations |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function setTranslations(string $key, array $translations) |
124
|
|
|
{ |
125
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
126
|
|
|
|
127
|
|
|
foreach ($translations as $locale => $translation) { |
128
|
|
|
$this->setTranslation($key, $locale, $translation); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $key |
136
|
|
|
* @param string $locale |
137
|
|
|
* |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function forgetTranslation(string $key, string $locale) |
141
|
|
|
{ |
142
|
|
|
$translations = $this->getTranslations($key); |
143
|
|
|
|
144
|
|
|
unset($translations[$locale]); |
145
|
|
|
|
146
|
|
|
$this->setAttribute($key, $translations); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getTranslatedLocales(string $key) : array |
152
|
|
|
{ |
153
|
|
|
return array_keys($this->getTranslations($key)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function isTranslatableAttribute(string $key) : bool |
157
|
|
|
{ |
158
|
|
|
return in_array($key, $this->getTranslatableAttributes()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
protected function guardAgainstUntranslatableAttribute(string $key) |
162
|
|
|
{ |
163
|
|
|
if (!$this->isTranslatableAttribute($key)) { |
164
|
|
|
throw AttributeIsNotTranslatable::make($key, $this); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
protected function normalizeLocale(string $key, string $locale) : string |
169
|
|
|
{ |
170
|
|
|
if (in_array($locale, $this->getTranslatedLocales($key))) { |
171
|
|
|
return $locale; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (!is_null($fallbackLocale = config('laravel-translatable.fallback_locale'))) { |
175
|
|
|
return $fallbackLocale; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $locale; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getTranslatableAttributes() : array |
182
|
|
|
{ |
183
|
|
|
return is_array($this->translatable) |
|
|
|
|
184
|
|
|
? $this->translatable |
185
|
|
|
: []; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getCasts() : array |
189
|
|
|
{ |
190
|
|
|
return array_merge( |
191
|
|
|
parent::getCasts(), |
192
|
|
|
array_fill_keys($this->getTranslatableAttributes(), 'array') |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
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.