language.inc.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. // CustomPages : Browser language detection
  3. // 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.
  4. // 2011, Jean-Karim Bockstael, CBlue <jeankarim@cblue.be>
  5. // This requires the Chamilo system to be initialized
  6. // (note that it's easier to do the following include in the parent page)
  7. // require_once('path/to/main/inc/global.inc.php');
  8. // Returns the best match between available languages and visitor preferences
  9. // return the best match as 2-chars code, null when none match
  10. function get_preferred_language($available_langs) {
  11. // Parsing the Accept-languages HTTP header
  12. $langs = array();
  13. foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $httplang) {
  14. $rawlang = explode(';q=', $httplang);
  15. if (strpos($rawlang[0], '-') !== FALSE) {
  16. // We ignore the locale part, as in en-GB vs en-US
  17. $rawlang[0] = substr($rawlang[0], 0, strpos($rawlang[0], '-'));
  18. }
  19. if (count($rawlang) == 1) {
  20. $rawlang[1] = 1.0; // The absence of weighting means a weight of 1 (max)
  21. }
  22. $langs[$rawlang[1]][] = $rawlang[0];
  23. }
  24. krsort($langs, SORT_NUMERIC);
  25. // Choosing the best match
  26. foreach($langs as $weight => $codes) {
  27. foreach ($codes as $code) {
  28. if (in_array($code, $available_langs)) {
  29. return $code;
  30. }
  31. }
  32. }
  33. // No match
  34. return null;
  35. }
  36. // Wrapper function for the get_lang function
  37. // use this if you want to avoid translation caching issues
  38. function cp_get_lang($variable) {
  39. return get_lang($variable, null, $_SESSION['user_language_choice']);
  40. }
  41. // Note that Chamilo languages are expressed as full english names, not 2-characters codes
  42. // e.g. 'english' instead of 'en', 'french' instead of 'fr', ...
  43. // We need a matching array. Note the value for the null key, which is the default language.
  44. // Also note that this is an example matchin array, not all languages are present.
  45. $chamilo_langs = array(null => 'english', 'en' => 'english', 'fr' => 'french', 'es' => 'spanish');
  46. // Which of these can we actually pick from ?
  47. $available_langs = array('en','fr');
  48. // Which language files will we need ?
  49. $language_file = array('courses', 'index', 'registration', 'admin','userInfo');
  50. // Let's find out which language to serve to this particular browser
  51. $lang_match = $chamilo_langs[get_preferred_language($available_langs)];
  52. // Chamilo overrides this parameters at some places, e.g. in the logout link
  53. if (isset($_REQUEST['language']) && !empty($_REQUEST['language']) && in_array($_REQUEST['language'], $chamilo_langs)) {
  54. $lang_match = $_REQUEST['language'];
  55. }
  56. // Maybe a language had already been selected, we should honor this
  57. if (isset($_SESSION['user_language_choice']) && in_array($_SESSION['user_language_choice'], $chamilo_langs)) {
  58. $lang_match = $_SESSION['user_language_choice'];
  59. }
  60. // We need to set the relevant session variables to the best match, to use Chamilo's i18n lib.
  61. $_user['language'] = $lang_match;
  62. $_SESSION['user_language_choice'] = $lang_match;
  63. ?>