Complex classes like I18N 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 I18N, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class I18N implements I18NInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * An array of the loaded languages |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $languagesLoaded = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * An array of existing languages by ISO |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $languagesIso = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * An array of existing languages by ID |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $languagesId = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Language currently in use |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $currentLanguage; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Language currently in use (ID) |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $currentLanguageId; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * All the translation strings |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | protected $strings = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Context of the current page |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $pageContext; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Strings cache |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $stringsRaw = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var CacheRepository The cache to store the terms in |
||
| 74 | */ |
||
| 75 | protected $cache; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var Session The session store |
||
| 79 | */ |
||
| 80 | protected $session; |
||
| 81 | |||
| 82 | /** |
||
| 83 | 177 | * @var Log The log writer |
|
| 84 | */ |
||
| 85 | 177 | protected $log; |
|
| 86 | 177 | ||
| 87 | 177 | /** |
|
| 88 | 177 | * @var ConfigRepository |
|
| 89 | */ |
||
| 90 | 177 | protected $config; |
|
| 91 | 177 | ||
| 92 | 177 | /** |
|
| 93 | 177 | * @var Application |
|
| 94 | 177 | */ |
|
| 95 | protected $app; |
||
| 96 | 177 | ||
| 97 | /** |
||
| 98 | 177 | * Prepare the translation service |
|
| 99 | 177 | * |
|
| 100 | 177 | * @param Application $app |
|
| 101 | 177 | */ |
|
| 102 | 177 | public function __construct(Application $app, CacheRepository $cache, Session $session, Log $log, ConfigRepository $config) |
|
| 132 | |||
| 133 | protected function getFilePath() { |
||
| 136 | |||
| 137 | 177 | /** |
|
| 138 | * Detects the default languages in the following order : |
||
| 139 | * |
||
| 140 | * 1. Is a user session var defined ? |
||
| 141 | * 2. Can we take it from the browser ? |
||
| 142 | 177 | * 3. Take the site default |
|
| 143 | * |
||
| 144 | * @param $locale string |
||
| 145 | 177 | * @param $fallback string |
|
| 146 | 177 | * @throws Exception if a default language cannot be found |
|
| 147 | 177 | * @return string |
|
| 148 | */ |
||
| 149 | 177 | public function getDefaultLanguage($locale, $fallback) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Set the current language |
||
| 191 | 177 | * |
|
| 192 | * @param string $language |
||
| 193 | 177 | * @return bool |
|
| 194 | */ |
||
| 195 | public function setCurrentLanguage($language) |
||
| 207 | 177 | ||
| 208 | /** |
||
| 209 | * Load a language file |
||
| 210 | * |
||
| 211 | * @param string $language |
||
| 212 | * @return bool |
||
| 213 | 27 | */ |
|
| 214 | public function loadLanguage($language) |
||
| 232 | |||
| 233 | 177 | /** |
|
| 234 | * Get the current language |
||
| 235 | 177 | * @return string |
|
| 236 | 156 | */ |
|
| 237 | public function getCurrent() |
||
| 241 | 177 | ||
| 242 | /** |
||
| 243 | * Get the current language id |
||
| 244 | 177 | * @return int |
|
| 245 | 21 | */ |
|
| 246 | 21 | public function getCurrentId() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Set the language to use |
||
| 253 | * |
||
| 254 | * @param string $language |
||
| 255 | 177 | * @return bool |
|
| 256 | 177 | */ |
|
| 257 | 177 | public function setLanguage($language) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Checks if a language is loaded or not |
||
| 285 | * |
||
| 286 | * @param string $language |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | protected function isLoaded($language) |
||
| 293 | 177 | ||
| 294 | /** |
||
| 295 | * Checks if a language is the default one |
||
| 296 | * |
||
| 297 | * @param string $language |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | protected function isDefault($language) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Checks if the language is availavble |
||
| 311 | * |
||
| 312 | * @param string $language |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | protected function isAvailable($language) |
||
| 319 | |||
| 320 | /** |
||
| 321 | 36 | * Retrieve languages. |
|
| 322 | * |
||
| 323 | 36 | * this is a hybrid method. |
|
| 324 | 33 | * |
|
| 325 | * |
||
| 326 | * I18N::languages(); |
||
| 327 | 3 | * returns ['fr' => ['id' => 1, 'name' => 'francais', 'iso' => 'fr'], 'en' => ...] |
|
| 328 | * |
||
| 329 | * |
||
| 330 | * I18N::languages('fr'); |
||
| 331 | * returns ['id' => 1, 'name' => 'francais', 'iso' => 'fr'] |
||
| 332 | * |
||
| 333 | * |
||
| 334 | * I18N::languages(1); |
||
| 335 | 3 | * returns ['id' => 1, 'name' => 'francais', 'iso' => 'fr'] |
|
| 336 | * |
||
| 337 | * |
||
| 338 | * I18N::languages('fr', 'id'); |
||
| 339 | 3 | * returns 1 |
|
| 340 | * |
||
| 341 | * @param int|string $key |
||
| 342 | * @param string $subkey |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function languages($key = null, $subkey = null) |
||
| 365 | |||
| 366 | public function languagesForSelect() |
||
| 375 | |||
| 376 | protected function translateGetString($context, $language, $string_id) |
||
| 389 | |||
| 390 | protected function translateInsertString($context, $text) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Retreive a string to translate |
||
| 411 | * |
||
| 412 | * if it doesn't find it, put it in the database |
||
| 413 | * |
||
| 414 | * @param string $string |
||
| 415 | * @param string $context |
||
| 416 | * @param string $language |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function translate($string, $context = 'default', $language = 'default') |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the cached strings |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | public function getRawStrings() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get the page's context |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | public function getContext() |
||
| 493 | |||
| 494 | public function generate() |
||
| 517 | } |
||
| 518 |