1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* ***************************************************************************** |
5
|
|
|
* Contributions to this work were made on behalf of the GÉANT project, a |
6
|
|
|
* project that has received funding from the European Union’s Framework |
7
|
|
|
* Programme 7 under Grant Agreements No. 238875 (GN3) and No. 605243 (GN3plus), |
8
|
|
|
* Horizon 2020 research and innovation programme under Grant Agreements No. |
9
|
|
|
* 691567 (GN4-1) and No. 731122 (GN4-2). |
10
|
|
|
* On behalf of the aforementioned projects, GEANT Association is the sole owner |
11
|
|
|
* of the copyright in all material which was developed by a member of the GÉANT |
12
|
|
|
* project. GÉANT Vereniging (Association) is registered with the Chamber of |
13
|
|
|
* Commerce in Amsterdam with registration number 40535155 and operates in the |
14
|
|
|
* UK as a branch of GÉANT Vereniging. |
15
|
|
|
* |
16
|
|
|
* Registered office: Hoekenrode 3, 1102BR Amsterdam, The Netherlands. |
17
|
|
|
* UK branch address: City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK |
18
|
|
|
* |
19
|
|
|
* License: see the web/copyright.inc.php file in the file structure or |
20
|
|
|
* <base_url>/copyright.php after deploying the software |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* |
26
|
|
|
* This is the definition of the CAT class |
27
|
|
|
* @author Stefan Winter <[email protected]> |
28
|
|
|
* @author Tomasz Wolniewicz <[email protected]> |
29
|
|
|
* |
30
|
|
|
* @package Developer |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
namespace core\common; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* This class maintains state of the selected language and can set the language. |
37
|
|
|
*/ |
38
|
|
|
class Language |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* the current language |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $LANG = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* language display name for the language set by the constructor |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $locale; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor sets the language by calling set_lang |
57
|
|
|
* and stores language settings in object properties |
58
|
|
|
* additionally it also sets static variables $laing_index and $root |
59
|
|
|
*/ |
60
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
$language = $this->setLang(); |
63
|
|
|
$this->LANG = $language[0]; |
64
|
|
|
$this->locale = $language[1]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sets the gettext domain |
69
|
|
|
* |
70
|
|
|
* @param string $domain the text domain |
71
|
|
|
* @return string previous seting so that you can restore it later |
72
|
|
|
*/ |
73
|
|
|
public function setTextDomain($domain) |
74
|
|
|
{ |
75
|
|
|
$loggerInstance = new \core\common\Logging(); |
76
|
|
|
$olddomain = textdomain(NULL); |
77
|
|
|
$loggerInstance->debug(4, "set_locale($domain)\n"); |
78
|
|
|
$loggerInstance->debug(4, ROOT . "\n"); |
79
|
|
|
textdomain($domain); |
80
|
|
|
bindtextdomain($domain, ROOT . "/translation/"); |
81
|
|
|
return $olddomain; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* set_lang does all language setting magic |
86
|
|
|
* checks if lang has been declared in the http call |
87
|
|
|
* if not, checks for saved lang in the SESSION |
88
|
|
|
* or finally checks browser properties. |
89
|
|
|
* Only one of the supported langiages can be set |
90
|
|
|
* if a match is not found, the default langiage is used |
91
|
|
|
* @param int $hardSetLang - this is currently not used but will allow to force lang setting if this was ever required |
92
|
|
|
* @return array the language that was set |
93
|
|
|
*/ |
94
|
|
|
private function setLang($hardSetLang = 0) |
95
|
|
|
{ |
96
|
|
|
// $langConverted will contain candidates for the language setting in the order |
97
|
|
|
// of prefference |
98
|
|
|
$langConverted = []; |
99
|
|
|
if ($hardSetLang !== 0) { |
100
|
|
|
$langConverted[] = $hardSetLang; |
101
|
|
|
} |
102
|
|
|
if (!empty($_REQUEST['lang'])) { |
103
|
|
|
$recoverLang = filter_input(INPUT_GET, 'lang', FILTER_SANITIZE_STRING) ?? filter_input(INPUT_POST, 'lang', FILTER_SANITIZE_STRING); |
104
|
|
|
$langConverted[] = $recoverLang; |
105
|
|
|
} |
106
|
|
|
\core\CAT::sessionStart(); |
107
|
|
|
if (!empty($_SESSION['language'])) { |
108
|
|
|
$langConverted[] = $_SESSION['language']; |
109
|
|
|
} |
110
|
|
|
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
111
|
|
|
$langs = explode(",", filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING)); |
112
|
|
|
foreach ($langs as $lang) { |
113
|
|
|
$result = []; |
114
|
|
|
preg_match("/(.*);+.*/", $lang, $result); |
115
|
|
|
$langConverted[] = (empty($result[1]) ? $lang : $result[1]); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
$langIndex = \config\Master::APPEARANCE['defaultlocale']; |
119
|
|
|
$theLocale = \config\Master::LANGUAGES[$langIndex]['locale']; |
120
|
|
|
// always add configured default language as the last resort |
121
|
|
|
$langConverted[] = $langIndex; |
122
|
|
|
setlocale(LC_ALL, 0); |
123
|
|
|
foreach ($langConverted as $tryLang) { |
124
|
|
|
// madness! setlocale is completely unflexible. If $tryLang is "en" |
125
|
|
|
// it will fail, because it only knows en_US, en_GB a.s.o. |
126
|
|
|
// we need to map stuff manually |
127
|
|
|
$localeTmp = FALSE; |
128
|
|
|
|
129
|
|
|
// check if this language is supported by the CAT config |
130
|
|
|
foreach (\config\Master::LANGUAGES as $language => $value) { |
131
|
|
|
if (preg_match("/^" . $language . ".*/", $tryLang)) { |
132
|
|
|
$localeTmp = $value['locale']; |
133
|
|
|
$langIndex = $language; // ??? |
134
|
|
|
break; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
// make sure that the selected locale is actually instlled on this system |
138
|
|
|
// normally this should not be needed, but it is a safeguard agains misconfiguration |
139
|
|
|
if ($localeTmp) { |
140
|
|
|
if (setlocale(LC_ALL, $localeTmp)) { |
141
|
|
|
$theLocale = $localeTmp; |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
putenv("LC_ALL=" . $theLocale); |
147
|
|
|
$_SESSION['language'] = $langIndex; |
148
|
|
|
$loggerInstance = new \core\common\Logging(); |
149
|
|
|
$loggerInstance->debug(4, "selected lang:$langIndex:$theLocale\n"); |
150
|
|
|
$loggerInstance->debug(4, print_r($langConverted, true)); |
151
|
|
|
return([$langIndex, $theLocale]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* gets the language setting in CAT |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public function getLang() |
160
|
|
|
{ |
161
|
|
|
return $this->LANG; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* pick a proper value for a given language |
166
|
|
|
* @param array $valueArray an array of (locale,content) records |
167
|
|
|
* @return string localised value corresponding to the chosen |
168
|
|
|
* locale or to the defalut locale C if a better mach was not available |
169
|
|
|
*/ |
170
|
|
|
public function getLocalisedValue($valueArray) |
171
|
|
|
{ |
172
|
|
|
$loggerInstance = new \core\common\Logging(); |
173
|
|
|
$out = 0; |
174
|
|
|
if (count($valueArray) > 0) { |
175
|
|
|
$returnValue = []; |
176
|
|
|
foreach ($valueArray as $val) { |
177
|
|
|
$returnValue[$val["lang"]] = $val['value']; |
178
|
|
|
} |
179
|
|
|
$out = $returnValue[$this->LANG] ?? $returnValue['C'] ?? array_shift($returnValue); |
180
|
|
|
} |
181
|
|
|
$loggerInstance->debug(4, "getLocalisedValue:$this->LANG:$out\n"); |
182
|
|
|
return $out; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Return all available translations of a given message |
187
|
|
|
* |
188
|
|
|
* @param string $message |
189
|
|
|
* @param string $domain |
190
|
|
|
* @return array indexed by language indexes |
191
|
|
|
*/ |
192
|
|
|
public function getAllTranslations($message, $domain) { |
193
|
|
|
$currentLocale = setlocale(LC_ALL, 0); |
194
|
|
|
$allValues = []; |
195
|
|
|
foreach (\config\Master::LANGUAGES as $onelanguage) { |
196
|
|
|
setlocale(LC_ALL, $onelanguage['locale']); |
197
|
|
|
$allValues[$onelanguage] = dgettext($domain, $message); |
198
|
|
|
} |
199
|
|
|
setlocale(LC_ALL, $currentLocale); |
200
|
|
|
return $allValues; |
201
|
|
|
} |
202
|
|
|
} |