| Total Complexity | 58 | 
| Total Lines | 428 | 
| Duplicated Lines | 0 % | 
| Changes | 2 | ||
| Bugs | 2 | Features | 0 | 
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.
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 | ||
| 6 | class Language { | ||
| 7 | private $languages = ["en_US.UTF-8" => "English"]; | ||
| 8 | private $lang; | ||
| 9 | private $loaded = false; | ||
| 10 | |||
| 11 | /** | ||
| 12 | * Default constructor. | ||
| 13 | * | ||
| 14 | * By default, the Language class only knows about en_GB (English). If you want more languages, you | ||
| 15 | * must call loadLanguages(). | ||
| 16 | */ | ||
| 17 | 	public function __construct() {} | ||
| 18 | |||
| 19 | /** | ||
| 20 | * Loads languages from disk. | ||
| 21 | * | ||
| 22 | * loadLanguages() reads the languages from disk by reading LANGUAGE_DIR and opening all directories | ||
| 23 | * in that directory. Each directory must contain a 'language.txt' file containing: | ||
| 24 | * | ||
| 25 | * <language display name> | ||
| 26 | * <win32 language name> | ||
| 27 | * | ||
| 28 | * For example: | ||
| 29 | * <code> | ||
| 30 | * Nederlands | ||
| 31 | * nld_NLD | ||
| 32 | * </code> | ||
| 33 | * | ||
| 34 | * Also, the directory names must have a name that is: | ||
| 35 | * 1. Available to the server's locale system | ||
| 36 | * 2. In the UTF-8 charset | ||
| 37 | * | ||
| 38 | * For example, nl_NL.UTF-8 | ||
| 39 | */ | ||
| 40 | 	public function loadLanguages() { | ||
| 41 | 		if ($this->loaded) { | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | |||
| 45 | 		$languages = explode(";", ENABLED_LANGUAGES); | ||
| 46 | $dh = opendir(LANGUAGE_DIR); | ||
| 47 | 		while (($entry = readdir($dh)) !== false) { | ||
| 48 | 			$langcode = str_ireplace(".UTF-8", "", $entry); | ||
| 49 | 			if (in_array($langcode, $languages) || in_array($entry, $languages)) { | ||
| 50 | 				if (is_dir(LANGUAGE_DIR . $entry . "/LC_MESSAGES") && is_file(LANGUAGE_DIR . $entry . "/language.txt")) { | ||
| 51 | $fh = fopen(LANGUAGE_DIR . $entry . "/language.txt", "r"); | ||
| 52 | $lang_title = fgets($fh); | ||
| 53 | fclose($fh); | ||
| 54 | 					$this->languages[$entry] = "{$langcode}: " . trim($lang_title); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | asort($this->languages, SORT_LOCALE_STRING); | ||
| 59 | $this->loaded = true; | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Attempt to set language. | ||
| 64 | * | ||
| 65 | * setLanguage attempts to set the language to the specified language. The language passed | ||
| 66 | * is the name of the directory containing the language. | ||
| 67 | * | ||
| 68 | * For setLanguage() to succeed, the language has to have been loaded via loadLanguages() AND | ||
| 69 | * the gettext system must 'know' the language specified. | ||
| 70 | * | ||
| 71 | * @param string $lang Language code (eg nl_NL.UTF-8) | ||
| 72 | */ | ||
| 73 | 	public function setLanguage($lang) { | ||
| 74 | 		if (isset($GLOBALS['translations'])) { | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | $lang = (empty($lang) || substr($lang, 0, 1) == '.' || $lang == "C") ? LANG : $lang; // default language fix | ||
| 78 | |||
| 79 | 		if ($this->is_language($lang)) { | ||
| 80 | $this->lang = $lang; | ||
| 81 | $tmp_translations = $this->getTranslations(); | ||
| 82 | $translations = []; | ||
| 83 | 			foreach ($tmp_translations as $resources) { | ||
| 84 | $resourcesCount = count($resources); | ||
| 85 | 				for ($i = 0; $i < $resourcesCount; ++$i) { | ||
| 86 | $msgid = $resources[$i]['msgid']; | ||
| 87 | 					if (isset($msgid)) { | ||
| 88 | $translations[$msgid] = $resources[$i]['msgstr']; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | $GLOBALS['translations'] = $translations; | ||
| 93 | } | ||
| 94 | 		else { | ||
| 95 | 			error_log(sprintf("Unknown language: '%s'", $lang)); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | 	public static function getstring($string) { | ||
| 100 | 		if (isset($GLOBALS['translations'], $GLOBALS['translations'][$string])) { | ||
| 101 | return $GLOBALS['translations'][$string]; | ||
| 102 | } | ||
| 103 | |||
| 104 | return $string; | ||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Return a list of supported languages. | ||
| 109 | * | ||
| 110 | * Returns an associative array in the format langid -> langname, for example "nl_NL.utf8" -> "Nederlands" | ||
| 111 | * | ||
| 112 | * @return array List of supported languages | ||
| 113 | */ | ||
| 114 | 	public function getLanguages() { | ||
| 115 | $this->loadLanguages(); | ||
| 116 | |||
| 117 | return $this->languages; | ||
| 118 | } | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Returns the $getLanguages and formats in JSON so it can be parsed | ||
| 122 | * by the javascript. | ||
| 123 | * | ||
| 124 | * @return string The javascript string | ||
| 125 | */ | ||
| 126 | 	public function getJSON() { | ||
| 127 | $json = []; | ||
| 128 | $languages = $this->getLanguages(); | ||
| 129 | 		foreach ($languages as $key => $lang) { | ||
| 130 | $json[] = [ | ||
| 131 | "lang" => $key, | ||
| 132 | "name" => $lang, | ||
| 133 | ]; | ||
| 134 | } | ||
| 135 | |||
| 136 | return json_encode($json); | ||
| 137 | } | ||
| 138 | |||
| 139 | /** | ||
| 140 | * Returns the ID of the currently selected language. | ||
| 141 | * | ||
| 142 | * @return string ID of selected language | ||
| 143 | */ | ||
| 144 | 	public function getSelected() { | ||
| 146 | } | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Returns if the specified language is valid or not. | ||
| 150 | * | ||
| 151 | * @param string $lang | ||
| 152 | * | ||
| 153 | * @return bool TRUE if the language is valid | ||
| 154 | */ | ||
| 155 | 	public function is_language($lang) { | ||
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Returns the resolved language code, i.e. ending on UTF-8. | ||
| 161 | * Examples: | ||
| 162 | * - en_GB => en.GB.UTF-8 | ||
| 163 | * - en_GB.utf8 => en_GB.UTF-8 | ||
| 164 | * - en_GB.UTF-8 => en_GB.UTF-8 (no changes). | ||
| 165 | * | ||
| 166 | * @param string $lang language code to resolve | ||
| 167 | * | ||
| 168 | * @return string resolved language name (i.e. language code ending on .UTF-8). | ||
| 169 | */ | ||
| 170 | 	public static function resolveLanguage($lang) { | ||
| 184 | } | ||
| 185 | |||
| 186 | 	public function getTranslations() { | ||
| 254 | } | ||
| 255 | |||
| 256 | /** | ||
| 257 | * getTranslationsFromFile. | ||
| 258 | * | ||
| 259 | * This file reads the translations from the binary .mo file and returns | ||
| 260 | * them in an array containing the original and the translation variant. | ||
| 261 | * The .mo file format is described on the following URL. | ||
| 262 | * http://www.gnu.org/software/gettext/manual/gettext.html#MO-Files | ||
| 263 | * | ||
| 264 | * byte | ||
| 265 | * +------------------------------------------+ | ||
| 266 | * 0 | magic number = 0x950412de | | ||
| 267 | * | | | ||
| 268 | * 4 | file format revision = 0 | | ||
| 269 | * | | | ||
| 270 | * 8 | number of strings | == N | ||
| 271 | * | | | ||
| 272 | * 12 | offset of table with original strings | == O | ||
| 273 | * | | | ||
| 274 | * 16 | offset of table with translation strings | == T | ||
| 275 | * | | | ||
| 276 | * 20 | size of hashing table | == S | ||
| 277 | * | | | ||
| 278 | * 24 | offset of hashing table | == H | ||
| 279 | * | | | ||
| 280 | * . . | ||
| 281 | * . (possibly more entries later) . | ||
| 282 | * . . | ||
| 283 | * | | | ||
| 284 | * O | length & offset 0th string ----------------. | ||
| 285 | * O + 8 | length & offset 1st string ------------------. | ||
| 286 | * ... ... | | | ||
| 287 | * O + ((N-1)*8)| length & offset (N-1)th string | | | | ||
| 288 | * | | | | | ||
| 289 | * T | length & offset 0th translation ---------------. | ||
| 290 | * T + 8 | length & offset 1st translation -----------------. | ||
| 291 | * ... ... | | | | | ||
| 292 | * T + ((N-1)*8)| length & offset (N-1)th translation | | | | | | ||
| 293 | * | | | | | | | ||
| 294 | * H | start hash table | | | | | | ||
| 295 | * ... ... | | | | | ||
| 296 | * H + S * 4 | end hash table | | | | | | ||
| 297 | * | | | | | | | ||
| 298 | * | NUL terminated 0th string <----------------' | | | | ||
| 299 | * | | | | | | ||
| 300 | * | NUL terminated 1st string <------------------' | | | ||
| 301 | * | | | | | ||
| 302 | * ... ... | | | ||
| 303 | * | | | | | ||
| 304 | * | NUL terminated 0th translation <---------------' | | ||
| 305 | * | | | | ||
| 306 | * | NUL terminated 1st translation <-----------------' | ||
| 307 | * | | | ||
| 308 | * ... ... | ||
| 309 | * | | | ||
| 310 | * +------------------------------------------+ | ||
| 311 | * | ||
| 312 | * @param $filename string Name of the .mo file. | ||
| 313 | * | ||
| 314 | * @return array|bool false when file is missing otherwise array with | ||
| 315 | * translations | ||
| 316 | */ | ||
| 317 | 	public function getTranslationsFromFile($filename) { | ||
| 434 | } | ||
| 435 | } | ||
| 436 |