language.inc.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * CustomPages : Browser language detection
  5. * Include this file in your custom page if you want to set the language variable of the Chamilo session
  6. * to the best pick according to the visitor's browser's options.
  7. * 2011, Jean-Karim Bockstael, CBlue <jeankarim@cblue.be>
  8. * This requires the Chamilo system to be initialized
  9. * (note that it's easier to do the following include in the parent page)
  10. * @package chamilo.custompages
  11. */
  12. /**
  13. * Returns the best match between available languages and visitor preferences
  14. * @return string the best match as 2-chars code, null when none match
  15. */
  16. function get_preferred_language($available_langs)
  17. {
  18. // Parsing the Accept-languages HTTP header
  19. $langs = array();
  20. foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $httplang) {
  21. $rawlang = explode(';q=', $httplang);
  22. if (strpos($rawlang[0], '-') !== false) {
  23. // We ignore the locale part, as in en-GB vs en-US
  24. $rawlang[0] = substr($rawlang[0], 0, strpos($rawlang[0], '-'));
  25. }
  26. if (count($rawlang) == 1) {
  27. $rawlang[1] = 1.0; // The absence of weighting means a weight of 1 (max)
  28. }
  29. $langs[$rawlang[1]][] = $rawlang[0];
  30. }
  31. krsort($langs, SORT_NUMERIC);
  32. // Choosing the best match
  33. foreach ($langs as $weight => $codes) {
  34. foreach ($codes as $code) {
  35. if (in_array($code, $available_langs)) {
  36. return $code;
  37. }
  38. }
  39. }
  40. // No match
  41. return null;
  42. }
  43. /**
  44. * Get a language variable in a specific language
  45. */
  46. function custompages_get_lang($variable)
  47. {
  48. return get_lang($variable, null, $_SESSION['user_language_choice']);
  49. }