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  | 
            ||
| 37 | class Language implements JsonSerializable { | 
            ||
| 38 | use  | 
            ||
| 39 | Singleton;  | 
            ||
| 40 | const INIT_STATE_METHOD = 'init';  | 
            ||
| 41 | /**  | 
            ||
| 42 | * Callable for time processing  | 
            ||
| 43 | *  | 
            ||
| 44 | * @var callable  | 
            ||
| 45 | */  | 
            ||
| 46 | public $time;  | 
            ||
| 47 | /**  | 
            ||
| 48 | * @var string  | 
            ||
| 49 | */  | 
            ||
| 50 | protected $current_language;  | 
            ||
| 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 | 62 | 	protected function init () { | 
            |
| 64 | /**  | 
            ||
| 65 | * Initialization: set default language based on system configuration and request-specific parameters  | 
            ||
| 66 | */  | 
            ||
| 67 | 62 | $Config = Config::instance(true);  | 
            |
| 68 | /**  | 
            ||
| 69 | * We need Config for initialization  | 
            ||
| 70 | */  | 
            ||
| 71 | 62 | 		if (!$Config) { | 
            |
| 72 | Event::instance()->once(  | 
            ||
| 73 | 'System/Config/init/after',  | 
            ||
| 74 | 				function () { | 
            ||
| 75 | $this->init_internal();  | 
            ||
| 76 | }  | 
            ||
| 77 | );  | 
            ||
| 78 | 		} else { | 
            ||
| 79 | 62 | $this->init_internal();  | 
            |
| 80 | }  | 
            ||
| 81 | /**  | 
            ||
| 82 | * Change language when configuration changes  | 
            ||
| 83 | */  | 
            ||
| 84 | 62 | Event::instance()->on(  | 
            |
| 85 | 62 | 'System/Config/changed',  | 
            |
| 86 | 			function () { | 
            ||
| 87 | 2 | $this->init_internal();  | 
            |
| 88 | 62 | }  | 
            |
| 89 | );  | 
            ||
| 90 | 62 | }  | 
            |
| 91 | 62 | 	protected function init_internal () { | 
            |
| 92 | 62 | $Config = Config::instance();  | 
            |
| 93 | 62 | $language = '';  | 
            |
| 94 | 62 | 		if ($Config->core['multilingual']) { | 
            |
| 95 | 12 | $language = User::instance(true)->language;  | 
            |
| 96 | /**  | 
            ||
| 97 | * Highest priority - `-Locale` header  | 
            ||
| 98 | */  | 
            ||
| 99 | /** @noinspection PhpParamsInspection */  | 
            ||
| 100 | 12 | $language = $language ?: $this->check_locale_header($Config->core['active_languages']);  | 
            |
| 101 | /**  | 
            ||
| 102 | * Second priority - URL  | 
            ||
| 103 | */  | 
            ||
| 104 | 12 | $language = $language ?: $this->url_language(Request::instance()->path);  | 
            |
| 105 | /**  | 
            ||
| 106 | * Third - `Accept-Language` header  | 
            ||
| 107 | */  | 
            ||
| 108 | /** @noinspection PhpParamsInspection */  | 
            ||
| 109 | 12 | $language = $language ?: $this->check_accept_header($Config->core['active_languages']);  | 
            |
| 110 | }  | 
            ||
| 111 | 62 | $this->current_language = $language ?: $Config->core['language'];  | 
            |
| 112 | 62 | }  | 
            |
| 113 | /**  | 
            ||
| 114 | * Returns instance for simplified work with translations, when using common prefix  | 
            ||
| 115 | *  | 
            ||
| 116 | * @param string $prefix  | 
            ||
| 117 | *  | 
            ||
| 118 | * @return Prefix  | 
            ||
| 119 | */  | 
            ||
| 120 | 22 | 	public static function prefix ($prefix) { | 
            |
| 121 | 22 | return new Prefix($prefix);  | 
            |
| 122 | }  | 
            ||
| 123 | /**  | 
            ||
| 124 | * Does URL have language prefix  | 
            ||
| 125 | *  | 
            ||
| 126 | * @param false|string $url Relative url, `Request::instance()->path` by default  | 
            ||
| 127 | *  | 
            ||
| 128 | * @return false|string If there is language prefix - language will be returned, `false` otherwise  | 
            ||
| 129 | */  | 
            ||
| 130 | 36 | 	public function url_language ($url = false) { | 
            |
| 131 | /**  | 
            ||
| 132 | * @var string $url  | 
            ||
| 133 | */  | 
            ||
| 134 | 36 | $url = $url ?: Request::instance()->path;  | 
            |
| 135 | 36 | 		if (isset($this->localized_url[$url])) { | 
            |
| 136 | return $this->localized_url[$url];  | 
            ||
| 137 | }  | 
            ||
| 138 | 36 | $aliases = $this->get_aliases();  | 
            |
| 139 | 36 | 		$clang   = explode('?', $url, 2)[0]; | 
            |
| 140 | 36 | 		$clang   = explode('/', trim($clang, '/'), 2)[0]; | 
            |
| 141 | 36 | 		if (isset($aliases[$clang])) { | 
            |
| 142 | 6 | 			if (count($this->localized_url) > 100) { | 
            |
| 143 | $this->localized_url = [];  | 
            ||
| 144 | }  | 
            ||
| 145 | 6 | return $this->localized_url[$url] = $aliases[$clang];  | 
            |
| 146 | }  | 
            ||
| 147 | 34 | return false;  | 
            |
| 148 | }  | 
            ||
| 149 | /**  | 
            ||
| 150 | * Checking Accept-Language header for languages that exists in configuration  | 
            ||
| 151 | *  | 
            ||
| 152 | * @param array $active_languages  | 
            ||
| 153 | *  | 
            ||
| 154 | * @return false|string  | 
            ||
| 155 | */  | 
            ||
| 156 | 8 | 	protected function check_accept_header ($active_languages) { | 
            |
| 157 | 8 | $aliases = $this->get_aliases();  | 
            |
| 158 | 8 | $accept_languages = array_filter(  | 
            |
| 159 | explode(  | 
            ||
| 160 | 8 | ',',  | 
            |
| 161 | strtolower(  | 
            ||
| 162 | 8 | 					str_replace('-', '_', Request::instance()->header('accept-language')) | 
            |
| 163 | )  | 
            ||
| 164 | )  | 
            ||
| 165 | );  | 
            ||
| 166 | 8 | 		foreach ($accept_languages as $language) { | 
            |
| 167 | 6 | 			$language = explode(';', $language, 2)[0]; | 
            |
| 168 | 6 | 			if (@in_array($aliases[$language], $active_languages)) { | 
            |
| 169 | 6 | return $aliases[$language];  | 
            |
| 170 | }  | 
            ||
| 171 | }  | 
            ||
| 172 | 2 | return false;  | 
            |
| 173 | }  | 
            ||
| 174 | /**  | 
            ||
| 175 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration  | 
            ||
| 176 | *  | 
            ||
| 177 | * @param string[] $active_languages  | 
            ||
| 178 | *  | 
            ||
| 179 | * @return false|string  | 
            ||
| 180 | */  | 
            ||
| 181 | 12 | 	protected function check_locale_header ($active_languages) { | 
            |
| 182 | 12 | $aliases = $this->get_aliases();  | 
            |
| 183 | /**  | 
            ||
| 184 | * For `X-Facebook-Locale` and other similar  | 
            ||
| 185 | */  | 
            ||
| 186 | 12 | 		foreach (Request::instance()->headers ?: [] as $i => $v) { | 
            |
| 187 | 10 | 			if (stripos($i, '-locale') !== false) { | 
            |
| 188 | 2 | $language = strtolower($v);  | 
            |
| 189 | 2 | 				if (@in_array($aliases[$language], $active_languages)) { | 
            |
| 190 | 2 | return $aliases[$language];  | 
            |
| 191 | }  | 
            ||
| 192 | 10 | return false;  | 
            |
| 193 | }  | 
            ||
| 194 | }  | 
            ||
| 195 | 10 | return false;  | 
            |
| 196 | }  | 
            ||
| 197 | /**  | 
            ||
| 198 | * Get languages aliases  | 
            ||
| 199 | *  | 
            ||
| 200 | * @return array|false  | 
            ||
| 201 | */  | 
            ||
| 202 | 36 | 	protected function get_aliases () { | 
            |
| 203 | 36 | return Cache::instance()->get(  | 
            |
| 204 | 36 | 'languages/aliases',  | 
            |
| 205 | 			function () { | 
            ||
| 206 | 4 | $aliases = [];  | 
            |
| 207 | /**  | 
            ||
| 208 | * @var string[] $aliases_list  | 
            ||
| 209 | */  | 
            ||
| 210 | 4 | $aliases_list = _strtolower(get_files_list(LANGUAGES.'/aliases'));  | 
            |
| 211 | 4 | 				foreach ($aliases_list as $alias) { | 
            |
| 212 | 4 | $aliases[$alias] = trim(file_get_contents(LANGUAGES."/aliases/$alias"));  | 
            |
| 213 | }  | 
            ||
| 214 | 4 | return $aliases;  | 
            |
| 215 | 36 | }  | 
            |
| 216 | );  | 
            ||
| 217 | }  | 
            ||
| 218 | /**  | 
            ||
| 219 | * Get translation  | 
            ||
| 220 | *  | 
            ||
| 221 | * @param bool|string $item  | 
            ||
| 222 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current  | 
            ||
| 223 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly  | 
            ||
| 224 | *  | 
            ||
| 225 | * @return string  | 
            ||
| 226 | */  | 
            ||
| 227 | 54 | 	public function get ($item, $language = false, $prefix = '') { | 
            |
| 228 | /**  | 
            ||
| 229 | * Small optimization, we can actually return value without translations  | 
            ||
| 230 | */  | 
            ||
| 231 | 54 | 		if ($item == 'clanguage' && $this->current_language && $language === false && !$prefix) { | 
            |
| 232 | 44 | return $this->current_language;  | 
            |
| 233 | }  | 
            ||
| 234 | 54 | $language = $language ?: $this->current_language;  | 
            |
| 235 | 54 | 		if (isset($this->translation[$language])) { | 
            |
| 236 | 54 | $translation = $this->translation[$language];  | 
            |
| 237 | 54 | 			if (isset($translation[$prefix.$item])) { | 
            |
| 238 | 54 | return $translation[$prefix.$item];  | 
            |
| 239 | 6 | 			} elseif (isset($translation[$item])) { | 
            |
| 240 | 4 | return $translation[$item];  | 
            |
| 241 | }  | 
            ||
| 242 | 2 | 			return ucfirst(str_replace('_', ' ', $item)); | 
            |
| 243 | }  | 
            ||
| 244 | 50 | $current_language = $this->current_language;  | 
            |
| 245 | 50 | $this->change($language);  | 
            |
| 246 | 50 | $return = $this->get($item, $this->current_language, $prefix);  | 
            |
| 247 | 50 | $this->change($current_language);  | 
            |
| 248 | 50 | return $return;  | 
            |
| 249 | }  | 
            ||
| 250 | /**  | 
            ||
| 251 | * Set translation  | 
            ||
| 252 | *  | 
            ||
| 253 | * @param array|string $item Item string, or key-value array  | 
            ||
| 254 | * @param null|string $value  | 
            ||
| 255 | *  | 
            ||
| 256 | * @return void  | 
            ||
| 257 | */  | 
            ||
| 258 | 	public function set ($item, $value = null) { | 
            ||
| 259 | $translate = &$this->translation[$this->current_language];  | 
            ||
| 260 | 		if (is_array($item)) { | 
            ||
| 261 | $translate = $item + ($translate ?: []);  | 
            ||
| 262 | 		} else { | 
            ||
| 263 | $translate[$item] = $value;  | 
            ||
| 264 | }  | 
            ||
| 265 | }  | 
            ||
| 266 | /**  | 
            ||
| 267 | * Get translation  | 
            ||
| 268 | *  | 
            ||
| 269 | * @param string $item  | 
            ||
| 270 | *  | 
            ||
| 271 | * @return string  | 
            ||
| 272 | */  | 
            ||
| 273 | 54 | 	public function __get ($item) { | 
            |
| 274 | 54 | return $this->get($item);  | 
            |
| 275 | }  | 
            ||
| 276 | /**  | 
            ||
| 277 | * Set translation  | 
            ||
| 278 | *  | 
            ||
| 279 | * @param array|string $item  | 
            ||
| 280 | * @param null|string $value  | 
            ||
| 281 | */  | 
            ||
| 282 | 	public function __set ($item, $value = null) { | 
            ||
| 285 | /**  | 
            ||
| 286 | * Change language  | 
            ||
| 287 | *  | 
            ||
| 288 | * @param string $language  | 
            ||
| 289 | *  | 
            ||
| 290 | * @return bool  | 
            ||
| 291 | */  | 
            ||
| 292 | 54 | 	public function change ($language) { | 
            |
| 293 | /**  | 
            ||
| 294 | * Already set to specified language  | 
            ||
| 295 | */  | 
            ||
| 324 | /**  | 
            ||
| 325 | * Check whether it is allowed to change to specified language according to configuration  | 
            ||
| 326 | *  | 
            ||
| 327 | * @param Config $Config  | 
            ||
| 328 | * @param string $language  | 
            ||
| 329 | *  | 
            ||
| 330 | * @return bool  | 
            ||
| 331 | */  | 
            ||
| 332 | 54 | 	protected function can_be_changed_to ($Config, $language) { | 
            |
| 355 | /**  | 
            ||
| 356 | * Load translation from all over the system, set `$this->translation[$language]` and return it  | 
            ||
| 357 | *  | 
            ||
| 358 | * @param $language  | 
            ||
| 359 | *  | 
            ||
| 360 | * @return string[]  | 
            ||
| 361 | */  | 
            ||
| 362 | 10 | 	protected function get_translation_internal ($language) { | 
            |
| 402 | /**  | 
            ||
| 403 | * @param string $filename  | 
            ||
| 404 | *  | 
            ||
| 405 | * @return string[]  | 
            ||
| 406 | */  | 
            ||
| 407 | 10 | 	protected function get_translation_from_json ($filename) { | 
            |
| 412 | /**  | 
            ||
| 413 | * @param string[]|string[][] $translation  | 
            ||
| 414 | *  | 
            ||
| 415 | * @return string[]  | 
            ||
| 416 | */  | 
            ||
| 417 | 10 | 	protected function get_translation_from_json_internal ($translation) { | 
            |
| 430 | /**  | 
            ||
| 431 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically  | 
            ||
| 432 | *  | 
            ||
| 433 | * @param string[] $translation  | 
            ||
| 434 | * @param string $language  | 
            ||
| 435 | *  | 
            ||
| 436 | * @return string[]  | 
            ||
| 437 | */  | 
            ||
| 438 | 10 | 	protected function fill_required_translation_keys ($translation, $language) { | 
            |
| 450 | /**  | 
            ||
| 451 | * Time formatting according to the current language (adding correct endings)  | 
            ||
| 452 | *  | 
            ||
| 453 | * @param int $in time (number)  | 
            ||
| 454 | * @param string $type Type of formatting<br>  | 
            ||
| 455 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years  | 
            ||
| 456 | *  | 
            ||
| 457 | * @return string  | 
            ||
| 458 | */  | 
            ||
| 459 | 4 | 	public function time ($in, $type) { | 
            |
| 481 | /**  | 
            ||
| 482 | * Allows to use formatted strings in translations  | 
            ||
| 483 | *  | 
            ||
| 484 | * @see format()  | 
            ||
| 485 | *  | 
            ||
| 486 | * @param string $item  | 
            ||
| 487 | * @param array $arguments  | 
            ||
| 488 | *  | 
            ||
| 489 | * @return string  | 
            ||
| 490 | */  | 
            ||
| 491 | 2 | 	public function __call ($item, $arguments) { | 
            |
| 494 | /**  | 
            ||
| 495 | * Allows to use formatted strings in translations  | 
            ||
| 496 | *  | 
            ||
| 497 | * @param string $item  | 
            ||
| 498 | * @param string[] $arguments  | 
            ||
| 499 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current  | 
            ||
| 500 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly  | 
            ||
| 501 | *  | 
            ||
| 502 | * @return string  | 
            ||
| 503 | */  | 
            ||
| 504 | 6 | 	public function format ($item, $arguments, $language = false, $prefix = '') { | 
            |
| 507 | /**  | 
            ||
| 508 | * Formatting date according to language locale (translating months names, days of week, etc.)  | 
            ||
| 509 | *  | 
            ||
| 510 | * @param string|string[] $data  | 
            ||
| 511 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual  | 
            ||
| 512 | * representation as full, so, this option allows to specify, which exactly form of representation do you want  | 
            ||
| 513 | *  | 
            ||
| 514 | * @return string|string[]  | 
            ||
| 515 | */  | 
            ||
| 516 | 2 | 	public function to_locale ($data, $short_may = false) { | 
            |
| 571 | /**  | 
            ||
| 572 | * Implementation of JsonSerializable interface  | 
            ||
| 573 | *  | 
            ||
| 574 | * @return string[]  | 
            ||
| 575 | */  | 
            ||
| 576 | 4 | 	public function jsonSerialize () { | 
            |
| 579 | }  | 
            ||
| 580 |