language.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. $language_file = array('courses', 'index', 'registration', 'admin','userInfo');
  38. $available_langs = array('en','fr');
  39. $chamilo_langs = array(null => 'english', 'en' => 'english', 'fr' => 'french', 'nl' => 'dutch', 'de' => 'german', 'es' => 'spanish');
  40. $lang_match = $chamilo_langs[get_preferred_language($available_langs)];
  41. // recover previous value ...
  42. if (isset($_SESSION['user_language_choice']))
  43. $lang_match = $_SESSION['user_language_choice'];
  44. // Chamilo parameter, on logout
  45. if (isset($_REQUEST['language']) && !empty($_REQUEST['language']) && in_array($_REQUEST['language'], $chamilo_langs)) {
  46. $lang_match = $_REQUEST['language'];
  47. }
  48. // Incoming link parameter
  49. if (isset($_REQUEST['lang']) && !empty($_REQUEST['lang']) && in_array($_REQUEST['lang'], $available_langs)) {
  50. $lang_match = $chamilo_langs[$_REQUEST['lang']];
  51. }
  52. $_user['language'] = $lang_match;
  53. $_SESSION['user_language_choice'] = $lang_match;
  54. ?>