Total Complexity | 62 |
Total Lines | 438 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 5 | 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) || str_starts_with($lang, '.') || $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() { |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * getTranslationsFromFile. |
||
268 | * |
||
269 | * This file reads the translations from the binary .mo file and returns |
||
270 | * them in an array containing the original and the translation variant. |
||
271 | * The .mo file format is described on the following URL. |
||
272 | * http://www.gnu.org/software/gettext/manual/gettext.html#MO-Files |
||
273 | * |
||
274 | * byte |
||
275 | * +------------------------------------------+ |
||
276 | * 0 | magic number = 0x950412de | |
||
277 | * | | |
||
278 | * 4 | file format revision = 0 | |
||
279 | * | | |
||
280 | * 8 | number of strings | == N |
||
281 | * | | |
||
282 | * 12 | offset of table with original strings | == O |
||
283 | * | | |
||
284 | * 16 | offset of table with translation strings | == T |
||
285 | * | | |
||
286 | * 20 | size of hashing table | == S |
||
287 | * | | |
||
288 | * 24 | offset of hashing table | == H |
||
289 | * | | |
||
290 | * . . |
||
291 | * . (possibly more entries later) . |
||
292 | * . . |
||
293 | * | | |
||
294 | * O | length & offset 0th string ----------------. |
||
295 | * O + 8 | length & offset 1st string ------------------. |
||
296 | * ... ... | | |
||
297 | * O + ((N-1)*8)| length & offset (N-1)th string | | | |
||
298 | * | | | | |
||
299 | * T | length & offset 0th translation ---------------. |
||
300 | * T + 8 | length & offset 1st translation -----------------. |
||
301 | * ... ... | | | | |
||
302 | * T + ((N-1)*8)| length & offset (N-1)th translation | | | | | |
||
303 | * | | | | | | |
||
304 | * H | start hash table | | | | | |
||
305 | * ... ... | | | | |
||
306 | * H + S * 4 | end hash table | | | | | |
||
307 | * | | | | | | |
||
308 | * | NUL terminated 0th string <----------------' | | | |
||
309 | * | | | | | |
||
310 | * | NUL terminated 1st string <------------------' | | |
||
311 | * | | | | |
||
312 | * ... ... | | |
||
313 | * | | | | |
||
314 | * | NUL terminated 0th translation <---------------' | |
||
315 | * | | | |
||
316 | * | NUL terminated 1st translation <-----------------' |
||
317 | * | | |
||
318 | * ... ... |
||
319 | * | | |
||
320 | * +------------------------------------------+ |
||
321 | * |
||
322 | * @param $filename string Name of the .mo file. |
||
323 | * |
||
324 | * @return array|bool false when file is missing otherwise array with |
||
325 | * translations |
||
326 | */ |
||
327 | public function getTranslationsFromFile($filename) { |
||
446 |