Complex classes like Language often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Language, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Language implements JsonSerializable { |
||
| 34 | use Singleton; |
||
| 35 | /** |
||
| 36 | * Callable for time processing |
||
| 37 | * |
||
| 38 | * @var callable |
||
| 39 | */ |
||
| 40 | public $time; |
||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $current_language; |
||
| 45 | /** |
||
| 46 | * For single initialization |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $init = false; |
||
| 51 | /** |
||
| 52 | * Local cache of translations |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $translation = []; |
||
| 57 | /** |
||
| 58 | * Cache to optimize frequent calls |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $localized_url = []; |
||
| 63 | /** |
||
| 64 | * Set basic language |
||
| 65 | */ |
||
| 66 | protected function construct () { |
||
| 95 | /** |
||
| 96 | * Initialization: set default language based on system configuration and request-specific parameters |
||
| 97 | */ |
||
| 98 | protected function init () { |
||
| 99 | $Config = Config::instance(); |
||
| 100 | if ($Config->core['multilingual']) { |
||
| 101 | /** |
||
| 102 | * Highest priority - `-Locale` header |
||
| 103 | */ |
||
| 104 | /** @noinspection PhpParamsInspection */ |
||
| 105 | $language = $this->check_locale_header($Config->core['active_languages']); |
||
| 106 | /** |
||
| 107 | * Second priority - URL |
||
| 108 | */ |
||
| 109 | $language = $language ?: $this->url_language(Request::instance()->path); |
||
| 110 | /** |
||
| 111 | * Third - `Accept-Language` header |
||
| 112 | */ |
||
| 113 | /** @noinspection PhpParamsInspection */ |
||
| 114 | $language = $language ?: $this->check_accept_header($Config->core['active_languages']); |
||
| 115 | } else { |
||
| 116 | $language = $Config->core['language']; |
||
| 117 | } |
||
| 118 | $this->current_language = $language ?: ''; |
||
| 119 | } |
||
| 120 | /** |
||
| 121 | * Returns instance for simplified work with translations, when using common prefix |
||
| 122 | * |
||
| 123 | * @param string $prefix |
||
| 124 | * |
||
| 125 | * @return Prefix |
||
| 126 | */ |
||
| 127 | static function prefix ($prefix) { |
||
| 130 | /** |
||
| 131 | * Does URL have language prefix |
||
| 132 | * |
||
| 133 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
| 134 | * |
||
| 135 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
| 136 | */ |
||
| 137 | 32 | function url_language ($url = false) { |
|
| 156 | /** |
||
| 157 | * Checking Accept-Language header for languages that exists in configuration |
||
| 158 | * |
||
| 159 | * @param array $active_languages |
||
| 160 | * |
||
| 161 | * @return false|string |
||
| 162 | */ |
||
| 163 | protected function check_accept_header ($active_languages) { |
||
| 181 | /** |
||
| 182 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
| 183 | * |
||
| 184 | * @param string[] $active_languages |
||
| 185 | * |
||
| 186 | * @return false|string |
||
| 187 | */ |
||
| 188 | protected function check_locale_header ($active_languages) { |
||
| 204 | /** |
||
| 205 | * Get languages aliases |
||
| 206 | * |
||
| 207 | * @return array|false |
||
| 208 | */ |
||
| 209 | protected function get_aliases () { |
||
| 222 | /** |
||
| 223 | * Get translation |
||
| 224 | * |
||
| 225 | * @param bool|string $item |
||
| 226 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 227 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | function get ($item, $language = false, $prefix = '') { |
||
| 248 | /** |
||
| 249 | * Set translation |
||
| 250 | * |
||
| 251 | * @param array|string $item Item string, or key-value array |
||
| 252 | * @param null|string $value |
||
| 253 | * |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | function set ($item, $value = null) { |
||
| 264 | /** |
||
| 265 | * Get translation |
||
| 266 | * |
||
| 267 | * @param string $item |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | function __get ($item) { |
||
| 274 | /** |
||
| 275 | * Set translation |
||
| 276 | * |
||
| 277 | * @param array|string $item |
||
| 278 | * @param null|string $value |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | function __set ($item, $value = null) { |
||
| 285 | /** |
||
| 286 | * Change language |
||
| 287 | * |
||
| 288 | * @param string $language |
||
| 289 | * |
||
| 290 | * @return bool |
||
| 291 | */ |
||
| 292 | function change ($language) { |
||
| 293 | /** |
||
| 294 | * Already set to specified language |
||
| 295 | */ |
||
| 296 | if ($language == $this->current_language && isset($this->translation[$language])) { |
||
| 297 | return true; |
||
| 298 | } |
||
| 299 | $Config = Config::instance(true); |
||
| 300 | /** |
||
| 301 | * @var string $language |
||
| 302 | */ |
||
| 303 | $language = $language ?: $Config->core['language']; |
||
| 304 | if (!$this->can_be_changed_to($Config, $language)) { |
||
| 305 | return false; |
||
| 306 | } |
||
| 307 | if (!isset($this->translation[$language])) { |
||
| 308 | $this->translation[$language] = $this->get_translation($language); |
||
| 309 | } |
||
| 310 | /** |
||
| 311 | * Change current language to `$language` |
||
| 312 | */ |
||
| 313 | $this->current_language = $language; |
||
| 314 | _include(LANGUAGES."/$language.php", false, false); |
||
| 315 | if ($Config->core['multilingual']) { |
||
| 316 | Response::instance()->header('content-language', $this->content_language); |
||
| 317 | } |
||
| 318 | return true; |
||
| 319 | } |
||
| 320 | /** |
||
| 321 | * Check whether it is allowed to change to specified language according to configuration |
||
| 322 | * |
||
| 323 | * @param Config $Config |
||
| 324 | * @param string $language |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | protected function can_be_changed_to ($Config, $language) { |
||
| 329 | if (!$language) { |
||
| 330 | return false; |
||
| 331 | } |
||
| 332 | return |
||
| 333 | // Config not loaded yet |
||
| 334 | !$Config->core || |
||
| 335 | // Set to language that is configured on system level |
||
| 336 | $language == $Config->core['language'] || |
||
| 337 | // Set to active language |
||
| 338 | ( |
||
| 339 | $Config->core['multilingual'] && |
||
| 340 | in_array($language, $Config->core['active_languages']) |
||
| 341 | ); |
||
| 342 | } |
||
| 343 | protected function get_translation ($language) { |
||
| 344 | return Cache::instance()->get( |
||
| 345 | "languages/$language", |
||
| 346 | function () use ($language) { |
||
| 347 | return $this->get_translation_internal($language); |
||
| 348 | } |
||
| 349 | ); |
||
| 350 | } |
||
| 351 | /** |
||
| 352 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
| 353 | * |
||
| 354 | * @param $language |
||
| 355 | * |
||
| 356 | * @return string[] |
||
| 357 | */ |
||
| 358 | protected function get_translation_internal ($language) { |
||
| 359 | /** |
||
| 360 | * Get current system translations |
||
| 361 | */ |
||
| 362 | $translation = $this->get_translation_from_json(LANGUAGES."/$language.json"); |
||
| 363 | $translation = $this->fill_required_translation_keys($translation, $language); |
||
| 364 | /** |
||
| 365 | * Set modules' translations |
||
| 366 | */ |
||
| 367 | foreach (get_files_list(MODULES, false, 'd', true) as $module_dir) { |
||
| 368 | if (file_exists("$module_dir/languages/$language.json")) { |
||
| 369 | $translation = $this->get_translation_from_json("$module_dir/languages/$language.json") + $translation; |
||
| 370 | } |
||
| 371 | } |
||
| 372 | Event::instance()->fire( |
||
| 373 | 'System/general/languages/load', |
||
| 374 | [ |
||
| 375 | 'clanguage' => $language, |
||
| 376 | 'clang' => $translation['clang'], |
||
| 377 | 'cregion' => $translation['cregion'] |
||
| 378 | ] |
||
| 379 | ); |
||
| 380 | /** |
||
| 381 | * Append translations from core language to fill potentially missing keys |
||
| 382 | */ |
||
| 383 | $core_language = Core::instance()->language; |
||
| 384 | if ($language != $core_language) { |
||
| 385 | $translation += $this->get_translation($core_language); |
||
| 386 | } |
||
| 387 | return $translation; |
||
| 388 | } |
||
| 389 | /** |
||
| 390 | * @param string $filename |
||
| 391 | * |
||
| 392 | * @return string[] |
||
| 393 | */ |
||
| 394 | protected function get_translation_from_json ($filename) { |
||
| 395 | return $this->get_translation_from_json_internal( |
||
| 396 | file_get_json_nocomments($filename) |
||
| 397 | ); |
||
| 398 | } |
||
| 399 | /** |
||
| 400 | * @param string[]|string[][] $translation |
||
| 401 | * |
||
| 402 | * @return string[] |
||
| 403 | */ |
||
| 404 | protected function get_translation_from_json_internal ($translation) { |
||
| 417 | /** |
||
| 418 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
| 419 | * |
||
| 420 | * @param string[] $translation |
||
| 421 | * @param string $language |
||
| 422 | * |
||
| 423 | * @return string[] |
||
| 424 | */ |
||
| 425 | protected function fill_required_translation_keys ($translation, $language) { |
||
| 437 | /** |
||
| 438 | * Time formatting according to the current language (adding correct endings) |
||
| 439 | * |
||
| 440 | * @param int $in time (number) |
||
| 441 | * @param string $type Type of formatting<br> |
||
| 442 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | function time ($in, $type) { |
||
| 468 | /** |
||
| 469 | * Allows to use formatted strings in translations |
||
| 470 | * |
||
| 471 | * @see format() |
||
| 472 | * |
||
| 473 | * @param string $item |
||
| 474 | * @param array $arguments |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | function __call ($item, $arguments) { |
||
| 481 | /** |
||
| 482 | * Allows to use formatted strings in translations |
||
| 483 | * |
||
| 484 | * @param string $item |
||
| 485 | * @param string[] $arguments |
||
| 486 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 487 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | function format ($item, $arguments, $language = false, $prefix = '') { |
||
| 494 | /** |
||
| 495 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
| 496 | * |
||
| 497 | * @param string|string[] $data |
||
| 498 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
| 499 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
| 500 | * |
||
| 501 | * @return string|string[] |
||
| 502 | */ |
||
| 503 | function to_locale ($data, $short_may = false) { |
||
| 558 | /** |
||
| 559 | * Implementation of JsonSerializable interface |
||
| 560 | * |
||
| 561 | * @return string[] |
||
| 562 | */ |
||
| 563 | function jsonSerialize () { |
||
| 566 | } |
||
| 567 |