From: moodler Date: Mon, 12 Feb 2007 14:58:44 +0000 (+0000) Subject: Feature from Eloy to set the language to whatever the browser suggests (if the user... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=ab036ed9ca34719201e726b0d3831fb4bcb72db0;p=moodle.git Feature from Eloy to set the language to whatever the browser suggests (if the user is not logged in) --- diff --git a/lib/moodlelib.php b/lib/moodlelib.php index f155c2d233..bda1fa64b3 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6647,5 +6647,37 @@ function is_enabled_enrol($enrol='') { return in_array($enrol, explode(',', $CFG->enrol_plugins_enabled)); } +/** + * This function will search for browser prefereed languages, setting Moodle + * to use the best one available if $SESSION->lang is undefined + */ +function setup_lang_from_browser() { + + global $CFG, $SESSION; + + if (!empty($SESSION->lang)) { // Lang is defined in session, nothing to do + return; + } + + if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // There isn't list of browser langs, nothing to do + return; + } + +/// Extract and clean langs from headers + $langs = strtolower(clean_param($_SERVER['HTTP_ACCEPT_LANGUAGE'], PARAM_CLEAN)); /// Get String with basic clean + $langs = explode(',', $langs); /// Convert to array + $langs = preg_replace('/([a-z]{2,3}).*/','$1_utf8', $langs); ///Convert to Moodle langs + $langs = array_unique($langs); /// Avoid duplicates + +/// Look for such langs under standard locations + foreach ($langs as $lang) { + if (file_exists($CFG->dataroot .'/lang/'. $lang) or file_exists($CFG->dirroot .'/lang/'. $lang)) { + $SESSION->lang = $lang; /// Lang exists, set it in session + break; /// We have finished. Go out + } + } + return; +} + // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: ?> diff --git a/lib/setup.php b/lib/setup.php index e4e36dcd47..995eb3194a 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -569,7 +569,11 @@ global $HTTPSPAGEREQUIRED; $SESSION->lang = $lang.'_utf8'; } } + + setup_lang_from_browser(); + unset($lang); + if (empty($CFG->lang)) { if (empty($SESSION->lang)) { $CFG->lang = 'en_utf8';