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  | 
            ||
| 25 | class Language implements JsonSerializable { | 
            ||
| 26 | use Singleton;  | 
            ||
| 27 | /**  | 
            ||
| 28 | * Current language  | 
            ||
| 29 | *  | 
            ||
| 30 | * @var string  | 
            ||
| 31 | */  | 
            ||
| 32 | public $clanguage;  | 
            ||
| 33 | /**  | 
            ||
| 34 | * callable for time processing  | 
            ||
| 35 | *  | 
            ||
| 36 | * @var callable  | 
            ||
| 37 | */  | 
            ||
| 38 | public $time;  | 
            ||
| 39 | /**  | 
            ||
| 40 | * For single initialization  | 
            ||
| 41 | *  | 
            ||
| 42 | * @var bool  | 
            ||
| 43 | */  | 
            ||
| 44 | protected $init = false;  | 
            ||
| 45 | /**  | 
            ||
| 46 | * Local cache of translations  | 
            ||
| 47 | *  | 
            ||
| 48 | * @var array  | 
            ||
| 49 | */  | 
            ||
| 50 | protected $translation = [];  | 
            ||
| 51 | /**  | 
            ||
| 52 | * Cache to optimize frequent calls  | 
            ||
| 53 | *  | 
            ||
| 54 | * @var array  | 
            ||
| 55 | */  | 
            ||
| 56 | protected $localized_url = [];  | 
            ||
| 57 | /**  | 
            ||
| 58 | * Set basic language  | 
            ||
| 59 | */  | 
            ||
| 60 | 	protected function construct () { | 
            ||
| 64 | /**  | 
            ||
| 65 | * Initialization  | 
            ||
| 66 | *  | 
            ||
| 67 | * Is called from Config class by system. Usually there is no need to call it manually.  | 
            ||
| 68 | */  | 
            ||
| 69 | 	function init () { | 
            ||
| 94 | /**  | 
            ||
| 95 | * Does URL have language prefix  | 
            ||
| 96 | *  | 
            ||
| 97 | * @param false|string $url Relative url, `$_SERVER->request_uri` by default  | 
            ||
| 98 | *  | 
            ||
| 99 | * @return false|string If there is language prefix - language will be returned, `false` otherwise  | 
            ||
| 100 | */  | 
            ||
| 101 | 	function url_language ($url = false) { | 
            ||
| 114 | /**  | 
            ||
| 115 | * Checking Accept-Language header for languages that exists in configuration  | 
            ||
| 116 | *  | 
            ||
| 117 | * @param array $active_languages  | 
            ||
| 118 | *  | 
            ||
| 119 | * @return false|string  | 
            ||
| 120 | */  | 
            ||
| 121 | 	protected function check_accept_header ($active_languages) { | 
            ||
| 142 | /**  | 
            ||
| 143 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration  | 
            ||
| 144 | *  | 
            ||
| 145 | * @param array $active_languages  | 
            ||
| 146 | *  | 
            ||
| 147 | * @return false|string  | 
            ||
| 148 | */  | 
            ||
| 149 | 	protected function check_locale_header ($active_languages) { | 
            ||
| 168 | /**  | 
            ||
| 169 | * Get languages aliases  | 
            ||
| 170 | *  | 
            ||
| 171 | * @return array|false  | 
            ||
| 172 | */  | 
            ||
| 173 | 	protected function get_aliases () { | 
            ||
| 186 | /**  | 
            ||
| 187 | * Get translation  | 
            ||
| 188 | *  | 
            ||
| 189 | * @param bool|string $item  | 
            ||
| 190 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current  | 
            ||
| 191 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly  | 
            ||
| 192 | *  | 
            ||
| 193 | * @return string  | 
            ||
| 194 | */  | 
            ||
| 195 | 	function get ($item, $language = false, $prefix = '') { | 
            ||
| 212 | /**  | 
            ||
| 213 | * Set translation  | 
            ||
| 214 | *  | 
            ||
| 215 | * @param array|string $item Item string, or key-value array  | 
            ||
| 216 | * @param null|string $value  | 
            ||
| 217 | *  | 
            ||
| 218 | * @return void  | 
            ||
| 219 | */  | 
            ||
| 220 | 	function set ($item, $value = null) { | 
            ||
| 228 | /**  | 
            ||
| 229 | * Get translation  | 
            ||
| 230 | *  | 
            ||
| 231 | * @param string $item  | 
            ||
| 232 | *  | 
            ||
| 233 | * @return string  | 
            ||
| 234 | */  | 
            ||
| 235 | 	function __get ($item) { | 
            ||
| 238 | /**  | 
            ||
| 239 | * Set translation  | 
            ||
| 240 | *  | 
            ||
| 241 | * @param array|string $item  | 
            ||
| 242 | * @param null|string $value  | 
            ||
| 243 | *  | 
            ||
| 244 | * @return string  | 
            ||
| 245 | */  | 
            ||
| 246 | 	function __set ($item, $value = null) { | 
            ||
| 249 | /**  | 
            ||
| 250 | * Change language  | 
            ||
| 251 | *  | 
            ||
| 252 | * @param string $language  | 
            ||
| 253 | *  | 
            ||
| 254 | * @return bool  | 
            ||
| 255 | */  | 
            ||
| 256 | 	function change ($language) { | 
            ||
| 291 | /**  | 
            ||
| 292 | * Check whether it is allowed to change to specified language according to configuration  | 
            ||
| 293 | *  | 
            ||
| 294 | * @param Config $Config  | 
            ||
| 295 | * @param string $language  | 
            ||
| 296 | *  | 
            ||
| 297 | * @return bool  | 
            ||
| 298 | */  | 
            ||
| 299 | 	protected function can_be_changed_to ($Config, $language) { | 
            ||
| 311 | /**  | 
            ||
| 312 | * Load translation from all over the system, set `$this->translation[$language]` and return it  | 
            ||
| 313 | *  | 
            ||
| 314 | * @param $language  | 
            ||
| 315 | *  | 
            ||
| 316 | * @return string[]  | 
            ||
| 317 | */  | 
            ||
| 318 | 	protected function get_translation ($language) { | 
            ||
| 358 | /**  | 
            ||
| 359 | * @param string $filename  | 
            ||
| 360 | *  | 
            ||
| 361 | * @return string[]  | 
            ||
| 362 | */  | 
            ||
| 363 | 	protected function get_translation_from_json ($filename) { | 
            ||
| 376 | /**  | 
            ||
| 377 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically  | 
            ||
| 378 | *  | 
            ||
| 379 | * @param string[] $translation  | 
            ||
| 380 | * @param string $language  | 
            ||
| 381 | *  | 
            ||
| 382 | * @return string[]  | 
            ||
| 383 | */  | 
            ||
| 384 | 	protected function fill_required_translation_keys ($translation, $language) { | 
            ||
| 401 | /**  | 
            ||
| 402 | * Time formatting according to the current language (adding correct endings)  | 
            ||
| 403 | *  | 
            ||
| 404 | * @param int $in time (number)  | 
            ||
| 405 | * @param string $type Type of formatting<br>  | 
            ||
| 406 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years  | 
            ||
| 407 | *  | 
            ||
| 408 | * @return string  | 
            ||
| 409 | */  | 
            ||
| 410 | 	function time ($in, $type) { | 
            ||
| 432 | /**  | 
            ||
| 433 | * Allows to use formatted strings in translations  | 
            ||
| 434 | *  | 
            ||
| 435 | * @see format()  | 
            ||
| 436 | *  | 
            ||
| 437 | * @param string $item  | 
            ||
| 438 | * @param array $arguments  | 
            ||
| 439 | *  | 
            ||
| 440 | * @return string  | 
            ||
| 441 | */  | 
            ||
| 442 | 	function __call ($item, $arguments) { | 
            ||
| 445 | /**  | 
            ||
| 446 | * Allows to use formatted strings in translations  | 
            ||
| 447 | *  | 
            ||
| 448 | * @param string $item  | 
            ||
| 449 | * @param string[] $arguments  | 
            ||
| 450 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current  | 
            ||
| 451 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly  | 
            ||
| 452 | *  | 
            ||
| 453 | * @return string  | 
            ||
| 454 | */  | 
            ||
| 455 | 	function format ($item, $arguments, $language = false, $prefix = '') { | 
            ||
| 458 | /**  | 
            ||
| 459 | * Formatting date according to language locale (translating months names, days of week, etc.)  | 
            ||
| 460 | *  | 
            ||
| 461 | * @param string|string[] $data  | 
            ||
| 462 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual  | 
            ||
| 463 | * representation as full, so, this option allows to specify, which exactly form of representation do you want  | 
            ||
| 464 | *  | 
            ||
| 465 | * @return string|string[]  | 
            ||
| 466 | */  | 
            ||
| 467 | 	function to_locale ($data, $short_may = false) { | 
            ||
| 522 | /**  | 
            ||
| 523 | * Implementation of JsonSerializable interface  | 
            ||
| 524 | *  | 
            ||
| 525 | * @return string[]  | 
            ||
| 526 | */  | 
            ||
| 527 | 	function jsonSerialize () { | 
            ||
| 530 | }  | 
            ||
| 531 |