LanguageManager.php 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Hold the PhpMyAdmin\LanguageManager class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\Core;
  10. use PhpMyAdmin\Language;
  11. use PhpMyAdmin\Template;
  12. use PhpMyAdmin\Url;
  13. use PhpMyAdmin\Util;
  14. /**
  15. * Language selection manager
  16. *
  17. * @package PhpMyAdmin
  18. */
  19. class LanguageManager
  20. {
  21. /**
  22. * @var array Definition data for languages
  23. *
  24. * Each member contains:
  25. * - Language code
  26. * - English language name
  27. * - Native language name
  28. * - Match regullar expression
  29. * - MySQL locale
  30. */
  31. private static $_language_data = array(
  32. 'af' => array(
  33. 'af',
  34. 'Afrikaans',
  35. '',
  36. 'af|afrikaans',
  37. '',
  38. ),
  39. 'am' => array(
  40. 'am',
  41. 'Amharic',
  42. 'አማርኛ',
  43. 'am|amharic',
  44. '',
  45. ),
  46. 'ar' => array(
  47. 'ar',
  48. 'Arabic',
  49. '&#1575;&#1604;&#1593;&#1585;&#1576;&#1610;&#1577;',
  50. 'ar|arabic',
  51. 'ar_AE',
  52. ),
  53. 'ar_ly' => array(
  54. 'ar_LY',
  55. 'Arabic (Libya)',
  56. 'ليبي',
  57. 'ar[_-]ly|arabic (libya)|libian arabic',
  58. 'ar_LY',
  59. ),
  60. 'az' => array(
  61. 'az',
  62. 'Azerbaijani',
  63. 'Az&#601;rbaycanca',
  64. 'az|azerbaijani',
  65. '',
  66. ),
  67. 'bn' => array(
  68. 'bn',
  69. 'Bangla',
  70. 'বাংলা',
  71. 'bn|bangla',
  72. '',
  73. ),
  74. 'be' => array(
  75. 'be',
  76. 'Belarusian',
  77. '&#1041;&#1077;&#1083;&#1072;&#1088;&#1091;&#1089;&#1082;&#1072;&#1103;',
  78. 'be|belarusian',
  79. 'be_BY',
  80. ),
  81. 'be@latin' => array(
  82. 'be@latin',
  83. 'Belarusian (latin)',
  84. 'Bie&#0322;aruskaja',
  85. 'be[-_]lat|be@latin|belarusian latin',
  86. '',
  87. ),
  88. 'ber' => array(
  89. 'ber',
  90. 'Berber',
  91. 'Tamaziɣt',
  92. 'ber|berber',
  93. '',
  94. ),
  95. 'bg' => array(
  96. 'bg',
  97. 'Bulgarian',
  98. '&#1041;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080;',
  99. 'bg|bulgarian',
  100. 'bg_BG',
  101. ),
  102. 'bs' => array(
  103. 'bs',
  104. 'Bosnian',
  105. 'Bosanski',
  106. 'bs|bosnian',
  107. '',
  108. ),
  109. 'br' => array(
  110. 'br',
  111. 'Breton',
  112. 'Brezhoneg',
  113. 'br|breton',
  114. '',
  115. ),
  116. 'brx' => array(
  117. 'brx',
  118. 'Bodo',
  119. 'बड़ो',
  120. 'brx|bodo',
  121. '',
  122. ),
  123. 'ca' => array(
  124. 'ca',
  125. 'Catalan',
  126. 'Catal&agrave;',
  127. 'ca|catalan',
  128. 'ca_ES',
  129. ),
  130. 'ckb' => array(
  131. 'ckb',
  132. 'Sorani',
  133. 'سۆرانی',
  134. 'ckb|sorani',
  135. '',
  136. ),
  137. 'cs' => array(
  138. 'cs',
  139. 'Czech',
  140. 'Čeština',
  141. 'cs|czech',
  142. 'cs_CZ',
  143. ),
  144. 'cy' => array(
  145. 'cy',
  146. 'Welsh',
  147. 'Cymraeg',
  148. 'cy|welsh',
  149. '',
  150. ),
  151. 'da' => array(
  152. 'da',
  153. 'Danish',
  154. 'Dansk',
  155. 'da|danish',
  156. 'da_DK',
  157. ),
  158. 'de' => array(
  159. 'de',
  160. 'German',
  161. 'Deutsch',
  162. 'de|german',
  163. 'de_DE',
  164. ),
  165. 'el' => array(
  166. 'el',
  167. 'Greek',
  168. '&Epsilon;&lambda;&lambda;&eta;&nu;&iota;&kappa;&#940;',
  169. 'el|greek',
  170. '',
  171. ),
  172. 'en' => array(
  173. 'en',
  174. 'English',
  175. '',
  176. 'en|english',
  177. 'en_US',
  178. ),
  179. 'en_gb' => array(
  180. 'en_GB',
  181. 'English (United Kingdom)',
  182. '',
  183. 'en[_-]gb|english (United Kingdom)',
  184. 'en_GB',
  185. ),
  186. 'eo' => array(
  187. 'eo',
  188. 'Esperanto',
  189. 'Esperanto',
  190. 'eo|esperanto',
  191. '',
  192. ),
  193. 'es' => array(
  194. 'es',
  195. 'Spanish',
  196. 'Espa&ntilde;ol',
  197. 'es|spanish',
  198. 'es_ES',
  199. ),
  200. 'et' => array(
  201. 'et',
  202. 'Estonian',
  203. 'Eesti',
  204. 'et|estonian',
  205. 'et_EE',
  206. ),
  207. 'eu' => array(
  208. 'eu',
  209. 'Basque',
  210. 'Euskara',
  211. 'eu|basque',
  212. 'eu_ES',
  213. ),
  214. 'fa' => array(
  215. 'fa',
  216. 'Persian',
  217. '&#1601;&#1575;&#1585;&#1587;&#1740;',
  218. 'fa|persian',
  219. '',
  220. ),
  221. 'fi' => array(
  222. 'fi',
  223. 'Finnish',
  224. 'Suomi',
  225. 'fi|finnish',
  226. 'fi_FI',
  227. ),
  228. 'fil' => array(
  229. 'fil',
  230. 'Filipino',
  231. 'Pilipino',
  232. 'fil|filipino',
  233. '',
  234. ),
  235. 'fr' => array(
  236. 'fr',
  237. 'French',
  238. 'Fran&ccedil;ais',
  239. 'fr|french',
  240. 'fr_FR',
  241. ),
  242. 'fy' => array(
  243. 'fy',
  244. 'Frisian',
  245. 'Frysk',
  246. 'fy|frisian',
  247. '',
  248. ),
  249. 'gl' => array(
  250. 'gl',
  251. 'Galician',
  252. 'Galego',
  253. 'gl|galician',
  254. 'gl_ES',
  255. ),
  256. 'gu' => array(
  257. 'gu',
  258. 'Gujarati',
  259. 'ગુજરાતી',
  260. 'gu|gujarati',
  261. 'gu_IN',
  262. ),
  263. 'he' => array(
  264. 'he',
  265. 'Hebrew',
  266. '&#1506;&#1489;&#1512;&#1497;&#1514;',
  267. 'he|hebrew',
  268. 'he_IL',
  269. ),
  270. 'hi' => array(
  271. 'hi',
  272. 'Hindi',
  273. '&#2361;&#2367;&#2344;&#2381;&#2342;&#2368;',
  274. 'hi|hindi',
  275. 'hi_IN',
  276. ),
  277. 'hr' => array(
  278. 'hr',
  279. 'Croatian',
  280. 'Hrvatski',
  281. 'hr|croatian',
  282. 'hr_HR',
  283. ),
  284. 'hu' => array(
  285. 'hu',
  286. 'Hungarian',
  287. 'Magyar',
  288. 'hu|hungarian',
  289. 'hu_HU',
  290. ),
  291. 'hy' => array(
  292. 'hy',
  293. 'Armenian',
  294. 'Հայերէն',
  295. 'hy|armenian',
  296. '',
  297. ),
  298. 'ia' => array(
  299. 'ia',
  300. 'Interlingua',
  301. '',
  302. 'ia|interlingua',
  303. '',
  304. ),
  305. 'id' => array(
  306. 'id',
  307. 'Indonesian',
  308. 'Bahasa Indonesia',
  309. 'id|indonesian',
  310. 'id_ID',
  311. ),
  312. 'ig' => array(
  313. 'ig',
  314. 'Igbo',
  315. 'Asụsụ Igbo',
  316. 'ig|igbo',
  317. '',
  318. ),
  319. 'it' => array(
  320. 'it',
  321. 'Italian',
  322. 'Italiano',
  323. 'it|italian',
  324. 'it_IT',
  325. ),
  326. 'ja' => array(
  327. 'ja',
  328. 'Japanese',
  329. '&#26085;&#26412;&#35486;',
  330. 'ja|japanese',
  331. 'ja_JP',
  332. ),
  333. 'ko' => array(
  334. 'ko',
  335. 'Korean',
  336. '&#54620;&#44397;&#50612;',
  337. 'ko|korean',
  338. 'ko_KR',
  339. ),
  340. 'ka' => array(
  341. 'ka',
  342. 'Georgian',
  343. '&#4325;&#4304;&#4320;&#4311;&#4323;&#4314;&#4312;',
  344. 'ka|georgian',
  345. '',
  346. ),
  347. 'kab' => array(
  348. 'kab',
  349. 'Kabylian',
  350. 'Taqbaylit',
  351. 'kab|kabylian',
  352. '',
  353. ),
  354. 'kk' => array(
  355. 'kk',
  356. 'Kazakh',
  357. 'Қазақ',
  358. 'kk|kazakh',
  359. '',
  360. ),
  361. 'km' => array(
  362. 'km',
  363. 'Khmer',
  364. 'ខ្មែរ',
  365. 'km|khmer',
  366. '',
  367. ),
  368. 'kn' => array(
  369. 'kn',
  370. 'Kannada',
  371. 'ಕನ್ನಡ',
  372. 'kn|kannada',
  373. '',
  374. ),
  375. 'ksh' => array(
  376. 'ksh',
  377. 'Colognian',
  378. 'Kölsch',
  379. 'ksh|colognian',
  380. '',
  381. ),
  382. 'ku' => array(
  383. 'ku',
  384. 'Kurdish',
  385. 'کوردی',
  386. 'ku|kurdish',
  387. '',
  388. ),
  389. 'ky' => array(
  390. 'ky',
  391. 'Kyrgyz',
  392. 'Кыргызча',
  393. 'ky|kyrgyz',
  394. '',
  395. ),
  396. 'li' => array(
  397. 'li',
  398. 'Limburgish',
  399. 'Lèmbörgs',
  400. 'li|limburgish',
  401. '',
  402. ),
  403. 'lt' => array(
  404. 'lt',
  405. 'Lithuanian',
  406. 'Lietuvi&#371;',
  407. 'lt|lithuanian',
  408. 'lt_LT',
  409. ),
  410. 'lv' => array(
  411. 'lv',
  412. 'Latvian',
  413. 'Latvie&scaron;u',
  414. 'lv|latvian',
  415. 'lv_LV',
  416. ),
  417. 'mk' => array(
  418. 'mk',
  419. 'Macedonian',
  420. 'Macedonian',
  421. 'mk|macedonian',
  422. 'mk_MK',
  423. ),
  424. 'ml' => array(
  425. 'ml',
  426. 'Malayalam',
  427. 'Malayalam',
  428. 'ml|malayalam',
  429. '',
  430. ),
  431. 'mn' => array(
  432. 'mn',
  433. 'Mongolian',
  434. '&#1052;&#1086;&#1085;&#1075;&#1086;&#1083;',
  435. 'mn|mongolian',
  436. 'mn_MN',
  437. ),
  438. 'ms' => array(
  439. 'ms',
  440. 'Malay',
  441. 'Bahasa Melayu',
  442. 'ms|malay',
  443. 'ms_MY',
  444. ),
  445. 'my' => array(
  446. 'my',
  447. 'Burmese',
  448. 'မြန်မာ',
  449. 'my|burmese',
  450. '',
  451. ),
  452. 'ne' => array(
  453. 'ne',
  454. 'Nepali',
  455. 'नेपाली',
  456. 'ne|nepali',
  457. '',
  458. ),
  459. 'nb' => array(
  460. 'nb',
  461. 'Norwegian',
  462. 'Norsk',
  463. 'nb|norwegian',
  464. 'nb_NO',
  465. ),
  466. 'nn' => array(
  467. 'nn',
  468. 'Norwegian Nynorsk',
  469. 'Nynorsk',
  470. 'nn|nynorsk',
  471. 'nn_NO',
  472. ),
  473. 'nl' => array(
  474. 'nl',
  475. 'Dutch',
  476. 'Nederlands',
  477. 'nl|dutch',
  478. 'nl_NL',
  479. ),
  480. 'pa' => array(
  481. 'pa',
  482. 'Punjabi',
  483. 'ਪੰਜਾਬੀ',
  484. 'pa|punjabi',
  485. '',
  486. ),
  487. 'pl' => array(
  488. 'pl',
  489. 'Polish',
  490. 'Polski',
  491. 'pl|polish',
  492. 'pl_PL',
  493. ),
  494. 'pt_br' => array(
  495. 'pt_BR',
  496. 'Brazilian Portuguese',
  497. 'Portugu&ecirc;s',
  498. 'pt[-_]br|brazilian portuguese',
  499. 'pt_BR',
  500. ),
  501. 'pt' => array(
  502. 'pt',
  503. 'Portuguese',
  504. 'Portugu&ecirc;s',
  505. 'pt|portuguese',
  506. 'pt_PT',
  507. ),
  508. 'rcf' => array(
  509. 'rcf',
  510. 'R&eacute;union Creole',
  511. 'Kr&eacute;ol',
  512. 'rcf|creole (reunion)',
  513. '',
  514. ),
  515. 'ro' => array(
  516. 'ro',
  517. 'Romanian',
  518. 'Rom&acirc;n&#259;',
  519. 'ro|romanian',
  520. 'ro_RO',
  521. ),
  522. 'ru' => array(
  523. 'ru',
  524. 'Russian',
  525. '&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;',
  526. 'ru|russian',
  527. 'ru_RU',
  528. ),
  529. 'si' => array(
  530. 'si',
  531. 'Sinhala',
  532. '&#3523;&#3538;&#3458;&#3524;&#3517;',
  533. 'si|sinhala',
  534. '',
  535. ),
  536. 'sk' => array(
  537. 'sk',
  538. 'Slovak',
  539. 'Sloven&#269;ina',
  540. 'sk|slovak',
  541. 'sk_SK',
  542. ),
  543. 'sl' => array(
  544. 'sl',
  545. 'Slovenian',
  546. 'Sloven&scaron;&#269;ina',
  547. 'sl|slovenian',
  548. 'sl_SI',
  549. ),
  550. 'sq' => array(
  551. 'sq',
  552. 'Albanian',
  553. 'Shqip',
  554. 'sq|albanian',
  555. 'sq_AL',
  556. ),
  557. 'sr@latin' => array(
  558. 'sr@latin',
  559. 'Serbian (latin)',
  560. 'Srpski',
  561. 'sr[-_]lat|sr@latin|serbian latin',
  562. 'sr_YU',
  563. ),
  564. 'sr' => array(
  565. 'sr',
  566. 'Serbian',
  567. '&#1057;&#1088;&#1087;&#1089;&#1082;&#1080;',
  568. 'sr|serbian',
  569. 'sr_YU',
  570. ),
  571. 'sv' => array(
  572. 'sv',
  573. 'Swedish',
  574. 'Svenska',
  575. 'sv|swedish',
  576. 'sv_SE',
  577. ),
  578. 'ta' => array(
  579. 'ta',
  580. 'Tamil',
  581. 'தமிழ்',
  582. 'ta|tamil',
  583. 'ta_IN',
  584. ),
  585. 'te' => array(
  586. 'te',
  587. 'Telugu',
  588. 'తెలుగు',
  589. 'te|telugu',
  590. 'te_IN',
  591. ),
  592. 'th' => array(
  593. 'th',
  594. 'Thai',
  595. '&#3616;&#3634;&#3625;&#3634;&#3652;&#3607;&#3618;',
  596. 'th|thai',
  597. 'th_TH',
  598. ),
  599. 'tk' => array(
  600. 'tk',
  601. 'Turkmen',
  602. 'Türkmençe',
  603. 'tk|turkmen',
  604. '',
  605. ),
  606. 'tr' => array(
  607. 'tr',
  608. 'Turkish',
  609. 'T&uuml;rk&ccedil;e',
  610. 'tr|turkish',
  611. 'tr_TR',
  612. ),
  613. 'tt' => array(
  614. 'tt',
  615. 'Tatarish',
  616. 'Tatar&ccedil;a',
  617. 'tt|tatarish',
  618. '',
  619. ),
  620. 'tzm' => array(
  621. 'tzm',
  622. 'Central Atlas Tamazight',
  623. 'Tamaziɣt',
  624. 'tzm|central atlas tamazight',
  625. '',
  626. ),
  627. 'ug' => array(
  628. 'ug',
  629. 'Uyghur',
  630. 'ئۇيغۇرچە',
  631. 'ug|uyghur',
  632. '',
  633. ),
  634. 'uk' => array(
  635. 'uk',
  636. 'Ukrainian',
  637. '&#1059;&#1082;&#1088;&#1072;&#1111;&#1085;&#1089;&#1100;&#1082;&#1072;',
  638. 'uk|ukrainian',
  639. 'uk_UA',
  640. ),
  641. 'ur' => array(
  642. 'ur',
  643. 'Urdu',
  644. 'اُردوُ',
  645. 'ur|urdu',
  646. 'ur_PK',
  647. ),
  648. 'uz@latin' => array(
  649. 'uz@latin',
  650. 'Uzbek (latin)',
  651. 'O&lsquo;zbekcha',
  652. 'uz[-_]lat|uz@latin|uzbek-latin',
  653. '',
  654. ),
  655. 'uz' => array(
  656. 'uz',
  657. 'Uzbek (cyrillic)',
  658. '&#1038;&#1079;&#1073;&#1077;&#1082;&#1095;&#1072;',
  659. 'uz[-_]cyr|uz@cyrillic|uzbek-cyrillic',
  660. '',
  661. ),
  662. 'vi' => array(
  663. 'vi',
  664. 'Vietnamese',
  665. 'Tiếng Việt',
  666. 'vi|vietnamese',
  667. 'vi_VN',
  668. ),
  669. 'vls' => array(
  670. 'vls',
  671. 'Flemish',
  672. 'West-Vlams',
  673. 'vls|flemish',
  674. '',
  675. ),
  676. 'zh_tw' => array(
  677. 'zh_TW',
  678. 'Chinese traditional',
  679. '&#20013;&#25991;',
  680. 'zh[-_](tw|hk)|chinese traditional',
  681. 'zh_TW',
  682. ),
  683. // only TW and HK use traditional Chinese while others (CN, SG, MY)
  684. // use simplified Chinese
  685. 'zh_cn' => array(
  686. 'zh_CN',
  687. 'Chinese simplified',
  688. '&#20013;&#25991;',
  689. 'zh(?![-_](tw|hk))([-_][[:alpha:]]{2,3})?|chinese simplified',
  690. 'zh_CN',
  691. ),
  692. );
  693. private $_available_locales;
  694. private $_available_languages;
  695. private $_lang_failed_cfg;
  696. private $_lang_failed_cookie;
  697. private $_lang_failed_request;
  698. private static $instance;
  699. /**
  700. * Returns LanguageManager singleton
  701. *
  702. * @return LanguageManager
  703. */
  704. public static function getInstance()
  705. {
  706. if (self::$instance === null) {
  707. self::$instance = new LanguageManager;
  708. }
  709. return self::$instance;
  710. }
  711. /**
  712. * Returns list of available locales
  713. *
  714. * @return array
  715. */
  716. public function listLocaleDir()
  717. {
  718. $result = array('en');
  719. /* Check for existing directory */
  720. if (!is_dir(LOCALE_PATH)) {
  721. return $result;
  722. }
  723. /* Open the directory */
  724. $handle = @opendir(LOCALE_PATH);
  725. /* This can happen if the kit is English-only */
  726. if ($handle === false) {
  727. return $result;
  728. }
  729. /* Process all files */
  730. while (false !== ($file = readdir($handle))) {
  731. $path = LOCALE_PATH
  732. . '/' . $file
  733. . '/LC_MESSAGES/phpmyadmin.mo';
  734. if ($file != "."
  735. && $file != ".."
  736. && @file_exists($path)
  737. ) {
  738. $result[] = $file;
  739. }
  740. }
  741. /* Close the handle */
  742. closedir($handle);
  743. return $result;
  744. }
  745. /**
  746. * Returns (cached) list of all available locales
  747. *
  748. * @return array of strings
  749. */
  750. public function availableLocales()
  751. {
  752. if (! $this->_available_locales) {
  753. if (! isset($GLOBALS['PMA_Config']) || empty($GLOBALS['PMA_Config']->get('FilterLanguages'))) {
  754. $this->_available_locales = $this->listLocaleDir();
  755. } else {
  756. $this->_available_locales = preg_grep(
  757. '@' . $GLOBALS['PMA_Config']->get('FilterLanguages') . '@',
  758. $this->listLocaleDir()
  759. );
  760. }
  761. }
  762. return $this->_available_locales;
  763. }
  764. /**
  765. * Checks whether there are some languages available
  766. *
  767. * @return boolean
  768. */
  769. public function hasChoice()
  770. {
  771. return count($this->availableLanguages()) > 1;
  772. }
  773. /**
  774. * Returns (cached) list of all available languages
  775. *
  776. * @return array of Language objects
  777. */
  778. public function availableLanguages()
  779. {
  780. if (! $this->_available_languages) {
  781. $this->_available_languages = array();
  782. foreach($this->availableLocales() as $lang) {
  783. $lang = strtolower($lang);
  784. if (isset($this::$_language_data[$lang])) {
  785. $data = $this::$_language_data[$lang];
  786. $this->_available_languages[$lang] = new Language(
  787. $data[0],
  788. $data[1],
  789. $data[2],
  790. $data[3],
  791. $data[4]
  792. );
  793. } else {
  794. $this->_available_languages[$lang] = new Language(
  795. $lang,
  796. ucfirst($lang),
  797. ucfirst($lang),
  798. $lang,
  799. ''
  800. );
  801. }
  802. }
  803. }
  804. return $this->_available_languages;
  805. }
  806. /**
  807. * Returns (cached) list of all available languages sorted
  808. * by name
  809. *
  810. * @return array of Language objects
  811. */
  812. public function sortedLanguages()
  813. {
  814. $this->availableLanguages();
  815. uasort($this->_available_languages, function($a, $b)
  816. {
  817. return $a->cmp($b);
  818. }
  819. );
  820. return $this->_available_languages;
  821. }
  822. /**
  823. * Return Language object for given code
  824. *
  825. * @param string $code Language code
  826. *
  827. * @return object|false Language object or false on failure
  828. */
  829. public function getLanguage($code)
  830. {
  831. $code = strtolower($code);
  832. $langs = $this->availableLanguages();
  833. if (isset($langs[$code])) {
  834. return $langs[$code];
  835. }
  836. return false;
  837. }
  838. /**
  839. * Return currently active Language object
  840. *
  841. * @return object Language object
  842. */
  843. public function getCurrentLanguage()
  844. {
  845. return $this->_available_languages[strtolower($GLOBALS['lang'])];
  846. }
  847. /**
  848. * Activates language based on configuration, user preferences or
  849. * browser
  850. *
  851. * @return Language
  852. */
  853. public function selectLanguage()
  854. {
  855. // check forced language
  856. if (! empty($GLOBALS['PMA_Config']->get('Lang'))) {
  857. $lang = $this->getLanguage($GLOBALS['PMA_Config']->get('Lang'));
  858. if ($lang !== false) {
  859. return $lang;
  860. }
  861. $this->_lang_failed_cfg = true;
  862. }
  863. // Don't use REQUEST in following code as it might be confused by cookies
  864. // with same name. Check user requested language (POST)
  865. if (! empty($_POST['lang'])) {
  866. $lang = $this->getLanguage($_POST['lang']);
  867. if ($lang !== false) {
  868. return $lang;
  869. }
  870. $this->_lang_failed_request = true;
  871. }
  872. // check user requested language (GET)
  873. if (! empty($_GET['lang'])) {
  874. $lang = $this->getLanguage($_GET['lang']);
  875. if ($lang !== false) {
  876. return $lang;
  877. }
  878. $this->_lang_failed_request = true;
  879. }
  880. // check previous set language
  881. if (! empty($GLOBALS['PMA_Config']->getCookie('pma_lang'))) {
  882. $lang = $this->getLanguage($GLOBALS['PMA_Config']->getCookie('pma_lang'));
  883. if ($lang !== false) {
  884. return $lang;
  885. }
  886. $this->_lang_failed_cookie = true;
  887. }
  888. $langs = $this->availableLanguages();
  889. // try to find out user's language by checking its HTTP_ACCEPT_LANGUAGE variable;
  890. $accepted_languages = Core::getenv('HTTP_ACCEPT_LANGUAGE');
  891. if ($accepted_languages) {
  892. foreach (explode(',', $accepted_languages) as $header) {
  893. foreach ($langs as $language) {
  894. if ($language->matchesAcceptLanguage($header)) {
  895. return $language;
  896. }
  897. }
  898. }
  899. }
  900. // try to find out user's language by checking its HTTP_USER_AGENT variable
  901. $user_agent = Core::getenv('HTTP_USER_AGENT');
  902. if (! empty($user_agent)) {
  903. foreach ($langs as $language) {
  904. if ($language->matchesUserAgent($user_agent)) {
  905. return $language;
  906. }
  907. }
  908. }
  909. // Didn't catch any valid lang : we use the default settings
  910. if (isset($langs[$GLOBALS['PMA_Config']->get('DefaultLang')])) {
  911. return $langs[$GLOBALS['PMA_Config']->get('DefaultLang')];
  912. }
  913. // Fallback to English
  914. return $langs['en'];
  915. }
  916. /**
  917. * Displays warnings about invalid languages. This needs to be postponed
  918. * to show messages at time when language is initialized.
  919. *
  920. * @return void
  921. */
  922. public function showWarnings()
  923. {
  924. // now, that we have loaded the language strings we can send the errors
  925. if ($this->_lang_failed_cfg
  926. || $this->_lang_failed_cookie
  927. || $this->_lang_failed_request
  928. ) {
  929. trigger_error(
  930. __('Ignoring unsupported language code.'),
  931. E_USER_ERROR
  932. );
  933. }
  934. }
  935. /**
  936. * Returns HTML code for the language selector
  937. *
  938. * @param boolean $use_fieldset whether to use fieldset for selection
  939. * @param boolean $show_doc whether to show documentation links
  940. *
  941. * @return string
  942. *
  943. * @access public
  944. */
  945. public function getSelectorDisplay($use_fieldset = false, $show_doc = true)
  946. {
  947. $_form_params = array(
  948. 'db' => $GLOBALS['db'],
  949. 'table' => $GLOBALS['table'],
  950. );
  951. // For non-English, display "Language" with emphasis because it's
  952. // not a proper word in the current language; we show it to help
  953. // people recognize the dialog
  954. $language_title = __('Language')
  955. . (__('Language') != 'Language' ? ' - <em>Language</em>' : '');
  956. if ($show_doc) {
  957. $language_title .= Util::showDocu('faq', 'faq7-2');
  958. }
  959. $available_languages = $this->sortedLanguages();
  960. return Template::get('select_lang')->render(
  961. array(
  962. 'language_title' => $language_title,
  963. 'use_fieldset' => $use_fieldset,
  964. 'available_languages' => $available_languages,
  965. '_form_params' => $_form_params,
  966. )
  967. );
  968. }
  969. }