1 | <?php |
||
9 | trait HasTranslations |
||
10 | { |
||
11 | public $locale = false; |
||
12 | |||
13 | /** |
||
14 | * @param string $key |
||
15 | * |
||
16 | * @return mixed |
||
17 | */ |
||
18 | public function getAttributeValue($key) |
||
19 | { |
||
20 | if (!$this->isTranslatableAttribute($key)) { |
||
21 | return parent::getAttributeValue($key); |
||
22 | } |
||
23 | |||
24 | return $this->getTranslation($key, config('app.locale')); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @param string $key |
||
29 | * @param string $locale |
||
30 | * |
||
31 | * @return mixed |
||
32 | */ |
||
33 | public function translate(string $key, string $locale = '') |
||
34 | { |
||
35 | return $this->getTranslation($key, $locale); |
||
36 | } |
||
37 | |||
38 | /*** |
||
39 | * @param string $key |
||
40 | * @param string $locale |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function getTranslation(string $key, string $locale) |
||
45 | { |
||
46 | $locale = $this->normalizeLocale($key, $locale); |
||
47 | |||
48 | $translations = $this->getTranslations($key); |
||
49 | |||
50 | $translation = $translations[$locale] ?? ''; |
||
51 | |||
52 | if ($this->hasGetMutator($key)) { |
||
|
|||
53 | return $this->mutateAttribute($key, $translation); |
||
54 | } |
||
55 | |||
56 | return $translation; |
||
57 | } |
||
58 | |||
59 | public function getTranslations($key) : array |
||
60 | { |
||
61 | $this->guardAgainstUntranslatableAttribute($key); |
||
62 | |||
63 | return json_decode($this->getAttributes()[$key] ?? '' ?: '{}', true); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string $key |
||
68 | * @param string $locale |
||
69 | * @param $value |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function setTranslation(string $key, string $locale, $value) |
||
74 | { |
||
75 | $this->guardAgainstUntranslatableAttribute($key); |
||
76 | |||
77 | $translations = $this->getTranslations($key); |
||
78 | |||
79 | $oldValue = $translations[$locale] ?? ''; |
||
80 | |||
81 | if ($this->hasSetMutator($key)) { |
||
82 | $method = 'set'.Str::studly($key).'Attribute'; |
||
83 | $value = $this->{$method}($value); |
||
84 | } |
||
85 | |||
86 | $translations[$locale] = $value; |
||
87 | |||
88 | $this->attributes[$key] = $this->asJson($translations); |
||
89 | |||
90 | event(new TranslationHasBeenSet($this, $key, $locale, $oldValue, $value)); |
||
91 | |||
92 | return $this; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $key |
||
97 | * @param array $translations |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setTranslations(string $key, array $translations) |
||
102 | { |
||
103 | $this->guardAgainstUntranslatableAttribute($key); |
||
104 | |||
105 | foreach ($translations as $locale => $translation) { |
||
106 | $this->setTranslation($key, $locale, $translation); |
||
107 | } |
||
108 | |||
109 | return $this; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param string $key |
||
114 | * @param string $locale |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function forgetTranslation(string $key, string $locale) |
||
119 | { |
||
120 | $translations = $this->getTranslations($key); |
||
121 | |||
122 | unset($translations[$locale]); |
||
123 | |||
124 | $this->setAttribute($key, $translations); |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | public function getTranslatedLocales(string $key) : array |
||
130 | { |
||
131 | return array_keys($this->getTranslations($key)); |
||
132 | } |
||
133 | |||
134 | public function isTranslatableAttribute(string $key) : bool |
||
135 | { |
||
136 | return in_array($key, $this->getTranslatableAttributes()); |
||
137 | } |
||
138 | |||
139 | protected function guardAgainstUntranslatableAttribute(string $key) |
||
140 | { |
||
141 | if (!$this->isTranslatableAttribute($key)) { |
||
142 | throw AttributeIsNotTranslatable::make($key, $this); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | protected function normalizeLocale(string $key, string $locale) : string |
||
147 | { |
||
148 | // if a locale has been set on the model, use that |
||
149 | if ($this->locale) { |
||
150 | return $this->locale; |
||
151 | } |
||
152 | |||
153 | if (in_array($locale, $this->getTranslatedLocales($key))) { |
||
154 | return $locale; |
||
155 | } |
||
156 | |||
157 | if (!is_null($fallbackLocale = config('laravel-translatable.fallback_locale'))) { |
||
158 | return $fallbackLocale; |
||
159 | } |
||
160 | |||
161 | return $locale; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set the locale property. Used in normalizeLocale() to force the translation |
||
166 | * to a different language that the one set in app()->getLocale(); |
||
167 | * |
||
168 | * @param string $locale |
||
169 | */ |
||
170 | public function setLocale($locale) |
||
171 | { |
||
172 | $this->locale = $locale; |
||
173 | } |
||
174 | |||
175 | public function getTranslatableAttributes() : array |
||
181 | |||
182 | public function getCasts() : array |
||
189 | } |
||
190 |
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.