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