Bug #3183970 Losing SQL history
[phpmyadmin-themes.git] / libraries / select_lang.lib.php
blob407da6f71f49b3f3cf978144e52dcfac6e6fc4f9
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 'en':
203 return array('en|english', 'en', '');
204 case 'af':
205 return array('af|afrikaans', 'af', '');
206 case 'ar':
207 return array('ar|arabic', 'ar', '&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;');
208 case 'az':
209 return array('az|azerbaijani', 'az', 'Az&#601;rbaycanca');
210 case 'bn':
211 return array('bn|bangla', 'bn', '');
212 case 'be':
213 return array('be|belarusian', 'be', '&#1041;&#1077;&#1083;&#1072;&#1088;&#1091;&#1089;&#1082;&#1072;&#1103;');
214 case 'be@latin':
215 return array('be[-_]lat|belarusian latin', 'be-lat', 'Bie&#0322;aruskaja');
216 case 'bg':
217 return array('bg|bulgarian', 'bg', '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;');
218 case 'bs':
219 return array('bs|bosnian', 'bs', 'Bosanski');
220 case 'ca':
221 return array('ca|catalan', 'ca', 'Catal&agrave;');
222 case 'cs':
223 return array('cs|czech', 'cs', '&#268;esky');
224 case 'cy':
225 return array('cy|welsh', 'cy', 'Cymraeg');
226 case 'da':
227 return array('da|danish', 'da', 'Dansk');
228 case 'de':
229 return array('de|german', 'de', 'Deutsch');
230 case 'el':
231 return array('el|greek', 'el', '&Epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;');
232 case 'en':
233 return array('en|english', 'en', '');
234 case 'en_GB':
235 return array('en[_-]gb|english (United Kingdom)', 'en-gb', '');
236 case 'es':
237 return array('es|spanish', 'es', 'Espa&ntilde;ol');
238 case 'et':
239 return array('et|estonian', 'et', 'Eesti');
240 case 'eu':
241 return array('eu|basque', 'eu', 'Euskara');
242 case 'fa':
243 return array('fa|persian', 'fa', '&#1601;&#1575;&#1585;&#1587;&#1740;');
244 case 'fi':
245 return array('fi|finnish', 'fi', 'Suomi');
246 case 'fr':
247 return array('fr|french', 'fr', 'Fran&ccedil;ais');
248 case 'gl':
249 return array('gl|galician', 'gl', 'Galego');
250 case 'he':
251 return array('he|hebrew', 'he', '&#1506;&#1489;&#1512;&#1497;&#1514;');
252 case 'hi':
253 return array('hi|hindi', 'hi', '&#2361;&#2367;&#2344;&#2381;&#2342;&#2368;');
254 case 'hr':
255 return array('hr|croatian', 'hr', 'Hrvatski');
256 case 'hu':
257 return array('hu|hungarian', 'hu', 'Magyar');
258 case 'id':
259 return array('id|indonesian', 'id', 'Bahasa Indonesia');
260 case 'it':
261 return array('it|italian', 'it', 'Italiano');
262 case 'ja':
263 return array('ja|japanese', 'ja', '&#26085;&#26412;&#35486;');
264 case 'ko':
265 return array('ko|korean', 'ko', '&#54620;&#44397;&#50612;');
266 case 'ka':
267 return array('ka|georgian', 'ka', '&#4325;&#4304;&#4320;&#4311;&#4323;&#4314;&#4312;');
268 case 'lt':
269 return array('lt|lithuanian', 'lt', 'Lietuvi&#371;');
270 case 'lv':
271 return array('lv|latvian', 'lv', 'Latvie&scaron;u');
272 case 'mk':
273 return array('mk|macedonian', 'mk', 'Macedonian');
274 case 'mn':
275 return array('mn|mongolian', 'mn', '&#1052;&#1086;&#1085;&#1075;&#1086;&#1083;');
276 case 'ms':
277 return array('ms|malay', 'ms', 'Bahasa Melayu');
278 case 'nl':
279 return array('nl|dutch', 'nl', 'Nederlands');
280 case 'nb':
281 return array('nb|norwegian', 'nb', 'Norsk');
282 case 'pl':
283 return array('pl|polish', 'pl', 'Polski');
284 case 'pt_BR':
285 return array('pt[-_]br|brazilian portuguese', 'pt-BR', 'Portugu&ecirc;s');
286 case 'pt':
287 return array('pt|portuguese', 'pt', 'Portugu&ecirc;s');
288 case 'ro':
289 return array('ro|romanian', 'ro', 'Rom&acirc;n&#259;');
290 case 'ru':
291 return array('ru|russian', 'ru', '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;');
292 case 'si':
293 return array('si|sinhala', 'si', '&#3523;&#3538;&#3458;&#3524;&#3517;');
294 case 'sk':
295 return array('sk|slovak', 'sk', 'Sloven&#269;ina');
296 case 'sl':
297 return array('sl|slovenian', 'sl', 'Sloven&scaron;&#269;ina');
298 case 'sq':
299 return array('sq|albanian', 'sq', 'Shqip');
300 case 'sr@latin':
301 return array('sr[-_]lat|serbian latin', 'sr-lat', 'Srpski');
302 case 'sr':
303 return array('sr|serbian', 'sr', '&#1057;&#1088;&#1087;&#1089;&#1082;&#1080;');
304 case 'sv':
305 return array('sv|swedish', 'sv', 'Svenska');
306 case 'ta':
307 return array('ta|tamil', 'ta', 'தமிழ்');
308 case 'te':
309 return array('te|telugu', 'te', 'తెలుగు');
310 case 'th':
311 return array('th|thai', 'th', '&#3616;&#3634;&#3625;&#3634;&#3652;&#3607;&#3618;');
312 case 'tr':
313 return array('tr|turkish', 'tr', 'T&uuml;rk&ccedil;e');
314 case 'tt':
315 return array('tt|tatarish', 'tt', 'Tatar&ccedil;a');
316 case 'ug':
317 return array('ug|uyghur', 'ug', 'ئۇيغۇرچە');
318 case 'uk':
319 return array('uk|ukrainian', 'uk', '&#1059;&#1082;&#1088;&#1072;&#1111;&#1085;&#1089;&#1100;&#1082;&#1072;');
320 case 'ur':
321 return array('ur|urdu', 'ur', 'اُردوُ');
322 case 'uz@latin':
323 return array('uz[-_]lat|uzbek-latin', 'uz-lat', 'O&lsquo;zbekcha');
324 case 'uz':
325 return array('uz[-_]cyr|uzbek-cyrillic', 'uz-cyr', '&#1038;&#1079;&#1073;&#1077;&#1082;&#1095;&#1072;');
326 case 'zh_TW':
327 return array('zh[-_](tw|hk)|chinese traditional', 'zh-TW', '&#20013;&#25991;');
328 case 'zh_CN':
329 return array('zh|chinese simplified', 'zh', '&#20013;&#25991;');
331 return array("$lang|$lang", $lang, $lang);
335 * Returns list of languages supported by phpMyAdmin
337 * @return array
339 function PMA_langList()
341 /* We can always speak English */
342 $result = array('en' => PMA_langDetails('en'));
344 /* Open the directory */
345 $handle = @opendir($GLOBALS['lang_path']);
346 /* This can happen if the kit is English-only */
347 if ($handle === FALSE) {
348 return $result;
351 /* Process all files */
352 while (FALSE !== ($file = readdir($handle))) {
353 if ($file != "." && $file != ".." && file_exists($GLOBALS['lang_path'] . '/' . $file . '/LC_MESSAGES/phpmyadmin.mo')) {
354 $result[$file] = PMA_langDetails($file);
357 /* Close the handle */
358 closedir($handle);
360 return $result;
364 * @global string path to the translations directory; may be absent if the kit is English-only
366 $GLOBALS['lang_path'] = './locale/';
369 * Load gettext functions.
371 require './libraries/php-gettext/gettext.inc';
374 * @global string interface language
376 $GLOBALS['lang'] = 'en';
378 * @global boolean whether loading lang from cfg failed
380 $GLOBALS['lang_failed_cfg'] = false;
382 * @global boolean whether loading lang from cookie failed
384 $GLOBALS['lang_failed_cookie'] = false;
386 * @global boolean whether loading lang from user request failed
388 $GLOBALS['lang_failed_request'] = false;
390 * @global string text direction ltr or rtl
392 $GLOBALS['text_dir'] = 'ltr';
395 * @global array supported languages
397 $GLOBALS['available_languages'] = PMA_langList();
399 // Language filtering support
400 if (! empty($GLOBALS['cfg']['FilterLanguages'])) {
401 $new_lang = array();
402 foreach ($GLOBALS['available_languages'] as $key => $val) {
403 if (preg_match('@' . $GLOBALS['cfg']['FilterLanguages'] . '@', $key)) {
404 $new_lang[$key] = $val;
407 if (count($new_lang) > 0) {
408 $GLOBALS['available_languages'] = $new_lang;
410 unset($key, $val, $new_lang);
414 * @global array MySQL charsets map
416 $GLOBALS['mysql_charset_map'] = array(
417 'big5' => 'big5',
418 'cp-866' => 'cp866',
419 'euc-jp' => 'ujis',
420 'euc-kr' => 'euckr',
421 'gb2312' => 'gb2312',
422 'gbk' => 'gbk',
423 'iso-8859-1' => 'latin1',
424 'iso-8859-2' => 'latin2',
425 'iso-8859-7' => 'greek',
426 'iso-8859-8' => 'hebrew',
427 'iso-8859-8-i' => 'hebrew',
428 'iso-8859-9' => 'latin5',
429 'iso-8859-13' => 'latin7',
430 'iso-8859-15' => 'latin1',
431 'koi8-r' => 'koi8r',
432 'shift_jis' => 'sjis',
433 'tis-620' => 'tis620',
434 'utf-8' => 'utf8',
435 'windows-1250' => 'cp1250',
436 'windows-1251' => 'cp1251',
437 'windows-1252' => 'latin1',
438 'windows-1256' => 'cp1256',
439 'windows-1257' => 'cp1257',
443 * Do the work!
446 if (! PMA_langCheck()) {
447 // fallback language
448 $fall_back_lang = 'en';
449 $line = __LINE__;
450 if (! PMA_langSet($fall_back_lang)) {
451 trigger_error('phpMyAdmin-ERROR: invalid lang code: '
452 . __FILE__ . '#' . $line . ', check hard coded fall back language.',
453 E_USER_WARNING);
454 // stop execution
455 // and tell the user that his chosen language is invalid
456 PMA_fatalError('Could not load any language, please check your language settings and folder.');
460 // Set locale
461 _setlocale(LC_MESSAGES, $GLOBALS['lang']);
462 _bindtextdomain('phpmyadmin', $GLOBALS['lang_path']);
463 _bind_textdomain_codeset('phpmyadmin', 'UTF-8');
464 _textdomain('phpmyadmin');
467 * Messages for phpMyAdmin.
469 * These messages are here for easy transition to Gettext.
470 * You should not add any messages here, use instead gettext directly
471 * in your template/PHP file.
474 if (! function_exists('__')) {
475 die('Bad invocation!');
478 /* We use only utf-8 */
479 $charset = 'utf-8';
481 /* l10n: Text direction, use either ltr or rtl */
482 $text_dir = __('ltr');
484 // now, that we have loaded the language strings we can send the errors
485 if ($GLOBALS['lang_failed_cfg']) {
486 trigger_error(
487 sprintf(__('Unknown language: %1$s.'),
488 htmlspecialchars($GLOBALS['lang_failed_cfg'])),
489 E_USER_ERROR);
491 if ($GLOBALS['lang_failed_cookie']) {
492 trigger_error(
493 sprintf(__('Unknown language: %1$s.'),
494 htmlspecialchars($GLOBALS['lang_failed_cookie'])),
495 E_USER_ERROR);
497 if ($GLOBALS['lang_failed_request']) {
498 trigger_error(
499 sprintf(__('Unknown language: %1$s.'),
500 htmlspecialchars($GLOBALS['lang_failed_request'])),
501 E_USER_ERROR);
504 unset($line, $fall_back_lang,
505 $GLOBALS['lang_failed_cfg'], $GLOBALS['lang_failed_cookie'], $GLOBALS['ang_failed_request']);