Factorize functions into a separate file in custompages scripts as recommended by #scrutinizer

pull/2487/head
Yannick Warnier 9 years ago
parent 3b433e2036
commit 3c254dc334
  1. 37
      custompages/language.inc.php
  2. 33
      custompages/language.php

@ -38,40 +38,9 @@ function get_preferred_language($available_langs) {
// No match
return null;
}
/**
* Wrapper function for the get_lang function
* use this if you want to avoid translation caching issues
* Get a language variable in a specific language
*/
function cp_get_lang($variable) {
return get_lang($variable, null, $_SESSION['user_language_choice']);
function custompages_get_lang($variable) {
return get_lang($variable, null, $_SESSION['user_language_choice']);
}
/**
* Code
*/
// Note that Chamilo languages are expressed as full english names, not 2-characters codes
// e.g. 'english' instead of 'en', 'french' instead of 'fr', ...
// We need a matching array. Note the value for the null key, which is the default language.
// Also note that this is an example matchin array, not all languages are present.
$chamilo_langs = array(null => 'english', 'en' => 'english', 'fr' => 'french', 'es' => 'spanish');
// Which of these can we actually pick from ?
$available_langs = array('en','fr');
// Let's find out which language to serve to this particular browser
$lang_match = $chamilo_langs[get_preferred_language($available_langs)];
// Chamilo overrides this parameters at some places, e.g. in the logout link
if (isset($_REQUEST['language']) && !empty($_REQUEST['language']) && in_array($_REQUEST['language'], $chamilo_langs)) {
$lang_match = $_REQUEST['language'];
}
// Maybe a language had already been selected, we should honor this
if (isset($_SESSION['user_language_choice']) && in_array($_SESSION['user_language_choice'], $chamilo_langs)) {
$lang_match = $_SESSION['user_language_choice'];
}
// We need to set the relevant session variables to the best match, to use Chamilo's i18n lib.
$_user['language'] = $lang_match;
$_SESSION['user_language_choice'] = $lang_match;
?>

@ -5,37 +5,12 @@
* logged in yet
* @package chamilo.custompages
*/
/**
* Get the preferred language base on the browser headers
*/
function get_preferred_language($available_langs) {
$langs = array();
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $httplang) {
$rawlang = explode(';q=', $httplang);
if (strpos($rawlang[0], '-') !== FALSE) {
$rawlang[0] = substr($rawlang[0], 0, strpos($rawlang[0], '-'));
}
if (count($rawlang) == 1) {
$rawlang[1] = 1.0;
}
$langs[$rawlang[1]] = $rawlang[0];
}
krsort($langs, SORT_NUMERIC);
foreach($langs as $weight => $code) {
if (in_array($code, $available_langs)) {
return $code;
}
}
return null;
}
/**
* Get a language variable in a specific language
*/
function custompages_get_lang($variable) {
return get_lang($variable, null, $_SESSION['user_language_choice']);
}
// Get helper functions
require_once __DIR__ . '/language.inc.php';
// Define the languages you want to make available for auto-detection here
$available_langs = array('en', 'fr', 'es', 'gl', 'eu');
// Define the translation of these language codes to Chamilo languages
$chamilo_langs = array(
null => 'english',
'en' => 'english',

Loading…
Cancel
Save