language.inc.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * Wrapper function for the get_lang function
  43. * use this if you want to avoid translation caching issues
  44. */
  45. function cp_get_lang($variable) {
  46. return get_lang($variable, null, $_SESSION['user_language_choice']);
  47. }
  48. /**
  49. * Code
  50. */
  51. // Note that Chamilo languages are expressed as full english names, not 2-characters codes
  52. // e.g. 'english' instead of 'en', 'french' instead of 'fr', ...
  53. // We need a matching array. Note the value for the null key, which is the default language.
  54. // Also note that this is an example matchin array, not all languages are present.
  55. $chamilo_langs = array(null => 'english', 'en' => 'english', 'fr' => 'french', 'es' => 'spanish');
  56. // Which of these can we actually pick from ?
  57. $available_langs = array('en','fr');
  58. // Let's find out which language to serve to this particular browser
  59. $lang_match = $chamilo_langs[get_preferred_language($available_langs)];
  60. // Chamilo overrides this parameters at some places, e.g. in the logout link
  61. if (isset($_REQUEST['language']) && !empty($_REQUEST['language']) && in_array($_REQUEST['language'], $chamilo_langs)) {
  62. $lang_match = $_REQUEST['language'];
  63. }
  64. // Maybe a language had already been selected, we should honor this
  65. if (isset($_SESSION['user_language_choice']) && in_array($_SESSION['user_language_choice'], $chamilo_langs)) {
  66. $lang_match = $_SESSION['user_language_choice'];
  67. }
  68. // We need to set the relevant session variables to the best match, to use Chamilo's i18n lib.
  69. $_user['language'] = $lang_match;
  70. $_SESSION['user_language_choice'] = $lang_match;
  71. ?>