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