1 | <?php |
||
9 | trait HasTranslations |
||
10 | { |
||
11 | /** |
||
12 | * @param string $key |
||
13 | * |
||
14 | * @return mixed |
||
15 | */ |
||
16 | public function getAttributeValue($key) |
||
24 | |||
25 | /** |
||
26 | * Set a given attribute on the model. |
||
27 | * |
||
28 | * @param string $key |
||
29 | * @param mixed $value |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | public function setAttribute($key, $value) |
||
34 | { |
||
35 | // pass arrays and untranslatable attributes to the parent method |
||
36 | if (!$this->isTranslatableAttribute($key) || is_array($value)) { |
||
37 | return parent::setAttribute($key, $value); |
||
38 | } |
||
39 | // if the attribute is translatable and not already translated (=array), |
||
40 | // set a translation for the current app locale |
||
41 | return $this->setTranslation($key, $this->getLocale(), $value); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string $key |
||
46 | * @param string $locale |
||
47 | * |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function translate(string $key, string $locale = '') |
||
54 | |||
55 | /*** |
||
56 | * @param string $key |
||
57 | * @param string $locale |
||
58 | * @param bool $useFallbackLocale |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function getTranslation(string $key, string $locale, bool $useFallbackLocale = true) |
||
76 | |||
77 | public function getTranslationWithFallback(string $key, string $locale) |
||
81 | |||
82 | public function getTranslationWithoutFallback(string $key, string $locale) |
||
86 | |||
87 | public function getTranslations($key) : array |
||
88 | { |
||
89 | $this->guardAgainstUntranslatableAttribute($key); |
||
90 | |||
91 | return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true) ?: []; |
||
92 | } |
||
93 | |||
94 | public function getAllTranslations() : array |
||
95 | { |
||
96 | $translations = []; |
||
97 | |||
98 | foreach ($this->getTranslatableAttributes() as $attribute) { |
||
99 | $this->guardAgainstUntranslatableAttribute($attribute); |
||
100 | $translations[$attribute] = json_decode($this->getAttributes()[$attribute] ?? '' ?: '{}') ?: []; |
||
101 | } |
||
102 | |||
103 | return json_decode(json_encode($translations), true); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param string $key |
||
108 | * @param string $locale |
||
109 | * @param $value |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function setTranslation(string $key, string $locale, $value) |
||
114 | { |
||
115 | $this->guardAgainstUntranslatableAttribute($key); |
||
116 | |||
117 | $translations = $this->getTranslations($key); |
||
118 | |||
119 | $oldValue = $translations[$locale] ?? ''; |
||
120 | |||
121 | if ($this->hasSetMutator($key)) { |
||
122 | $method = 'set'.Str::studly($key).'Attribute'; |
||
123 | $this->{$method}($value, $locale); |
||
124 | $value = $this->attributes[$key]; |
||
125 | } |
||
126 | |||
127 | $translations[$locale] = $value; |
||
128 | |||
129 | $this->attributes[$key] = $this->asJson($translations); |
||
130 | |||
131 | event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $key |
||
138 | * @param array $translations |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function setTranslations(string $key, array $translations) |
||
152 | |||
153 | /** |
||
154 | * @param string $key |
||
155 | * @param string $locale |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function forgetTranslation(string $key, string $locale) |
||
169 | |||
170 | public function forgetAllTranslations(string $locale) |
||
176 | |||
177 | public function getTranslatedLocales(string $key) : array |
||
181 | |||
182 | public function isTranslatableAttribute(string $key) : bool |
||
186 | |||
187 | protected function guardAgainstUntranslatableAttribute(string $key) |
||
193 | |||
194 | protected function normalizeLocale(string $key, string $locale, bool $useFallbackLocale) : string |
||
210 | |||
211 | protected function getLocale() : string |
||
212 | { |
||
213 | return config('app.locale'); |
||
215 | |||
216 | public function getTranslatableAttributes() : array |
||
222 | |||
223 | public function getCasts() : array |
||
230 | } |
||
231 |
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.