aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/languages.php
blob: 61ba91ddbca59166fd3c62a7112de824a733824f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
 * Elgg language module
 * Functions to manage language and translations.
 *
 * @package Elgg.Core
 * @subpackage Languages
 */

/**
 * Given a message key, returns an appropriately translated full-text string
 *
 * @param string $message_key The short message code
 * @param array  $args        An array of arguments to pass through vsprintf().
 * @param string $language    Optionally, the standard language code
 *                            (defaults to site/user default, then English)
 *
 * @return string Either the translated string, the English string,
 * or the original language string.
 */
function elgg_echo($message_key, $args = array(), $language = "") {
	global $CONFIG;

	static $CURRENT_LANGUAGE;

	// old param order is deprecated
	if (!is_array($args)) {
		elgg_deprecated_notice(
			'As of Elgg 1.8, the 2nd arg to elgg_echo() is an array of string replacements and the 3rd arg is the language.',
			1.8
		);

		$language = $args;
		$args = array();
	}

	if (!isset($CONFIG->translations)) {
		// this means we probably had an exception before translations were initialized
		register_translations(dirname(dirname(dirname(__FILE__))) . "/languages/");
	}

	if (!$CURRENT_LANGUAGE) {
		$CURRENT_LANGUAGE = get_language();
	}
	if (!$language) {
		$language = $CURRENT_LANGUAGE;
	}

	if (isset($CONFIG->translations[$language][$message_key])) {
		$string = $CONFIG->translations[$language][$message_key];
	} else if (isset($CONFIG->translations["en"][$message_key])) {
		$string = $CONFIG->translations["en"][$message_key];
		$lang = $CONFIG->translations["en"][$language];
		elgg_log(sprintf('Missing %s translation for "%s" language key', $lang, $message_key), 'NOTICE');
	} else {
		$string = $message_key;
		elgg_log(sprintf('Missing English translation for "%s" language key', $message_key), 'NOTICE');
	}

	// only pass through if we have arguments to allow backward compatibility
	// with manual sprintf() calls.
	if ($args) {
		$string = vsprintf($string, $args);
	}

	return $string;
}

/**
 * Add a translation.
 *
 * Translations are arrays in the Zend Translation array format, eg:
 *
 *	$english = array('message1' => 'message1', 'message2' => 'message2');
 *  $german = array('message1' => 'Nachricht1','message2' => 'Nachricht2');
 *
 * @param string $country_code   Standard country code (eg 'en', 'nl', 'es')
 * @param array  $language_array Formatted array of strings
 *
 * @return bool Depending on success
 */
function add_translation($country_code, $language_array) {
	global $CONFIG;
	if (!isset($CONFIG->translations)) {
		$CONFIG->translations = array();
	}

	$country_code = strtolower($country_code);
	$country_code = trim($country_code);
	if (is_array($language_array) && sizeof($language_array) > 0 && $country_code != "") {
		if (!isset($CONFIG->translations[$country_code])) {
			$CONFIG->translations[$country_code] = $language_array;
		} else {
			$CONFIG->translations[$country_code] = $language_array + $CONFIG->translations[$country_code];
		}
		return true;
	}
	return false;
}

/**
 * Detect the current language being used by the current site or logged in user.
 *
 * @return string The language code for the site/user or "en" if not set
 */
function get_current_language() {
	$language = get_language();

	if (!$language) {
		$language = 'en';
	}

	return $language;
}

/**
 * Gets the current language in use by the system or user.
 *
 * @return string The language code (eg "en") or false if not set
 */
function get_language() {
	global $CONFIG;

	$user = elgg_get_logged_in_user_entity();
	$language = false;

	if (($user) && ($user->language)) {
		$language = $user->language;
	}

	if ((!$language) && (isset($CONFIG->language)) && ($CONFIG->language)) {
		$language = $CONFIG->language;
	}

	if ($language) {
		return $language;
	}

	return false;
}

/**
 * @access private
 */
function _elgg_load_translations() {
	global $CONFIG;

	if ($CONFIG->system_cache_enabled) {
		$loaded = true;
		$languages = array_unique(array('en', get_current_language()));
		foreach ($languages as $language) {
			$data = elgg_load_system_cache("$language.lang");
			if ($data) {
				add_translation($language, unserialize($data));
			} else {
				$loaded = false;
			}
		}

		if ($loaded) {
			$CONFIG->i18n_loaded_from_cache = true;
			// this is here to force 
			$CONFIG->language_paths[dirname(dirname(dirname(__FILE__))) . "/languages/"] = true;
			return;
		}
	}

	// load core translations from languages directory
	register_translations(dirname(dirname(dirname(__FILE__))) . "/languages/");
}



/**
 * When given a full path, finds translation files and loads them
 *
 * @param string $path     Full path
 * @param bool   $load_all If true all languages are loaded, if
 *                         false only the current language + en are loaded
 *
 * @return bool success
 */
function register_translations($path, $load_all = false) {
	global $CONFIG;

	$path = sanitise_filepath($path);

	// Make a note of this path just incase we need to register this language later
	if (!isset($CONFIG->language_paths)) {
		$CONFIG->language_paths = array();
	}
	$CONFIG->language_paths[$path] = true;

	// Get the current language based on site defaults and user preference
	$current_language = get_current_language();
	elgg_log("Translations loaded from: $path");

	// only load these files unless $load_all is true.
	$load_language_files = array(
		'en.php',
		"$current_language.php"
	);

	$load_language_files = array_unique($load_language_files);

	$handle = opendir($path);
	if (!$handle) {
		elgg_log("Could not open language path: $path", 'ERROR');
		return false;
	}

	$return = true;
	while (false !== ($language = readdir($handle))) {
		// ignore bad files
		if (substr($language, 0, 1) == '.' || substr($language, -4) !== '.php') {
			continue;
		}

		if (in_array($language, $load_language_files) || $load_all) {
			if (!include_once($path . $language)) {
				$return = false;
				continue;
			}
		}
	}

	return $return;
}

/**
 * Reload all translations from all registered paths.
 *
 * This is only called by functions which need to know all possible translations.
 *
 * @todo Better on demand loading based on language_paths array
 *
 * @return void
 */
function reload_all_translations() {
	global $CONFIG;

	static $LANG_RELOAD_ALL_RUN;
	if ($LANG_RELOAD_ALL_RUN) {
		return;
	}

	if ($CONFIG->i18n_loaded_from_cache) {
		$cache = elgg_get_system_cache();
		$cache_dir = $cache->getVariable("cache_path");
		$filenames = elgg_get_file_list($cache_dir, array(), array(), array(".lang"));
		foreach ($filenames as $filename) {
			if (preg_match('/([a-z]+)\.[^.]+$/', $filename, $matches)) {
				$language = $matches[1];
				$data = elgg_load_system_cache("$language.lang");
				if ($data) {
					add_translation($language, unserialize($data));
				}
			}
		}
	} else {
		foreach ($CONFIG->language_paths as $path => $dummy) {
			register_translations($path, true);
		}
	}

	$LANG_RELOAD_ALL_RUN = true;
}

/**
 * Return an array of installed translations as an associative
 * array "two letter code" => "native language name".
 *
 * @return array
 */
function get_installed_translations() {
	global $CONFIG;

	// Ensure that all possible translations are loaded
	reload_all_translations();

	$installed = array();

	foreach ($CONFIG->translations as $k => $v) {
		$installed[$k] = elgg_echo($k, array(), $k);
		if (elgg_is_admin_logged_in()) {
			$completeness = get_language_completeness($k);
			if (($completeness < 100) && ($k != 'en')) {
				$installed[$k] .= " (" . $completeness . "% " . elgg_echo('complete') . ")";
			}
		}
	}

	return $installed;
}

/**
 * Return the level of completeness for a given language code (compared to english)
 *
 * @param string $language Language
 *
 * @return int
 */
function get_language_completeness($language) {
	global $CONFIG;

	// Ensure that all possible translations are loaded
	reload_all_translations();

	$language = sanitise_string($language);

	$en = count($CONFIG->translations['en']);

	$missing = get_missing_language_keys($language);
	if ($missing) {
		$missing = count($missing);
	} else {
		$missing = 0;
	}

	//$lang = count($CONFIG->translations[$language]);
	$lang = $en - $missing;

	return round(($lang / $en) * 100, 2);
}

/**
 * Return the translation keys missing from a given language,
 * or those that are identical to the english version.
 *
 * @param string $language The language
 *
 * @return mixed
 */
function get_missing_language_keys($language) {
	global $CONFIG;

	// Ensure that all possible translations are loaded
	reload_all_translations();

	$missing = array();

	foreach ($CONFIG->translations['en'] as $k => $v) {
		if ((!isset($CONFIG->translations[$language][$k]))
		|| ($CONFIG->translations[$language][$k] == $CONFIG->translations['en'][$k])) {
			$missing[] = $k;
		}
	}

	if (count($missing)) {
		return $missing;
	}

	return false;
}