summaryrefslogtreecommitdiff
path: root/includes/i18n.inc.php
blob: 47d68523a1be2dbb1adfee481ba62b2c346da2cb (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
<?php

if (!defined('IN_FS')) {
    die('Do not access this file directly.');
}

require_once BASEDIR . '/lang/en.php';
FlySprayI18N::init('en', $language);
FlySprayI18N::setDefault($language);

class FlySprayI18N {
    private static $translations = array();

    public static function init($lang, $translation) {
        self::$translations[$lang] = $translation;
    }

    public static function setDefault($translation) {
        self::$translations['default'] = $translation;
    }

    public static function L($key, $lang = null) {
        if (!isset($lang) || empty($lang) || !is_string($lang)) {
            $lang = 'default';
        }
        if ($lang != 'default' && $lang != 'en' && !array_key_exists($lang, self::$translations)) {
            // echo "<pre>Only once here for $lang!</pre>";
            $language = BASEDIR . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $lang . '.php';
            if (is_readable($language)) {
                if ((@require $language) !== FALSE) {
                    // echo "<pre>Loaded: $lang!</pre>";
                    self::$translations[$lang] = $translation;
                }
                else {
                    $lang = 'default';
                }
            }
            else {
                $lang = 'default';
            }
        }
        if (empty($key)) {
            return '';
        }
        if (isset(self::$translations[$lang][$key])) {
            // echo "<pre>Case 1: $lang!</pre>";
            return self::$translations[$lang][$key];
        }
        if (isset(self::$translations['default'][$key])) {
            // echo "<pre>Case 2: $lang!</pre>";
            return self::$translations['default'][$key];
        }
        if (isset(self::$translations['en'][$key])) {
            // echo "<pre>Case 3: $lang!</pre>";
            return self::$translations['en'][$key];
        }
        // echo "<pre>Case 4: $lang!". var_dump(self::$translations['en']) ."</pre>";
        return "[[$key]]";
    }
}
/**
 * get the language string $key
 * return string
 */

function L($key){
    global $language;
    if (empty($key)) {
        return '';
    }
    if (isset($language[$key])) {
        return $language[$key];
    }
    return "[[$key]]";
}

/**
 * get the language string $key in $lang
 * or current default language if $lang
 * is not given.
 * return string
 */

function tL($key, $lang = null) {
    return FlySprayI18N::L($key, $lang);
}
/**
 * html escaped variant of the previous
 * return $string
 */
function eL($key){
    return htmlspecialchars(L($key), ENT_QUOTES, 'utf-8');
}

function load_translations(){
	global $proj, $language, $user, $fs;
	# Load translations
	# if no valid lang_code, return english
	# valid == a-z and "_" case insensitive

	if (isset($user) && array_key_exists('lang_code', $user->infos)){
		$lang_code=$user->infos['lang_code'];
	}

	# 20150211 add language preferences detection of visitors
	# locale_accept_from_http() not available on every hosting, so we must parse it self.
	# TODO ..and we can loop later through $langs until we find a matching translation file
	if((!isset($lang_code) || $lang_code=='' || $lang_code=='browser') && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
		foreach( explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) {
			# taken from a php.net comment
			$pattern = '/^(?P<primarytag>[a-zA-Z]{2,8})'.
				'(?:-(?P<subtag>[a-zA-Z]{2,8}))?(?:(?:;q=)'.
				'(?P<quantifier>\d\.\d))?$/';
			$splits = array();
			if (preg_match($pattern, $lang, $splits)) {
				$langs[]=$splits;
			}
		}
		# TODO maybe sort $langs-array by quantifiers, but for most browsers it should be ok, because they sent it in right order.
		if(isset($langs)){
			$lang_code=$langs[0]['primarytag'];
			if(isset($langs[0]['subtag'])){
				$lang_code.='_'.$langs[0]['subtag']; # '_' for our language files, '-' in HTTP_ACCEPT_LANGUAGE
			}
		}
	}

	if(!isset($lang_code) || $lang_code=='' || $lang_code=='project'){
		if($proj->prefs['lang_code']){
			$lang_code = $proj->prefs['lang_code'];
		}else{
			$lang_code = 'en';
		}
	}

	if (!preg_match('/^[a-z_]+$/iD', $lang_code)) {
		$lang_code ='en';
	}

	$lang_code = strtolower($lang_code);
	$translation = BASEDIR.'/lang/'.$lang_code.'.php';
	if ($lang_code != 'en' && is_readable($translation)) {
		include_once($translation);
		$language = is_array($translation) ? array_merge($language, $translation) : $language;
                FlySprayI18N::init($lang_code, $language);
	}elseif( 'en'!=substr($lang_code, 0, strpos($lang_code, '_')) && is_readable(BASEDIR.'/lang/'.(substr($lang_code, 0, strpos($lang_code, '_'))).'.php') ){
		# fallback 'de_AT' to 'de', but not for 'en_US'
		$translation=BASEDIR.'/lang/'.(substr($lang_code, 0, strpos($lang_code, '_'))).'.php';
		include_once($translation);
		$language = is_array($translation) ? array_merge($language, $translation) : $language;    
	}

        FlySprayI18N::setDefault($language);
    // correctly translate title since language not set when initialising the project
    if (isset($proj) && !$proj->id) {
        $proj->prefs['project_title'] = L('allprojects');
        $proj->prefs['feed_description']  = L('feedforall');
    }

    for ($i = 6; $i >= 1; $i--) {
        $fs->priorities[$i] = L('priority' . $i);
    }
    for ($i = 5; $i >= 1; $i--) {
        $fs->severities[$i] = L('severity' . $i);
    }
}