language.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Definition of language-related functions for cases where th user isn't
  5. * logged in yet
  6. * @package chamilo.custompages
  7. */
  8. /**
  9. * Get the preferred language base on the browser headers
  10. */
  11. function get_preferred_language($available_langs) {
  12. $langs = array();
  13. foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $httplang) {
  14. $rawlang = explode(';q=', $httplang);
  15. if (strpos($rawlang[0], '-') !== FALSE) {
  16. $rawlang[0] = substr($rawlang[0], 0, strpos($rawlang[0], '-'));
  17. }
  18. if (count($rawlang) == 1) {
  19. $rawlang[1] = 1.0;
  20. }
  21. $langs[$rawlang[1]] = $rawlang[0];
  22. }
  23. krsort($langs, SORT_NUMERIC);
  24. foreach($langs as $weight => $code) {
  25. if (in_array($code, $available_langs)) {
  26. return $code;
  27. }
  28. }
  29. return null;
  30. }
  31. /**
  32. * Get a language variable in a specific language
  33. */
  34. function custompages_get_lang($variable) {
  35. return get_lang($variable, null, $_SESSION['user_language_choice']);
  36. }
  37. $available_langs = array('en', 'fr', 'es', 'gl', 'eu');
  38. $chamilo_langs = array(
  39. null => 'english',
  40. 'en' => 'english',
  41. 'fr' => 'french',
  42. 'nl' => 'dutch',
  43. 'de' => 'german',
  44. 'es' => 'spanish',
  45. 'gl' => 'galician',
  46. 'eu' => 'basque'
  47. );
  48. $lang_match = $chamilo_langs[get_preferred_language($available_langs)];
  49. // recover previous value ...
  50. if (isset($_SESSION['user_language_choice']))
  51. $lang_match = $_SESSION['user_language_choice'];
  52. // Chamilo parameter, on logout
  53. if (isset($_REQUEST['language']) && !empty($_REQUEST['language']) && in_array($_REQUEST['language'], $chamilo_langs)) {
  54. $lang_match = $_REQUEST['language'];
  55. }
  56. // Incoming link parameter
  57. if (isset($_REQUEST['lang']) && !empty($_REQUEST['lang']) && in_array($_REQUEST['lang'], $available_langs)) {
  58. $lang_match = $chamilo_langs[$_REQUEST['lang']];
  59. }
  60. $detect = api_get_setting('auto_detect_language_custom_pages');
  61. if ($detect === 'true') {
  62. // Auto detect
  63. $_user['language'] = $lang_match;
  64. $_SESSION['user_language_choice'] = $lang_match;
  65. } else {
  66. // Chamilo default platform.
  67. $defaultLanguage = api_get_interface_language();
  68. $_user['language'] = $defaultLanguage;
  69. $_SESSION['user_language_choice'] = $defaultLanguage;
  70. }