From be933850a76df2f8a253511aebbda1633555226e Mon Sep 17 00:00:00 2001 From: skodak Date: Sun, 30 Jul 2006 10:39:21 +0000 Subject: [PATCH] Bug #6176 - locale setting on config variables page not functioning correctly; merged from MOODLE_16_STABLE --- admin/config.php | 8 +++---- admin/configvars.php | 4 ++-- lang/en_utf8/admin.php | 2 +- lib/defaults.php | 2 +- lib/moodlelib.php | 49 ++++++++++++++++++++++++++---------------- lib/setup.php | 1 + lib/weblib.php | 17 ++++++++++----- mod/chat/lib.php | 3 --- 8 files changed, 52 insertions(+), 34 deletions(-) diff --git a/admin/config.php b/admin/config.php index 959bf2b762..9c396dd1a2 100644 --- a/admin/config.php +++ b/admin/config.php @@ -46,7 +46,10 @@ $value = implode(',',$value); } } - unset($conf); + if ($name == "locale") { + $value = trim($value); + } + $conf = new object(); $conf->name = $name; $conf->value = $value; if ($current = get_record('config', 'name', $name)) { @@ -74,9 +77,6 @@ if (empty($config)) { $config = $CFG; - if (!$config->locale = get_field('config', 'value', 'name', 'locale')) { - $config->locale = $CFG->lang; - } } $sesskey = !empty($USER->id) ? $USER->sesskey : ''; diff --git a/admin/configvars.php b/admin/configvars.php index 59087ccee8..9849dcc60f 100644 --- a/admin/configvars.php +++ b/admin/configvars.php @@ -312,10 +312,10 @@ class configvarrss extends configvar { /// language menu $interface['langcache'] = new configvar ( get_string('configlangcache', 'admin'), choose_from_menu($noyesoptions, 'langcache', $config->langcache, '', '', '', true) ); - + /// locale $interface['locale'] = new configvar ( get_string('configlocale', 'admin'), - '' ); + '' ); /// docroot $interface['docroot'] = new configvar ( get_string('configdocroot', 'admin'), diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index 53711a0dcd..9d541e9167 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -70,7 +70,7 @@ $string['configlangcache'] = 'Cache the language menu. Saves a lot of memory and $string['configlangdir'] = 'Most languages are printed left-to-right, but some, like Arabic and Hebrew, are printed right-to-left.'; $string['configlanglist'] = 'Leave this blank to allow users to choose from any language you have in this installation of Moodle. However, you can shorten the language menu by entering a comma-separated list of language codes that you want. For example: en,es_es,fr,it'; $string['configlangmenu'] = 'Choose whether or not you want to display the general-purpose language menu on the home page, login page etc. This does not affect the user\'s ability to set the preferred language in their own profile.'; -$string['configlocale'] = 'Choose a sitewide locale - this will affect the format and language of dates. You need to have this locale data installed on your operating system. (eg en_US or es_ES). If you don\'t know what to choose leave it blank.'; +$string['configlocale'] = 'Choose a sitewide locale - this will override the format and language of dates for all language packs (though names of days in calendar are not affected). You need to have this locale data installed on your operating system (eg for linux en_US.UTF-8 or es_ES.UTF-8). In most cases this field should be left blank.'; $string['configloginhttps'] = 'Turning this on will make Moodle use a secure https connection just for the login page (providing a secure login), and then afterwards revert back to the normal http URL for general speed. CAUTION: this setting REQUIRES https to be specifically enabled on the web server - if it is not then YOU COULD LOCK YOURSELF OUT OF YOUR SITE.'; $string['configloglifetime'] = 'This specifies the length of time you want to keep logs about user activity. Logs that are older than this age are automatically deleted. It is best to keep logs as long as possible, in case you need them, but if you have a very busy server and are experiencing performance problems, then you may want to lower the log lifetime.'; $string['configlongtimenosee'] = 'If students haven\'t logged in for a very long time, then they are automatically unsubscribed from courses. This parameter specifies that time limit.'; diff --git a/lib/defaults.php b/lib/defaults.php index 771e9f4771..e9d982eca2 100644 --- a/lib/defaults.php +++ b/lib/defaults.php @@ -60,7 +60,7 @@ 'langcache' => 1, 'langmenu' => 1, 'langlist' => '', - 'locale' => 'en', + 'locale' => '', // by default language pack locales should be used 'loginhttps' => false, 'loglifetime' => 0, 'longtimenosee' => 100, diff --git a/lib/moodlelib.php b/lib/moodlelib.php index b27faf0375..80bda95bdf 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -1507,12 +1507,17 @@ function require_login($courseid=0, $autologinguest=true, $cm=null) { // Redefine global $COURSE if we can global $course; // We use the global hack once here so it doesn't need to be used again - if (is_object($course)) { + if (is_object($course) and !empty($course->id) and ($courseid == 0 or $course->id == $courseid)) { $COURSE = clone($course); } else if ($courseid) { $COURSE = get_record('course', 'id', $courseid); } + if (!empty($COURSE->lang)) { + $CFG->courselang = $COURSE->lang; + moodle_setlocale(); + } + // First check that the user is logged in to the site. if (! (isset($USER->loggedin) and $USER->confirmed and ($USER->site == $CFG->wwwroot)) ) { // They're not $SESSION->wantsurl = $FULLME; @@ -4698,9 +4703,11 @@ function get_string($identifier, $module='', $a=NULL) { global $CFG; - global $course; /// Not a nice hack, but quick + global $course, $COURSE; if (empty($CFG->courselang)) { - if (is_object($course) and isset($course->lang)) { + if (!empty($COURSE->lang)) { + $CFG->courselang = $COURSE->lang; + } else if (!empty($course->lang)) { // ugly backwards compatibility hack $CFG->courselang = $course->lang; } } @@ -5918,6 +5925,13 @@ function moodle_setlocale($locale='') { global $SESSION, $USER, $CFG; + static $currentlocale; // last locale caching + if (!isset($currentlocale)) { + $currentlocale = ''; + } + + $oldlocale = $currentlocale; + /// Fetch the correct locale based on ostype if(!empty($CFG->unicodedb) && $CFG->ostype == 'WINDOWS') { $stringtofetch = 'localewin'; @@ -5925,19 +5939,18 @@ function moodle_setlocale($locale='') { $stringtofetch = 'locale'; } - if ($locale) { - $CFG->locale = $locale; - } else if (!empty($CFG->courselang) and ($CFG->courselang != $CFG->lang) ) { - $CFG->locale = get_string($stringtofetch); - } else if (!empty($SESSION->lang) and ($SESSION->lang != $CFG->lang) ) { - $CFG->locale = get_string($stringtofetch); - } else if (!empty($USER->lang) and ($USER->lang != $CFG->lang) ) { - $CFG->locale = get_string($stringtofetch); - } else if (empty($CFG->locale)) { - $CFG->locale = get_string($stringtofetch); - if (!get_field('config', 'value', 'name', 'locale')) { // Make SURE there isn't one already - set_config('locale', $CFG->locale); // Cache it to save lookups in future - } +/// the priority is the same as in get_string() - parameter, config, course, session, user, global language + if (!empty($locale)) { + $currentlocale = $locale; + } else if (!empty($CFG->locale)) { // override locale for all language packs + $currentlocale = $CFG->locale; + } else { + $currentlocale = get_string($stringtofetch); + } + +/// do nothing if locale already set up + if ($oldlocale == $currentlocale) { + return; } /// Due to some strange BUG we cannot set the LC_TIME directly, so we fetch current values, @@ -5952,14 +5965,14 @@ function moodle_setlocale($locale='') { $messages= setlocale (LC_MESSAGES, 0); } /// Set locale to all - setlocale (LC_ALL, $CFG->locale); + setlocale (LC_ALL, $currentlocale); /// Set old values setlocale (LC_MONETARY, $monetary); setlocale (LC_NUMERIC, $numeric); if ($CFG->ostype != 'WINDOWS') { setlocale (LC_MESSAGES, $messages); } - if ($CFG->locale == 'tr_TR' or $CFG->locale == 'tr_TR.UTF-8') { // To workaround a well-known PHP bug with Turkish + if ($currentlocale == 'tr_TR' or $currentlocale == 'tr_TR.UTF-8') { // To workaround a well-known PHP problem with Turkish letter Ii setlocale (LC_CTYPE, $ctype); } } diff --git a/lib/setup.php b/lib/setup.php index 2558bd1c8a..6036e42304 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -508,6 +508,7 @@ $CFG->os = PHP_OS; $CFG->lang = !empty($CFG->unicodedb) ? 'en_utf8' : 'en'; } + // set default locale - might be changed again later in require_login() moodle_setlocale(); if (!empty($CFG->opentogoogle)) { diff --git a/lib/weblib.php b/lib/weblib.php index 6b9e631464..5b2c3ae16f 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1882,7 +1882,7 @@ function highlightfast($needle, $haystack) { function print_header ($title='', $heading='', $navigation='', $focus='', $meta='', $cache=true, $button=' ', $menu='', $usexml=false, $bodytags='') { - global $USER, $CFG, $THEME, $SESSION, $ME, $SITE, $COURSE, $HTTPSPAGEREQUIRED; + global $USER, $CFG, $THEME, $SESSION, $ME, $SITE, $HTTPSPAGEREQUIRED; /// This makes sure that the header is never repeated twice on a page if (defined('HEADER_PRINTED')) { @@ -1893,15 +1893,25 @@ function print_header ($title='', $heading='', $navigation='', $focus='', $meta= } define('HEADER_PRINTED', 'true'); -/// Set up course-based lang and theme if any + + global $course, $COURSE; if (!empty($COURSE->lang)) { $CFG->courselang = $COURSE->lang; + moodle_setlocale(); + } else if (!empty($course->lang)) { // ugly backwards compatibility hack + $CFG->courselang = $course->lang; + moodle_setlocale(); } if (!empty($COURSE->theme)) { if (!empty($CFG->allowcoursethemes)) { $CFG->coursetheme = $COURSE->theme; theme_setup(); } + } else if (!empty($course->theme)) { // ugly backwards compatibility hack + if (!empty($CFG->allowcoursethemes)) { + $CFG->coursetheme = $course->theme; + theme_setup(); + } } /// We have to change some URLs in styles if we are in a $HTTPSPAGEREQUIRED page @@ -1977,9 +1987,6 @@ function print_header ($title='', $heading='', $navigation='', $focus='', $meta= $encoding = current_charset(); - if (!empty($CFG->courselang)) { - moodle_setlocale(); - } $meta = ''. "\n". $meta ."\n"; if (!$usexml) { diff --git a/mod/chat/lib.php b/mod/chat/lib.php index c8d550b089..88d1b98c5f 100644 --- a/mod/chat/lib.php +++ b/mod/chat/lib.php @@ -321,9 +321,6 @@ function chat_force_language($lang) { if(!empty($CFG->courselang)) { unset($CFG->courselang); } - if(!empty($CFG->locale)) { - unset($CFG->locale); - } $CFG->lang = $lang; moodle_setlocale(); } -- 2.39.5