Explicitely state GLOBALS here, though it should not be needed
[phpmyadmin-regexreplace.git] / libraries / select_lang.lib.php
blob2751df3e514369bb1dce792d412ffe1f8e140ac2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * phpMyAdmin Language Loading File
6 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Returns language name
15 function PMA_langName($tmplang) {
16 $lang_name = ucfirst(substr(strrchr($tmplang[0], '|'), 1));
18 // Include native name if non empty
19 if (!empty($tmplang[2])) {
20 $lang_name = $tmplang[2] . ' - ' . $lang_name;
23 return $lang_name;
26 /**
27 * tries to find the language to use
29 * @uses $GLOBALS['cfg']['lang']
30 * @uses $GLOBALS['cfg']['DefaultLang']
31 * @uses $GLOBALS['lang_failed_cfg']
32 * @uses $GLOBALS['lang_failed_cookie']
33 * @uses $GLOBALS['lang_failed_request']
34 * @uses $_REQUEST['lang']
35 * @uses $_COOKIE['pma_lang']
36 * @uses $_SERVER['HTTP_ACCEPT_LANGUAGE']
37 * @uses $_SERVER['HTTP_USER_AGENT']
38 * @uses PMA_langSet()
39 * @uses PMA_langDetect()
40 * @uses explode()
41 * @return bool success if valid lang is found, otherwise false
43 function PMA_langCheck()
45 // check forced language
46 if (! empty($GLOBALS['cfg']['Lang'])) {
47 if (PMA_langSet($GLOBALS['cfg']['Lang'])) {
48 return true;
49 } else {
50 $GLOBALS['lang_failed_cfg'] = $GLOBALS['cfg']['Lang'];
54 // Don't use REQUEST in following code as it might be confused by cookies with same name
55 // check user requested language (POST)
56 if (! empty($_POST['lang'])) {
57 if (PMA_langSet($_POST['lang'])) {
58 return true;
59 } elseif (!is_string($_POST['lang'])) {
60 /* Faked request, don't care on localisation */
61 $GLOBALS['lang_failed_request'] = 'Yes';
62 } else {
63 $GLOBALS['lang_failed_request'] = $_POST['lang'];
67 // check user requested language (GET)
68 if (! empty($_GET['lang'])) {
69 if (PMA_langSet($_GET['lang'])) {
70 return true;
71 } elseif (!is_string($_GET['lang'])) {
72 /* Faked request, don't care on localisation */
73 $GLOBALS['lang_failed_request'] = 'Yes';
74 } else {
75 $GLOBALS['lang_failed_request'] = $_GET['lang'];
79 // check previous set language
80 if (! empty($_COOKIE['pma_lang'])) {
81 if (PMA_langSet($_COOKIE['pma_lang'])) {
82 return true;
83 } elseif (!is_string($_COOKIE['pma_lang'])) {
84 /* Faked request, don't care on localisation */
85 $GLOBALS['lang_failed_cookie'] = 'Yes';
86 } else {
87 $GLOBALS['lang_failed_cookie'] = $_COOKIE['pma_lang'];
91 // try to findout user's language by checking its HTTP_ACCEPT_LANGUAGE variable
92 if (PMA_getenv('HTTP_ACCEPT_LANGUAGE')) {
93 foreach (explode(',', PMA_getenv('HTTP_ACCEPT_LANGUAGE')) as $lang) {
94 if (PMA_langDetect($lang, 1)) {
95 return true;
100 // try to findout user's language by checking its HTTP_USER_AGENT variable
101 if (PMA_langDetect(PMA_getenv('HTTP_USER_AGENT'), 2)) {
102 return true;
105 // Didn't catch any valid lang : we use the default settings
106 if (PMA_langSet($GLOBALS['cfg']['DefaultLang'])) {
107 return true;
110 return false;
114 * checks given lang and sets it if valid
115 * returns true on success, otherwise flase
117 * @uses $GLOBALS['available_languages'] to check $lang
118 * @uses $GLOBALS['lang'] to set it
119 * @param string $lang language to set
120 * @return bool success
122 function PMA_langSet(&$lang)
124 /* Partial backward compatibility with 3.3 and older branches */
125 $lang = str_replace('-utf-8', '', $lang);
127 if (!is_string($lang) || empty($lang) || empty($GLOBALS['available_languages'][$lang])) {
128 return false;
130 $GLOBALS['lang'] = $lang;
131 return true;
135 * Analyzes some PHP environment variables to find the most probable language
136 * that should be used
138 * @param string string to analyze
139 * @param integer type of the PHP environment variable which value is $str
141 * @return bool true on success, otherwise false
143 * @uses $GLOBALS['available_languages'] to check $lang
145 * @access private
147 function PMA_langDetect($str, $envType)
149 if (empty($str)) {
150 return false;
152 if (empty($GLOBALS['available_languages'])) {
153 return false;
156 foreach ($GLOBALS['available_languages'] as $lang => $value) {
157 // $envType = 1 for the 'HTTP_ACCEPT_LANGUAGE' environment variable,
158 // 2 for the 'HTTP_USER_AGENT' one
159 $expr = $value[0];
160 if (strpos($expr, '[-_]') === FALSE) {
161 $expr = str_replace('|', '([-_][[:alpha:]]{2,3})?|', $expr);
163 if (($envType == 1 && preg_match('/^(' . addcslashes($expr,'/') . ')(;q=[0-9]\\.[0-9])?$/i', $str))
164 || ($envType == 2 && preg_match('/(\(|\[|;[[:space:]])(' . addcslashes($expr,'/') . ')(;|\]|\))/i', $str))) {
165 if (PMA_langSet($lang)) {
166 return true;
171 return false;
172 } // end of the 'PMA_langDetect()' function
176 * All the supported languages have to be listed in the array below.
177 * 1. The key must be the "official" ISO 639 language code and, if required,
178 * the dialect code. It can also contain some information about the
179 * charset (see the Russian case).
180 * 2. The first of the values associated to the key is used in a regular
181 * expression to find some keywords corresponding to the language inside two
182 * environment variables.
183 * These values contain:
184 * - the "official" ISO language code and, if required, the dialect code
185 * too ('bu' for Bulgarian, 'fr([-_][[:alpha:]]{2})?' for all French
186 * dialects, 'zh[-_]tw' for Chinese traditional...), the dialect has to
187 * be specified first;
188 * - the '|' character (it means 'OR');
189 * - the full language name.
190 * 3. The second value associated to the key is the language code as defined by
191 * the RFC1766.
192 * 4. The third value is its native name in html entities or UTF-8.
194 * Beware that the sorting order (first values associated to keys by
195 * alphabetical reverse order in the array) is important: 'zh-tw' (chinese
196 * traditional) must be detected before 'zh' (chinese simplified) for
197 * example.
200 function PMA_langDetails($lang) {
201 switch ($lang) {
202 case 'af':
203 return array('af|afrikaans', 'af', '');
204 case 'ar':
205 return array('ar|arabic', 'ar', '&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;');
206 case 'az':
207 return array('az|azerbaijani', 'az', 'Az&#601;rbaycanca');
208 case 'bn':
209 return array('bn|bangla', 'bn', 'বাংলা');
210 case 'be':
211 return array('be|belarusian', 'be', '&#1041;&#1077;&#1083;&#1072;&#1088;&#1091;&#1089;&#1082;&#1072;&#1103;');
212 case 'be@latin':
213 return array('be[-_]lat|belarusian latin', 'be-lat', 'Bie&#0322;aruskaja');
214 case 'bg':
215 return array('bg|bulgarian', 'bg', '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;');
216 case 'bs':
217 return array('bs|bosnian', 'bs', 'Bosanski');
218 case 'ca':
219 return array('ca|catalan', 'ca', 'Catal&agrave;');
220 case 'cs':
221 return array('cs|czech', 'cs', '&#268;esky');
222 case 'cy':
223 return array('cy|welsh', 'cy', 'Cymraeg');
224 case 'da':
225 return array('da|danish', 'da', 'Dansk');
226 case 'de':
227 return array('de|german', 'de', 'Deutsch');
228 case 'el':
229 return array('el|greek', 'el', '&Epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;');
230 case 'en':
231 return array('en|english', 'en', '');
232 case 'en_GB':
233 return array('en[_-]gb|english (United Kingdom)', 'en-gb', '');
234 case 'es':
235 return array('es|spanish', 'es', 'Espa&ntilde;ol');
236 case 'et':
237 return array('et|estonian', 'et', 'Eesti');
238 case 'eu':
239 return array('eu|basque', 'eu', 'Euskara');
240 case 'fa':
241 return array('fa|persian', 'fa', '&#1601;&#1575;&#1585;&#1587;&#1740;');
242 case 'fi':
243 return array('fi|finnish', 'fi', 'Suomi');
244 case 'fr':
245 return array('fr|french', 'fr', 'Fran&ccedil;ais');
246 case 'gl':
247 return array('gl|galician', 'gl', 'Galego');
248 case 'he':
249 return array('he|hebrew', 'he', '&#1506;&#1489;&#1512;&#1497;&#1514;');
250 case 'hi':
251 return array('hi|hindi', 'hi', '&#2361;&#2367;&#2344;&#2381;&#2342;&#2368;');
252 case 'hr':
253 return array('hr|croatian', 'hr', 'Hrvatski');
254 case 'hu':
255 return array('hu|hungarian', 'hu', 'Magyar');
256 case 'id':
257 return array('id|indonesian', 'id', 'Bahasa Indonesia');
258 case 'it':
259 return array('it|italian', 'it', 'Italiano');
260 case 'ja':
261 return array('ja|japanese', 'ja', '&#26085;&#26412;&#35486;');
262 case 'ko':
263 return array('ko|korean', 'ko', '&#54620;&#44397;&#50612;');
264 case 'ka':
265 return array('ka|georgian', 'ka', '&#4325;&#4304;&#4320;&#4311;&#4323;&#4314;&#4312;');
266 case 'lt':
267 return array('lt|lithuanian', 'lt', 'Lietuvi&#371;');
268 case 'lv':
269 return array('lv|latvian', 'lv', 'Latvie&scaron;u');
270 case 'mk':
271 return array('mk|macedonian', 'mk', 'Macedonian');
272 case 'mn':
273 return array('mn|mongolian', 'mn', '&#1052;&#1086;&#1085;&#1075;&#1086;&#1083;');
274 case 'ms':
275 return array('ms|malay', 'ms', 'Bahasa Melayu');
276 case 'nl':
277 return array('nl|dutch', 'nl', 'Nederlands');
278 case 'nb':
279 return array('nb|norwegian', 'nb', 'Norsk');
280 case 'pl':
281 return array('pl|polish', 'pl', 'Polski');
282 case 'pt_BR':
283 return array('pt[-_]br|brazilian portuguese', 'pt-BR', 'Portugu&ecirc;s');
284 case 'pt':
285 return array('pt|portuguese', 'pt', 'Portugu&ecirc;s');
286 case 'ro':
287 return array('ro|romanian', 'ro', 'Rom&acirc;n&#259;');
288 case 'ru':
289 return array('ru|russian', 'ru', '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;');
290 case 'si':
291 return array('si|sinhala', 'si', '&#3523;&#3538;&#3458;&#3524;&#3517;');
292 case 'sk':
293 return array('sk|slovak', 'sk', 'Sloven&#269;ina');
294 case 'sl':
295 return array('sl|slovenian', 'sl', 'Sloven&scaron;&#269;ina');
296 case 'sq':
297 return array('sq|albanian', 'sq', 'Shqip');
298 case 'sr@latin':
299 return array('sr[-_]lat|serbian latin', 'sr-lat', 'Srpski');
300 case 'sr':
301 return array('sr|serbian', 'sr', '&#1057;&#1088;&#1087;&#1089;&#1082;&#1080;');
302 case 'sv':
303 return array('sv|swedish', 'sv', 'Svenska');
304 case 'ta':
305 return array('ta|tamil', 'ta', 'தமிழ்');
306 case 'te':
307 return array('te|telugu', 'te', 'తెలుగు');
308 case 'th':
309 return array('th|thai', 'th', '&#3616;&#3634;&#3625;&#3634;&#3652;&#3607;&#3618;');
310 case 'tr':
311 return array('tr|turkish', 'tr', 'T&uuml;rk&ccedil;e');
312 case 'tt':
313 return array('tt|tatarish', 'tt', 'Tatar&ccedil;a');
314 case 'ug':
315 return array('ug|uyghur', 'ug', 'ئۇيغۇرچە');
316 case 'uk':
317 return array('uk|ukrainian', 'uk', '&#1059;&#1082;&#1088;&#1072;&#1111;&#1085;&#1089;&#1100;&#1082;&#1072;');
318 case 'ur':
319 return array('ur|urdu', 'ur', 'اُردوُ');
320 case 'uz@latin':
321 return array('uz[-_]lat|uzbek-latin', 'uz-lat', 'O&lsquo;zbekcha');
322 case 'uz':
323 return array('uz[-_]cyr|uzbek-cyrillic', 'uz-cyr', '&#1038;&#1079;&#1073;&#1077;&#1082;&#1095;&#1072;');
324 case 'zh_TW':
325 return array('zh[-_](tw|hk)|chinese traditional', 'zh-TW', '&#20013;&#25991;');
326 case 'zh_CN':
327 return array('zh|chinese simplified', 'zh', '&#20013;&#25991;');
329 return array("$lang|$lang", $lang, $lang);
333 * Returns list of languages supported by phpMyAdmin
335 * @return array
337 function PMA_langList()
339 /* We can always speak English */
340 $result = array('en' => PMA_langDetails('en'));
342 /* Open the directory */
343 $handle = @opendir($GLOBALS['lang_path']);
344 /* This can happen if the kit is English-only */
345 if ($handle === FALSE) {
346 return $result;
349 /* Process all files */
350 while (FALSE !== ($file = readdir($handle))) {
351 if ($file != "." && $file != ".." && file_exists($GLOBALS['lang_path'] . '/' . $file . '/LC_MESSAGES/phpmyadmin.mo')) {
352 $result[$file] = PMA_langDetails($file);
355 /* Close the handle */
356 closedir($handle);
358 return $result;
362 * @global string path to the translations directory; may be absent if the kit is English-only
364 $GLOBALS['lang_path'] = './locale/';
367 * Load gettext functions.
369 require './libraries/php-gettext/gettext.inc';
372 * @global string interface language
374 $GLOBALS['lang'] = 'en';
376 * @global boolean whether loading lang from cfg failed
378 $GLOBALS['lang_failed_cfg'] = false;
380 * @global boolean whether loading lang from cookie failed
382 $GLOBALS['lang_failed_cookie'] = false;
384 * @global boolean whether loading lang from user request failed
386 $GLOBALS['lang_failed_request'] = false;
388 * @global string text direction ltr or rtl
390 $GLOBALS['text_dir'] = 'ltr';
393 * @global array supported languages
395 $GLOBALS['available_languages'] = PMA_langList();
397 // Language filtering support
398 if (! empty($GLOBALS['cfg']['FilterLanguages'])) {
399 $new_lang = array();
400 foreach ($GLOBALS['available_languages'] as $key => $val) {
401 if (preg_match('@' . $GLOBALS['cfg']['FilterLanguages'] . '@', $key)) {
402 $new_lang[$key] = $val;
405 if (count($new_lang) > 0) {
406 $GLOBALS['available_languages'] = $new_lang;
408 unset($key, $val, $new_lang);
412 * @global array MySQL charsets map
414 $GLOBALS['mysql_charset_map'] = array(
415 'big5' => 'big5',
416 'cp-866' => 'cp866',
417 'euc-jp' => 'ujis',
418 'euc-kr' => 'euckr',
419 'gb2312' => 'gb2312',
420 'gbk' => 'gbk',
421 'iso-8859-1' => 'latin1',
422 'iso-8859-2' => 'latin2',
423 'iso-8859-7' => 'greek',
424 'iso-8859-8' => 'hebrew',
425 'iso-8859-8-i' => 'hebrew',
426 'iso-8859-9' => 'latin5',
427 'iso-8859-13' => 'latin7',
428 'iso-8859-15' => 'latin1',
429 'koi8-r' => 'koi8r',
430 'shift_jis' => 'sjis',
431 'tis-620' => 'tis620',
432 'utf-8' => 'utf8',
433 'windows-1250' => 'cp1250',
434 'windows-1251' => 'cp1251',
435 'windows-1252' => 'latin1',
436 'windows-1256' => 'cp1256',
437 'windows-1257' => 'cp1257',
441 * Do the work!
444 if (! PMA_langCheck()) {
445 // fallback language
446 $fall_back_lang = 'en';
447 $line = __LINE__;
448 if (! PMA_langSet($fall_back_lang)) {
449 trigger_error('phpMyAdmin-ERROR: invalid lang code: '
450 . __FILE__ . '#' . $line . ', check hard coded fall back language.',
451 E_USER_WARNING);
452 // stop execution
453 // and tell the user that his chosen language is invalid
454 PMA_fatalError('Could not load any language, please check your language settings and folder.');
458 // Set locale
459 _setlocale(LC_MESSAGES, $GLOBALS['lang']);
460 _bindtextdomain('phpmyadmin', $GLOBALS['lang_path']);
461 _bind_textdomain_codeset('phpmyadmin', 'UTF-8');
462 _textdomain('phpmyadmin');
465 * Messages for phpMyAdmin.
467 * These messages are here for easy transition to Gettext.
468 * You should not add any messages here, use instead gettext directly
469 * in your template/PHP file.
472 if (! function_exists('__')) {
473 die('Bad invocation!');
476 /* We use only utf-8 */
477 $charset = 'utf-8';
479 /* l10n: Text direction, use either ltr or rtl */
480 $GLOBALS['text_dir'] = __('ltr');
482 // now, that we have loaded the language strings we can send the errors
483 if ($GLOBALS['lang_failed_cfg']) {
484 trigger_error(
485 sprintf(__('Unknown language: %1$s.'),
486 htmlspecialchars($GLOBALS['lang_failed_cfg'])),
487 E_USER_ERROR);
489 if ($GLOBALS['lang_failed_cookie']) {
490 trigger_error(
491 sprintf(__('Unknown language: %1$s.'),
492 htmlspecialchars($GLOBALS['lang_failed_cookie'])),
493 E_USER_ERROR);
495 if ($GLOBALS['lang_failed_request']) {
496 trigger_error(
497 sprintf(__('Unknown language: %1$s.'),
498 htmlspecialchars($GLOBALS['lang_failed_request'])),
499 E_USER_ERROR);
502 unset($line, $fall_back_lang,
503 $GLOBALS['lang_failed_cfg'], $GLOBALS['lang_failed_cookie'], $GLOBALS['ang_failed_request']);