1 | <?php |
||
19 | trait MultilanguageTrait |
||
20 | { |
||
21 | /** |
||
22 | * Container for temporary storage of translation data. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | private $storage = []; |
||
27 | |||
28 | /** |
||
29 | * Return key name of relation between main table and translations table. |
||
30 | * |
||
31 | * @return string |
||
32 | */ |
||
33 | public static function getKeyToMainModel() |
||
37 | |||
38 | /** |
||
39 | * Return translations table name. |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | public static function getTranslateTablelName() |
||
47 | |||
48 | /** |
||
49 | * Return related translate model name. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public static function getTranslateModelName() |
||
59 | |||
60 | /** |
||
61 | * Return related translated records. |
||
62 | * |
||
63 | * @return ActiveQuery |
||
64 | */ |
||
65 | public function getTranslateList() |
||
71 | |||
72 | /** |
||
73 | * Override model magic getter. Return translate for field. |
||
74 | * Example: if we try $model->title_en, we will get 'title' in english. |
||
75 | * |
||
76 | * @param string $name field name. |
||
77 | * |
||
78 | * @return mixed|null |
||
79 | */ |
||
80 | public function __get($name) |
||
98 | |||
99 | /** |
||
100 | * Override model magic setter. Set translation for the field. |
||
101 | * For example $model->title_en will save title field in translate model where |
||
102 | * language_id => record in language with 'en' locale. |
||
103 | * |
||
104 | * @param string $name name of field. |
||
105 | * @param mixed $value value to be stored in field. |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | public function __set($name, $value) |
||
122 | |||
123 | /** |
||
124 | * Override model method to save all translations after main model saved. |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function afterSave($insert, $changedAttributes) |
||
142 | |||
143 | /** |
||
144 | * Returns default translate. If field name is given, we can take an alternative |
||
145 | * translate when default translate value is empty. |
||
146 | * |
||
147 | * @param string $field |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function getDefaultTranslate($field = null) |
||
179 | |||
180 | /** |
||
181 | * Check for multi-language mode of field. |
||
182 | * |
||
183 | * @param string $name name of field to be checked. |
||
184 | * |
||
185 | * @return boolean |
||
186 | */ |
||
187 | private function isMultiLanguageField($name): bool |
||
206 | |||
207 | /** |
||
208 | * Find or create related model with translates. |
||
209 | * |
||
210 | * @param string $lang language short name. |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | private function findOrCreateTranslateModel($lang) |
||
239 | } |
||
240 |
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.