From 5285dba95fb3bf4cbb1c7794364b61f063796e12 Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Mon, 15 Feb 2010 19:04:41 +0200 Subject: [PATCH] Feature #272 - Installation scripts: If it is possible, the initial value of the language during the installation procedure is according to the the browser. --- main/install/index.php | 40 +++++---- main/install/install_upgrade.lib.php | 119 +++++++++++++++++++++++---- 2 files changed, 127 insertions(+), 32 deletions(-) diff --git a/main/install/index.php b/main/install/index.php index 6d6f2621fe..038eb18866 100755 --- a/main/install/index.php +++ b/main/install/index.php @@ -43,6 +43,10 @@ session_start(); require '../inc/lib/main_api.lib.php'; require api_get_path(LIBRARY_PATH).'database.lib.php'; +// Including specialized libraries for the installation procedure. +require_once 'install_upgrade.lib.php'; //also defines constants +require_once 'install_functions.inc.php'; + // The function api_get_setting() might be called within the installation scripts. // We need to provide some limited support for it through initialization of the // global array-type variable $_setting. @@ -52,21 +56,27 @@ $_setting = array( 'permissions_for_new_files' => '0660' ); -// Loading language files. -// TODO: It would be nice browser's intrface language to be detected at this point. -require api_get_path(SYS_LANG_PATH).'english/trad4all.inc.php'; -require api_get_path(SYS_LANG_PATH).'english/install.inc.php'; +// Determination of the language during the installation procedure. if (!empty($_POST['language_list'])) { $search = array('../', '\\0'); $install_language = str_replace($search, '', urldecode($_POST['language_list'])); - if (!is_dir(api_get_path(SYS_LANG_PATH).$install_language)) { - $install_language = 'english'; - } - include_once api_get_path(SYS_LANG_PATH).$install_language.'/trad4all.inc.php'; - include_once api_get_path(SYS_LANG_PATH).$install_language.'/install.inc.php'; api_session_register('install_language'); } elseif (isset($_SESSION['install_language']) && $_SESSION['install_language']) { $install_language = $_SESSION['install_language']; +} else { + // Trying to switch to the browser's language, it is covenient for most of the cases. + $install_language = detect_browser_language(); +} + +// Language validation. +if (!array_key_exists($install_language, get_language_folder_list())) { + $install_language = 'english'; +} + +// Loading language files. +require api_get_path(SYS_LANG_PATH).'english/trad4all.inc.php'; +require api_get_path(SYS_LANG_PATH).'english/install.inc.php'; +if ($install_language != 'english') { include_once api_get_path(SYS_LANG_PATH).$install_language.'/trad4all.inc.php'; include_once api_get_path(SYS_LANG_PATH).$install_language.'/install.inc.php'; } @@ -86,10 +96,6 @@ api_set_internationalization_default_encoding($charset); // Page encoding initialization. header('Content-Type: text/html; charset='. api_get_system_encoding()); -// Specialized libraries for the installation procedure. -require_once 'install_upgrade.lib.php'; //also defines constants -require_once 'install_functions.inc.php'; - // Some constants define('SYSTEM_INSTALLATION', 1); define('MAX_COURSE_TRANSFER', 100); @@ -232,9 +238,9 @@ if (!isset($_GET['running'])) { $adminPhoneForm = '(000) 001 02 03'; $institutionForm = 'My Organisation'; $institutionUrlForm = 'http://www.chamilo.org'; - $languageForm = 'english'; // TODO: A better choice to be tested: - //$languageForm = api_get_interface_language(); + //$languageForm = 'english'; + $languageForm = api_get_interface_language(); $checkEmailByHashSent = 0; $ShowEmailnotcheckedToStudent = 1; @@ -355,7 +361,7 @@ if ($encryptPassForm == '1') { document.getElementById('optional_param5').style.display = ''; document.getElementById('optional_param6').style.display = ''; init_visibility = 1; - document.getElementById('optionalparameters').innerHTML=' '; + document.getElementById('optionalparameters').innerHTML=' '; } else { document.getElementById('optional_param1').style.display = 'none'; document.getElementById('optional_param2').style.display = 'none'; @@ -365,7 +371,7 @@ if ($encryptPassForm == '1') { document.getElementById('optional_param4').style.display = 'none'; document.getElementById('optional_param5').style.display = 'none'; document.getElementById('optional_param6').style.display = 'none'; - document.getElementById('optionalparameters').innerHTML=' '; + document.getElementById('optionalparameters').innerHTML=' '; init_visibility = 0; } } diff --git a/main/install/install_upgrade.lib.php b/main/install/install_upgrade.lib.php index 397e696192..f8a69f6a56 100755 --- a/main/install/install_upgrade.lib.php +++ b/main/install/install_upgrade.lib.php @@ -392,25 +392,114 @@ function get_sql_file_contents($file, $section, $print_errors = true) { } /** - * Returns a list of language directories. + * Tries to detect browser's language. + * @return string Returns a language identificator, i.e. 'english', 'spanish', ... */ -function get_language_folder_list() { - $result = array(); - $exceptions = array('.', '..', 'CVS', '.svn'); - $search = array('_latin', '_unicode', '_corporate', '_org' , '_KM', '_'); - $replace_with = array(' (Latin)', ' (unicode)', ' (corporate)', ' (org)', ' (KM)', ' '); - $dirname = api_get_path(SYS_LANG_PATH); - $handle = opendir($dirname); - while ($entries = readdir($handle)) { - if (in_array($entries, $exceptions)) { - continue; +function detect_browser_language() { + static $language_index = array( + 'ar' => 'arabic', + 'ast' => 'asturian', + 'bg' => 'bulgarian', + 'bs' => 'bosnian', + 'ca' => 'catalan', + 'zh' => 'simpl_chinese', + 'zh-tw' => 'trad_chinese', + 'cs' => 'czech', + 'da' => 'danish', + 'prs' => 'dari', + 'de' => 'german', + 'el' => 'greek', + 'en' => 'english', + 'es' => 'spanish', + 'eo' => 'esperanto', + 'eu' => 'euskera', + 'fa' => 'persian', + 'fr' => 'french', + 'fur' => 'friulian', + 'gl' => 'galician', + 'ka' => 'georgian', + 'hr' => 'croatian', + 'he' => 'hebrew', + 'id' => 'indonesian', + 'it' => 'italian', + 'ko' => 'korean', + 'lv' => 'latvian', + 'lt' => 'lithuanian', + 'mk' => 'macedonian', + 'hu' => 'hungarian', + 'ms' => 'malay', + 'nl' => 'dutch', + 'ja' => 'japanese', + 'no' => 'norwegian', + 'oc' => 'occitan', + 'ps' => 'pashto', + 'pl' => 'polish', + 'pt' => 'portuguese', + 'pt-br' => 'brazilian', + 'ro' => 'romanian', + 'qu' => 'quechua_cusco', + 'ru' => 'russian', + 'sk' => 'slovak', + 'sl' => 'slovenian', + 'sr' => 'serbian', + 'fi' => 'finnish', + 'sv' => 'swedish', + 'th' => 'thai', + 'tr' => 'turkce', + 'uk' => 'ukrainian', + 'vi' => 'vietnamese', + 'sw' => 'swahili', + 'yo' => 'yoruba' + ); + + $system_available_languages = & get_language_folder_list(); + + $accept_languages = strtolower(str_replace('_', '-', $_SERVER['HTTP_ACCEPT_LANGUAGE'])); + + foreach ($language_index as $code => $language) { + if (strpos($accept_languages, $code) === 0) { + if (!empty($system_available_languages[$language])) { + return $language; + } } - if (is_dir($dirname.$entries)) { - $result[$entries] = ucwords(str_replace($search, $replace_with, $entries)); + } + + $user_agent = strtolower(str_replace('_', '-', $_SERVER['HTTP_USER_AGENT'])); + + foreach ($language_index as $code => $language) { + if (preg_match("/[[( ]{$code}[;,_-)]/", $user_agent)) { + if (!empty($system_available_languages[$language])) { + return $language; + } } } - closedir($handle); - asort($result); + + return 'english'; +} + +/** + * Returns a list of language directories. + */ +function & get_language_folder_list() { + static $result; + if (!is_array($result)) { + $result = array(); + $exceptions = array('.', '..', 'CVS', '.svn'); + $search = array('_latin', '_unicode', '_corporate', '_org' , '_KM', '_'); + $replace_with = array(' (Latin)', ' (unicode)', ' (corporate)', ' (org)', ' (KM)', ' '); + $dirname = api_get_path(SYS_LANG_PATH); + $handle = opendir($dirname); + while ($entries = readdir($handle)) { + if (in_array($entries, $exceptions)) { + continue; + } + if (is_dir($dirname.$entries)) { + $result[$entries] = ucwords(str_replace($search, $replace_with, $entries)); + } + } + closedir($handle); + asort($result); + } return $result; }