Passed
Pull Request — development (#3540)
by Emanuele
07:01
created

Loader::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * This class takes care of loading language files
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 dev
11
 *
12
 */
13
14
namespace ElkArte\Languages;
15
16
use ElkArte\Debug;
17
18
/**
19
 * This class takes care of loading language files
20
 */
21
class Loader
22
{
23
	/** @var string */
24
	protected $path = '';
25
26
	/** @var string */
27
	protected $language = 'English';
28
29
	/** @var bool */
30
	protected $load_fallback = true;
31
32
	/** @var mixed[] */
33
	protected $variable = true;
34
35
	/** @var string[] Holds the name of the files already loaded to load them only once */
36
	protected $loaded = [];
37
38
	public function __construct($lang = null, &$variable)
39
	{
40
		if ($lang !== null)
41
		{
42
			$this->language = ucfirst($lang);
43
		}
44
45
		$this->path = SOURCEDIR . '/ElkArte/Languages/';
46
47
		$this->variable = &$variable;
48
		if (empty($this->variable))
49
		{
50
			$this->variable = [];
51
		}
52
	}
53
54
	public function setFallback(bool $new_Status)
55
	{
56
		$this->load_fallback = $new_Status;
57
	}
58
59
	public function changePath($path)
60
	{
61
		$this->path = $path;
62
	}
63
64
	public function load($file_name, $fatal = true, $fix_calendar_arrays = false)
65
	{
66
		global $db_show_debug;
67
68
		$file_names = explode('+', $file_name);
69
70
		// For each file open it up and write it out!
71
		foreach ($file_names as $file)
72
		{
73
			$file = ucfirst($file);
74
			if (isset($this->loaded[$file]))
75
			{
76
				continue;
77
			}
78
79
			$found = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $found is dead and can be removed.
Loading history...
80
			$found_fallback = false;
81
			if ($this->load_fallback)
82
			{
83
				$found_fallback = $this->loadFile($file, 'English');
84
			}
85
			$found = $this->loadFile($file, $this->language);
86
87
			$this->loaded[$file] = true;
88
89
			// Keep track of what we're up to, soldier.
90
			if ($found && $db_show_debug === true)
91
			{
92
				Debug::instance()->add(
93
					'language_files',
94
					$file . '.' . $this->language .
95
					' (' . str_replace(BOARDDIR, '', $this->path) . ')'
96
				);
97
			}
98
99
			// That couldn't be found!  Log the error, but *try* to continue normally.
100
			if (!$found && $fatal)
101
			{
102
				Errors::instance()->log_error(
0 ignored issues
show
Bug introduced by
The type ElkArte\Languages\Errors was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
					sprintf(
104
						$txt['theme_language_error'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $txt seems to be never defined.
Loading history...
105
						$file . '.' . $this->language,
106
						'template'
107
					)
108
				);
109
				// If we do have a fallback it may not be necessary to break out.
110
				if ($found_fallback === false)
111
				{
112
					break;
113
				}
114
			}
115
		}
116
117
		if ($fix_calendar_arrays)
118
		{
119
			$this->fix_calendar_text();
120
		}
121
	}
122
123
	protected function loadFile($name, $lang)
124
	{
125
		$filepath = $this->path . $name . '/' . $lang . '.php';
126
		if (file_exists($filepath))
127
		{
128
			require($filepath);
129
			if (!empty($txt))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $txt seems to never exist and therefore empty should always be true.
Loading history...
130
			{
131
				$this->variable += $txt;
132
			}
133
			// Temporary solution (I think);
134
			if (!empty($txtBirthdayEmails))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $txtBirthdayEmails seems to never exist and therefore empty should always be true.
Loading history...
135
			{
136
				$this->variable['$txtBirthdayEmails'] = $txtBirthdayEmails;
137
			}
138
			return true;
139
		}
140
		return false;
141
	}
142
143
	/**
144
	 * Loads / Sets arrays for use in date display
145
	 * This is here and not in a language file for two reasons:
146
	 *  1. the structure is required by the code, so better be sure
147
	 *     to have it the way we are supposed to have it
148
	 *  2. Transifex (that we use for translating the strings) doesn't
149
	 *     support array of arrays, so if we move this to a language file
150
	 *     we'd need to move away from Tx.
151
	 */
152
	protected function fix_calendar_text()
153
	{
154
		global $txt;
155
156
		$txt['days'] = array(
157
			$txt['sunday'],
158
			$txt['monday'],
159
			$txt['tuesday'],
160
			$txt['wednesday'],
161
			$txt['thursday'],
162
			$txt['friday'],
163
			$txt['saturday'],
164
		);
165
		$txt['days_short'] = array(
166
			$txt['sunday_short'],
167
			$txt['monday_short'],
168
			$txt['tuesday_short'],
169
			$txt['wednesday_short'],
170
			$txt['thursday_short'],
171
			$txt['friday_short'],
172
			$txt['saturday_short'],
173
		);
174
		$txt['months'] = array(
175
			1 => $txt['january'],
176
			$txt['february'],
177
			$txt['march'],
178
			$txt['april'],
179
			$txt['may'],
180
			$txt['june'],
181
			$txt['july'],
182
			$txt['august'],
183
			$txt['september'],
184
			$txt['october'],
185
			$txt['november'],
186
			$txt['december'],
187
		);
188
		$txt['months_titles'] = array(
189
			1 => $txt['january_titles'],
190
			$txt['february_titles'],
191
			$txt['march_titles'],
192
			$txt['april_titles'],
193
			$txt['may_titles'],
194
			$txt['june_titles'],
195
			$txt['july_titles'],
196
			$txt['august_titles'],
197
			$txt['september_titles'],
198
			$txt['october_titles'],
199
			$txt['november_titles'],
200
			$txt['december_titles'],
201
		);
202
		$txt['months_short'] = array(
203
			1 => $txt['january_short'],
204
			$txt['february_short'],
205
			$txt['march_short'],
206
			$txt['april_short'],
207
			$txt['may_short'],
208
			$txt['june_short'],
209
			$txt['july_short'],
210
			$txt['august_short'],
211
			$txt['september_short'],
212
			$txt['october_short'],
213
			$txt['november_short'],
214
			$txt['december_short'],
215
		);
216
	}
217
}