aboutsummaryrefslogtreecommitdiffstats
path: root/include/language.php
blob: 72ceb67b0481c22d078db088966bf4f3f3d9b497 (plain) (blame)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
 * @file include/language.php
 *
 * @brief Translation support.
 *
 * This file contains functions to work with translations and other
 * language related tasks.
 */

use CommerceGuys\Intl\Language\LanguageRepository;

/**
 * @brief Get the browser's submitted preferred languages.
 *
 * This functions parses the HTTP_ACCEPT_LANGUAGE header sent by the browser and
 * extracts the preferred languages and their priority.
 *
 * Get the language setting directly from system variables, bypassing get_config()
 * as database may not yet be configured.
 *
 * If possible, we use the value from the browser.
 *
 * @return array with ordered list of preferred languages from browser
 */
function get_browser_language() {
	$langs = array();
	$lang_parse = array();

	if (x($_SERVER, 'HTTP_ACCEPT_LANGUAGE')) {
		// break up string into pieces (languages and q factors)
		preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
			$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);

		if (count($lang_parse[1])) {
			// create a list like "en" => 0.8
			$langs = array_combine($lang_parse[1], $lang_parse[4]);

			// set default to 1 for any without q factor
			foreach ($langs as $lang => $val) {
				if ($val === '') $langs[$lang] = 1;
			}

			// sort list based on value
			arsort($langs, SORT_NUMERIC);
		}
	}

	return $langs;
}

/**
 * @brief Returns the best language for which also a translation exists.
 *
 * This function takes the results from get_browser_language() and compares it
 * with the available translations and returns the best fitting language for
 * which there exists a translation.
 *
 * If there is no match fall back to config['system']['language']
 *
 * @return Language code in 2-letter ISO 639-1 (en).
 */
function get_best_language() {
	$langs = get_browser_language();

	if(isset($langs) && count($langs)) {
		foreach ($langs as $lang => $v) {
			$lang = strtolower($lang);
			if(is_dir("view/$lang")) {
				$preferred = $lang;
				break;
			}
		}
	}


	if(! isset($preferred)) {

		/*
		 * We could find no perfect match for any of the preferred languages. 
		 * For cases where the preference is fr-fr and we have fr but *not* fr-fr
		 * run the test again and only look for the language base
		 * which should provide an interface they can sort of understand
		 */

		if(isset($langs) && count($langs)) {
			foreach ($langs as $lang => $v) {
				if(strlen($lang) ===  2) {
					/* we have already checked this language */
					continue;
				}
				/* Check the base */
				$lang = strtolower(substr($lang,0,2));
				if(is_dir("view/$lang")) {
					$preferred = $lang;
					break;
				}
			}
		}
	}

	if(! isset($preferred)) {
		$preferred = 'unset';
	}

	$arr = array('langs' => $langs, 'preferred' => $preferred);

	call_hooks('get_best_language',$arr);

	if($arr['preferred'] !== 'unset')
		return $arr['preferred'];

	return ((isset(App::$config['system']['language'])) ? App::$config['system']['language'] : 'en');
}

/*
 * push_lang and pop_lang let you temporarily override the default language.
 * Often used to email the administrator during a session created in another language.
 * The stack is one level deep - so you must pop after every push.
 */


function push_lang($language) {

	App::$langsave = App::$language;

	if($language === App::$language)
		return;

	if(isset(App::$strings) && count(App::$strings)) {
		App::$stringsave = App::$strings;
	}
	App::$strings = array();
	load_translation_table($language);
	App::$language = $language;
}

function pop_lang() {

	if(App::$language === App::$langsave)
		return;

	if(isset(App::$stringsave) && is_array(App::$stringsave))
		App::$strings = App::$stringsave;
	else
		App::$strings = array();

	App::$language = App::$langsave;
}

/**
 * @brief Load string translation table for alternate language.
 *
 * @param string $lang language code in 2-letter ISO 639-1 (en, de, fr) format
 * @param boolean $install (optional) default false
 */
function load_translation_table($lang, $install = false) {

	App::$strings = array();

	if(file_exists("view/$lang/hstrings.php")) {
		include("view/$lang/hstrings.php");
	}
	elseif(file_exists("view/$lang/strings.php")) {
		include("view/$lang/strings.php");
	}

	if(! $install) {
		$plugins = q("SELECT aname FROM addon WHERE installed=1;");
		if ($plugins !== false) {
			foreach($plugins as $p) {
				$name = $p['aname'];
				if(file_exists("addon/$name/lang/$lang/hstrings.php")) {
					include("addon/$name/lang/$lang/hstrings.php");
				}
				elseif(file_exists("addon/$name/lang/$lang/strings.php")) {
					include("addon/$name/lang/$lang/strings.php");
				}
			}
		}
	}

	// Allow individual strings to be over-ridden on this site
	// Either for the default language or for all languages

	if(file_exists("view/local-$lang/hstrings.php")) {
		include("view/local-$lang/hstrings.php");
	}
	elseif(file_exists("view/local-$lang/strings.php")) {
		include("view/local-$lang/strings.php");
	}
}

/**
 * @brief Translate string if translation exists.
 *
 * @param string $s string that should get translated
 * @param string $ctx (optional) context to appear in po file
 * @return translated string if exists, otherwise return $s
 *
 */
function t($s, $ctx = '') {

	$cs = $ctx ? '__ctx:' . $ctx . '__ ' . $s : $s;
	if (x(App::$strings, $cs)) {
		$t = App::$strings[$cs];

		return ((is_array($t)) ? translate_projectname($t[0]) : translate_projectname($t));
	}

	return translate_projectname($s);
}

/**
 * @brief translate product name
 *  Merging strings from different project names is problematic so we'll do that with a string replacement
 */

function translate_projectname($s) {

	return str_replace(array('$projectname','$Projectname'),array(Zotlabs\Lib\System::get_platform_name(),ucfirst(Zotlabs\Lib\System::get_platform_name())),$s);

}



/**
 * @brief
 *
 * @param string $singular
 * @param string $plural
 * @param int $count
 * @param string $ctx
 * @return string
 */
function tt($singular, $plural, $count, $ctx = ''){

	$cs = $ctx ? "__ctx:" . $ctx . "__ " . $singular : $singular;
	if (x(App::$strings,$cs)) {
		$t = App::$strings[$cs];
		$f = 'string_plural_select_' . str_replace('-', '_', App::$language);
		if (! function_exists($f))
			$f = 'string_plural_select_default';

		$k = $f(intval($count));

		return is_array($t) ? $t[$k] : $t;
	}

	if ($count != 1) {
		return $plural;
	} else {
		return $singular;
	}
}

/**
 * @brief Return slash separated string of plurals translation form
 *
 * @param string $k key in translations array
 * @return string
 */
function ta($k){

        $t = App::$strings[$k];
        if (is_array($t)) {
                return implode("/", $t);
        } else {
                return $k;
        }
}

/**
 * @brief Return string_plural_select_xx function code
 *
 * @return string
 */
function tf() {

        $r = "";
        $lang = str_replace('-', '_', App::$language);
        $file = "view/$lang/hstrings.php";
        if(file_exists($file))
                $r = trim(shell_exec("sed -n '5p' ".$file." | tr -d '$'"));
        return $r != "" ? $r : "return 0;";
}

/**
 * @brief Provide a fallback which will not collide with a function defined in
 * any language file.
 *
 * @param int $n
 * @return boolean
 */
function string_plural_select_default($n) {
	return ($n != 1);
}

/**
 * @brief Takes a string and tries to identify the language.
 *
 * It uses the pear library Text_LanguageDetect and it can identify 52 human languages.
 * It returns the identified languges and a confidence score for each.
 *
 * Strings need to have a min length config['system']['language_detect_min_length']
 * and you can influence the confidence that must be met before a result will get
 * returned through config['system']['language_detect_min_confidence'].
 *
 * @see http://pear.php.net/package/Text_LanguageDetect
 * @param string $s A string to examine
 * @return string Language code in 2-letter ISO 639-1 (en, de, fr) format
 */
function detect_language($s) {
	$min_length = get_config('system', 'language_detect_min_length');
	if ($min_length === false)
		$min_length = LANGUAGE_DETECT_MIN_LENGTH;

	$min_confidence = get_config('system', 'language_detect_min_confidence');
	if ($min_confidence === false)
		$min_confidence = LANGUAGE_DETECT_MIN_CONFIDENCE;

	// embedded apps have long base64 strings which will trip up the detector.
	$naked_body = preg_replace('/\[app\](.*?)\[\/app\]/', '', $s);
	// strip off bbcode
	$naked_body = preg_replace('/\[(.+?)\]/', '', $naked_body);
	if (mb_strlen($naked_body) < intval($min_length)) {
		logger('string length less than ' . intval($min_length), LOGGER_DATA);
		return '';
	}

	$l = new Text_LanguageDetect;
	try {
		// return 2-letter ISO 639-1 (en) language code
		$l->setNameMode(2);
		$lng = $l->detectConfidence($naked_body);
		logger('detect language: ' . print_r($lng, true) . $naked_body, LOGGER_DATA);
	} catch (Text_LanguageDetect_Exception $e) {
		logger('detect language exception: ' . $e->getMessage(), LOGGER_DATA);
	}

	if ((! $lng) || (! (x($lng,'language')))) {
		return '';
	}

	if ($lng['confidence'] < (float) $min_confidence) {
		logger('detect language: confidence less than ' . (float) $min_confidence, LOGGER_DATA);
		return '';
	}

	return($lng['language']);
}

/**
 * @brief Returns the display name of a given language code.
 *
 * By default we use the localized language name. You can switch the result
 * to any language with the optional 2nd parameter $l.
 *
 * $s and $l should be in 2-letter ISO 639-1 format.
 *
 * If nothing could be looked up it returns $s.
 *
 * @param string $s Language code to look up
 * @param string $l (optional) In which language to return the name
 * @return string with the language name, or $s if unrecognized
 */
function get_language_name($s, $l = null) {
	// get() expects the second part to be in upper case
	if (strpos($s, '-') !== false) $s = substr($s, 0, 2) . strtoupper(substr($s, 2));
	if ($l !== null && strpos($l, '-') !== false) $l = substr($l, 0, 2) . strtoupper(substr($l, 2));

	$languageRepository = new LanguageRepository;

	// Sometimes intl doesn't like the second part at all ...
	try {
		$language = $languageRepository->get($s, $l);
	} catch(CommerceGuys\Intl\Exception\UnknownLanguageException $e) {
		$s = substr($s, 0, 2);
		if($l !== null) $l = substr($s, 0, 2);
		try {
			$language = $languageRepository->get($s, $l);
		} catch (CommerceGuys\Intl\Exception\UnknownLanguageException $e) {
			return $s; // Give up
		} catch (CommerceGuys\Intl\Exception\UnknownLocaleException $e) {
			return $s; // Give up
		}
	}

	return $language->getName();
}



function language_list() {

	$langs = glob('view/*/hstrings.php');

	$lang_options = array();
	$selected = "";

	if(is_array($langs) && count($langs)) {
		if(! in_array('view/en/hstrings.php',$langs))
			$langs[] = 'view/en/';
		asort($langs);
		foreach($langs as $l) {
			$ll = substr($l,5);
			$ll = substr($ll,0,strrpos($ll,'/'));
			$lang_options[$ll] = get_language_name($ll, $ll) . " ($ll)";
		}
	}
	return $lang_options;
}

function lang_selector() {

	$langs = glob('view/*/hstrings.php');

	$lang_options = array();
	$selected = "";

	if(is_array($langs) && count($langs)) {
		$langs[] = '';
		if(! in_array('view/en/hstrings.php',$langs))
			$langs[] = 'view/en/';
		asort($langs);
		foreach($langs as $l) {
			if($l == '') {
				$lang_options[""] = t('default');
				continue;
			}
			$ll = substr($l,5);
			$ll = substr($ll,0,strrpos($ll,'/'));
			$selected = (($ll === App::$language && (x($_SESSION, 'language'))) ? $ll : $selected);
			$lang_options[$ll] = get_language_name($ll, $ll) . " ($ll)";
		}
	}

	$tpl = get_markup_template('lang_selector.tpl');

	$o = replace_macros($tpl, array(
		'$title' => t('Select an alternate language'),
		'$langs' => array($lang_options, $selected),

	));

	return $o;
}