1 | <?php |
||
10 | trait Translates |
||
11 | { |
||
12 | /** |
||
13 | * The language the model is currently loaded in. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $selectedLanguage; |
||
18 | |||
19 | /** |
||
20 | * Array of the model's attributes that should be translated. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | // protected $translatable = []; |
||
25 | |||
26 | /** |
||
27 | * Array that holds the translated values while saving is performed. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $translatedAttributes = []; |
||
32 | |||
33 | /** |
||
34 | * Returns the Model identifier used for storing translations in combination with the instance identifier. |
||
35 | * |
||
36 | * @return string |
||
37 | */ |
||
38 | public function getModelIdentifier(): string |
||
42 | |||
43 | /** |
||
44 | * Returns the instance identifier used for storing translations in combination with the model identifier. |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | public function getInstanceIdentifier(): string |
||
52 | |||
53 | /** |
||
54 | * Returns the attribute name of the instance identifier. |
||
55 | * |
||
56 | * @return string |
||
57 | */ |
||
58 | protected function getInstanceIdentifierName(): string |
||
62 | |||
63 | /** |
||
64 | * Set the $translatable array effectively changing which attributes are translatable. |
||
65 | * It is highly recommended you avoid using this method, unless absolutely necessary. |
||
66 | * |
||
67 | * @param array $translatable |
||
68 | * @return self |
||
69 | */ |
||
70 | public function setTranslatable(array $translatable): self |
||
76 | |||
77 | /** |
||
78 | * Return all the translatable attribute names or only the ones present in the first argument. |
||
79 | * |
||
80 | * @param array $only |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getTranslatable(array $only = []): array |
||
89 | |||
90 | /** |
||
91 | * Set the current language on the instance. |
||
92 | * Automatically loads the language if it hasn't been loaded previously. |
||
93 | * |
||
94 | * @param string $language |
||
95 | * @return self |
||
96 | */ |
||
97 | public function setLanguage(string $language): self |
||
106 | |||
107 | /** |
||
108 | * Checks if a language has been previously loaded and is present in the $loadedLanguages array. |
||
109 | * |
||
110 | * @param string $language |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function languageLoaded(string $language): bool |
||
117 | |||
118 | /** |
||
119 | * Load a language and store it in the $loadedLanguages array. |
||
120 | * |
||
121 | * @param string $language |
||
122 | * @return self |
||
123 | */ |
||
124 | public function loadLanguage(string $language): array |
||
131 | |||
132 | /** |
||
133 | * Set the provided data as a loaded language. |
||
134 | * |
||
135 | * @param array $translations |
||
136 | * @return self |
||
137 | */ |
||
138 | public function setTranslations(array $translations): self |
||
159 | |||
160 | /** |
||
161 | * Reload only the languages that have already been loaded. |
||
162 | * |
||
163 | * @return self |
||
164 | */ |
||
165 | public function reloadTranslations(): self |
||
173 | |||
174 | /** |
||
175 | * Return the currently used language. Defaults to the app locale if $selectedLanguage is null. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getActiveLanguage(): string |
||
183 | |||
184 | /** |
||
185 | * Override Laravel's refresh() method to refresh the translations as well. |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | public function refresh(): self |
||
197 | |||
198 | /** |
||
199 | * Override Laravel's getAttribute() method to check if the requested attribute is translatable. |
||
200 | * If the attribute is translatable and the language not loaded, it will load the language. |
||
201 | * |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function getAttribute($attribute) |
||
214 | |||
215 | /** |
||
216 | * Checks whether an attribute is translatable. |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function attributeIsTranslatable($attribute) |
||
224 | |||
225 | /** |
||
226 | * Persist the translations using the loaded TranslationDriver. |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | protected function persistTranslations() |
||
234 | |||
235 | /** |
||
236 | * Merge the translated attributes with the translated ones. |
||
237 | * |
||
238 | * @return void |
||
239 | */ |
||
240 | protected function mergeTranslationsWithAttributes() |
||
244 | |||
245 | /** |
||
246 | * Separate the translatable attribute values from the $attributes array. |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | public function separateTranslationsFromAttributes(): array |
||
263 | |||
264 | /** |
||
265 | * Remove all translations from the persistent storage. |
||
266 | * This is called after the model is deleted. |
||
267 | * |
||
268 | * @param string $language |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function deleteTranslations(...$languages): bool |
||
279 | |||
280 | /** |
||
281 | * Check if the class uses the SoftDeletes trait. |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | protected function usesSoftDelete(): bool |
||
289 | |||
290 | /** |
||
291 | * Register model event listeners which ensure that the translatable attributes are synced |
||
292 | * when the model is being saved, deleted or restored. |
||
293 | */ |
||
294 | public static function bootTranslates() |
||
321 | |||
322 | /** |
||
323 | * We make an accessor for this in order to make it possible to add it to the $appends model property. |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | public function getAvailableLanguagesAttribute() |
||
331 | |||
332 | /** |
||
333 | * We make an accessor for this in order to make it possible to add it to the $appends model property. |
||
334 | * |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getSelectedLanguageAttribute() |
||
341 | |||
342 | /** |
||
343 | * Constrain a query to Models Models available in the provided language. |
||
344 | * |
||
345 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
346 | * @param string $language |
||
347 | * @return \Illuminate\Database\Eloquent\Builder |
||
348 | */ |
||
349 | public function scopeInLanguage(Builder $query, string $language) |
||
356 | } |
||
357 |
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.