aboutsummaryrefslogtreecommitdiffstats
path: root/include/pgettext.php
blob: c7fc5869825ef57cca19fd3b75e3ef660acbd660 (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
<?php

/**
 * translation support
 */


/**
 *
 * 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.
 *
 */


if(! function_exists('get_language')) {
function get_language() {

	if (isset($_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);
    	}
	}

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

	if(isset($preferred))
		return $preferred;

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


function push_lang($language) {
	global $lang, $a;

	$a->langsave = $lang;

	if($language === $lang)
		return;

	if(isset($a->strings) && count($a->strings)) {
		$a->stringsave = $a->strings;
	}
	$a->strings = array();
	load_translation_table($language);
	$lang = $language;
}

function pop_lang() {
	global $lang, $a;

	if($lang === $a->langsave)
		return;

	if(isset($a->stringsave))
		$a->strings = $a->stringsave;
	else
		$a->strings = array();

	$lang = $a->langsave;
}


// load string translation table for alternate language

if(! function_exists('load_translation_table')) {
function load_translation_table($lang) {
	global $a;

	if(file_exists("view/$lang/strings.php")) {
		include("view/$lang/strings.php");
	}
	else
		$a->strings = array();
}}

// translate string if translation exists

if(! function_exists('t')) {
function t($s) {

	$a = get_app();

	if(x($a->strings,$s)) {
		$t = $a->strings[$s];
		return is_array($t)?$t[0]:$t;
	}
	return $s;
}}

if(! function_exists('tt')){
function tt($singular, $plural, $count){
	global $lang;
	$a = get_app();

	if(x($a->strings,$singular)) {
		$t = $a->strings[$singular];
		$f = 'string_plural_select_' . str_replace('-','_',$lang);
		$k = $f($count);
		return is_array($t)?$t[$k]:$t;
	}
	
	if ($count!=1){
		return $plural;
	} else {
		return $singular;
	}
}}