language.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. $detect = api_get_setting('auto_detect_language_custom_pages');
  52. if ($detect === 'true') {
  53. // Auto detect
  54. $_user['language'] = $lang_match;
  55. $_SESSION['user_language_choice'] = $lang_match;
  56. } else {
  57. // Chamilo default platform.
  58. $defaultLanguage = api_get_interface_language();
  59. $_user['language'] = $defaultLanguage;
  60. $_SESSION['user_language_choice'] = $defaultLanguage;
  61. }