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, bool $withFallback = true) |
43
|
|
|
{ |
44
|
|
|
$locale = $this->normalizeLocale($key, $locale, $withFallback); |
45
|
|
|
|
46
|
|
|
$translations = $this->getTranslations($key); |
47
|
|
|
|
48
|
|
|
$translation = $translations[$locale] ?? ''; |
49
|
|
|
|
50
|
|
|
if ($this->hasGetMutator($key)) { |
|
|
|
|
51
|
|
|
return $this->mutateAttribute($key, $translation); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $translation; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getTranslationWithFallback(string $key, string $locale) |
58
|
|
|
{ |
59
|
|
|
return $this->getTranslation($key, $locale, true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getTranslationWithoutFallback(string $key, string $locale) |
63
|
|
|
{ |
64
|
|
|
return $this->getTranslation($key, $locale, false); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getTranslations($key) : array |
68
|
|
|
{ |
69
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
70
|
|
|
|
71
|
|
|
return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $key |
76
|
|
|
* @param string $locale |
77
|
|
|
* @param $value |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setTranslation(string $key, string $locale, $value) |
82
|
|
|
{ |
83
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
84
|
|
|
|
85
|
|
|
$translations = $this->getTranslations($key); |
86
|
|
|
|
87
|
|
|
$oldValue = $translations[$locale] ?? ''; |
88
|
|
|
|
89
|
|
|
if ($this->hasSetMutator($key)) { |
|
|
|
|
90
|
|
|
$method = 'set'.Str::studly($key).'Attribute'; |
91
|
|
|
$value = $this->{$method}($value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$translations[$locale] = $value; |
95
|
|
|
|
96
|
|
|
$this->attributes[$key] = $this->asJson($translations); |
|
|
|
|
97
|
|
|
|
98
|
|
|
event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $key |
105
|
|
|
* @param array $translations |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function setTranslations(string $key, array $translations) |
110
|
|
|
{ |
111
|
|
|
$this->guardAgainstUntranslatableAttribute($key); |
112
|
|
|
|
113
|
|
|
foreach ($translations as $locale => $translation) { |
114
|
|
|
$this->setTranslation($key, $locale, $translation); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $key |
122
|
|
|
* @param string $locale |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function forgetTranslation(string $key, string $locale) |
127
|
|
|
{ |
128
|
|
|
$translations = $this->getTranslations($key); |
129
|
|
|
|
130
|
|
|
unset($translations[$locale]); |
131
|
|
|
|
132
|
|
|
$this->setAttribute($key, $translations); |
|
|
|
|
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getTranslatedLocales(string $key) : array |
138
|
|
|
{ |
139
|
|
|
return array_keys($this->getTranslations($key)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isTranslatableAttribute(string $key) : bool |
143
|
|
|
{ |
144
|
|
|
return in_array($key, $this->getTranslatableAttributes()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
protected function guardAgainstUntranslatableAttribute(string $key) |
148
|
|
|
{ |
149
|
|
|
if (!$this->isTranslatableAttribute($key)) { |
150
|
|
|
throw AttributeIsNotTranslatable::make($key, $this); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function normalizeLocale(string $key, string $locale, bool $withFallBack) : string |
155
|
|
|
{ |
156
|
|
|
if (in_array($locale, $this->getTranslatedLocales($key))) { |
157
|
|
|
return $locale; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (!$withFallBack) |
161
|
|
|
{ |
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
|
|
|
|
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.