language.inc.php 1.7 KB

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