1 | <?php |
||
31 | trait I18NAbleTrait |
||
32 | { |
||
33 | |||
34 | private $_lang = 'en'; |
||
35 | private $_rawI18N = []; |
||
36 | private $_languages = ['en']; |
||
37 | private $_defaultLanguage = 'en'; |
||
38 | |||
39 | /** |
||
40 | * Get current working language code |
||
41 | * @return string Language code |
||
42 | * @Ignored |
||
43 | */ |
||
44 | 80 | public function getLang() |
|
48 | |||
49 | /** |
||
50 | * Get available languages |
||
51 | * @return string[] |
||
52 | * @Ignored |
||
53 | */ |
||
54 | 39 | public function getLanguages() |
|
58 | |||
59 | /** |
||
60 | * Get i18n values with all languages. |
||
61 | * This returns all language values of all class fields. Class field names are |
||
62 | * keys for arrays of language values, with language codes as a keys. |
||
63 | * |
||
64 | * Example returned variable: |
||
65 | * ```php |
||
66 | * [ |
||
67 | * 'name' => [ |
||
68 | * 'en' => 'January', |
||
69 | * 'pl' => 'Styczeń' |
||
70 | * ], |
||
71 | * 'description' => [ |
||
72 | * 'en' => 'First mothn of a year' |
||
73 | * 'pl' => 'Pierwszy miesiąc roku' |
||
74 | * ] |
||
75 | * ] |
||
76 | * ``` |
||
77 | * @return mixed[] Associative array of language values |
||
78 | * @Ignored |
||
79 | */ |
||
80 | 31 | public function getRawI18N() |
|
93 | |||
94 | /** |
||
95 | * Set language code. This changes current model language. |
||
96 | * After setting language model attributes will store values in different locale. |
||
97 | * |
||
98 | * Language code must be previously set by `setLanguages`. |
||
99 | * When trying to set undefined language code, method will do nothing. |
||
100 | * When setting already choosen language code, method will also ignore this call. |
||
101 | * Example method calls: |
||
102 | * ```php |
||
103 | * // Set available languages |
||
104 | * $model->setLanguages(['en', 'pl']); |
||
105 | * |
||
106 | * // Will ignore as en is by default |
||
107 | * $model->setLang('en'); |
||
108 | * |
||
109 | * // Will set pl as language |
||
110 | * $model->setLang('pl'); |
||
111 | * |
||
112 | * // Will ignore as ru is not available |
||
113 | * $model->setLang('ru') |
||
114 | * ``` |
||
115 | * |
||
116 | * For initial call, when there are no data set yet, `$triggetEvents` |
||
117 | * can be set to false to improve performance. |
||
118 | * |
||
119 | * @param string $code |
||
120 | * @param boolean $triggerEvents |
||
121 | * @Ignored |
||
122 | */ |
||
123 | 97 | public function setLang($code, $triggerEvents = true) |
|
170 | |||
171 | /** |
||
172 | * Set available languages. This method accepts one parameter, |
||
173 | * array contaning language codes prefably in short ISO form. |
||
174 | * |
||
175 | * Example valid array and method calls: |
||
176 | * |
||
177 | * ```php |
||
178 | * $languages = ['en', 'pl', 'ru']; |
||
179 | * $model->setLanguages($languages); |
||
180 | * $model2->setLanguages(['en']); |
||
181 | * ``` |
||
182 | * |
||
183 | * For initial call, when there are no data set yet, `$triggetEvents` |
||
184 | * can be set to false to improve performance. |
||
185 | * |
||
186 | * @param string[] $languages |
||
187 | * @param boolean $triggerEvents |
||
188 | * @Ignored |
||
189 | */ |
||
190 | 80 | public function setLanguages($languages, $triggerEvents = true) |
|
207 | |||
208 | /** |
||
209 | * Set i18n values in all languages. |
||
210 | * This method must keep `$values` for further use, by method `getRawI18N`. |
||
211 | * @param mixed[] $values |
||
212 | * @Ignored |
||
213 | */ |
||
214 | 15 | public function setRawI18N($values) |
|
218 | |||
219 | /** |
||
220 | * Get default language used for I18N operations. |
||
221 | * |
||
222 | * If not previously set, will fall back to `en`. |
||
223 | * |
||
224 | * @return string |
||
225 | * @Ignored |
||
226 | */ |
||
227 | 11 | public function getDefaultLanguage() |
|
231 | |||
232 | /** |
||
233 | * Set default language used for I18N operations. This language |
||
234 | * will be used if the `setLang` method was not called. |
||
235 | * |
||
236 | * The value should be language code, for example `en` |
||
237 | * |
||
238 | * @param string $language |
||
239 | * @Ignored |
||
240 | */ |
||
241 | 1 | public function setDefaultLanguage($language) |
|
245 | |||
246 | /** |
||
247 | * Change i18n attributes values to appropriate language |
||
248 | * @param string $fromCode |
||
249 | * @param string $toCode |
||
250 | */ |
||
251 | 11 | private function _changeAttributesLang($fromCode, $toCode) |
|
297 | |||
298 | } |
||
299 |