language.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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');
  38. $chamilo_langs = array(null => 'english', 'en' => 'english', 'fr' => 'french', 'nl' => 'dutch', 'de' => 'german', 'es' => 'spanish');
  39. $lang_match = $chamilo_langs[get_preferred_language($available_langs)];
  40. // recover previous value ...
  41. if (isset($_SESSION['user_language_choice']))
  42. $lang_match = $_SESSION['user_language_choice'];
  43. // Chamilo parameter, on logout
  44. if (isset($_REQUEST['language']) && !empty($_REQUEST['language']) && in_array($_REQUEST['language'], $chamilo_langs)) {
  45. $lang_match = $_REQUEST['language'];
  46. }
  47. // Incoming link parameter
  48. if (isset($_REQUEST['lang']) && !empty($_REQUEST['lang']) && in_array($_REQUEST['lang'], $available_langs)) {
  49. $lang_match = $chamilo_langs[$_REQUEST['lang']];
  50. }
  51. global $_configuration;
  52. if (isset($_configuration['auto_detect_language_custom_pages']) &&
  53. $_configuration['auto_detect_language_custom_pages'] == true
  54. ) {
  55. // Auto detect
  56. $_user['language'] = $lang_match;
  57. $_SESSION['user_language_choice'] = $lang_match;
  58. } else {
  59. // Chamilo default platform.
  60. $defaultLanguage = api_get_interface_language();
  61. $_user['language'] = $defaultLanguage;
  62. $_SESSION['user_language_choice'] = $defaultLanguage;
  63. }