From 8a0c8bd8d3d0f0d994cac241d8fbefee3dc3b13f Mon Sep 17 00:00:00 2001 From: Olivier PEREZ Date: Tue, 20 Jan 2015 00:05:43 +0100 Subject: [PATCH] Make language selection work better --- app/classes/Framadate/Utils.php | 6 ++-- app/inc/i18n.php | 64 +++++++++++++++++---------------- app/inc/smarty.php | 2 +- bandeaux.php | 4 +-- choix_autre.php | 2 +- tpl/head.tpl | 4 +-- tpl/header.tpl | 2 +- 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/app/classes/Framadate/Utils.php b/app/classes/Framadate/Utils.php index f2358a0..dc14eca 100644 --- a/app/classes/Framadate/Utils.php +++ b/app/classes/Framadate/Utils.php @@ -50,10 +50,10 @@ class Utils { * @deprecated */ public static function print_header($title = '') { - global $lang; + global $html_lang; echo ' - + '; @@ -72,7 +72,7 @@ class Utils { - + '; if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/nav/nav.js")) { echo ''; diff --git a/app/inc/i18n.php b/app/inc/i18n.php index 587a704..e62cd7b 100644 --- a/app/inc/i18n.php +++ b/app/inc/i18n.php @@ -19,59 +19,61 @@ asort($ALLOWED_LANGUAGES); -if (isset($_POST['lang']) && is_string($_POST['lang']) && in_array($_POST['lang'], array_keys($ALLOWED_LANGUAGES)) ) { - $mlocale = $_POST['lang'] ; - setcookie('lang' , $_POST['lang'], time()+60*5, '/'); -} elseif ( isset($_COOKIE['lang']) && is_string($_COOKIE['lang']) && in_array($_COOKIE['lang'], array_keys($ALLOWED_LANGUAGES)) ) { - $mlocale = $_COOKIE['lang'] ; +if (isset($_POST['lang']) && is_string($_POST['lang']) && in_array($_POST['lang'], array_keys($ALLOWED_LANGUAGES))) { + $mlocale = $_POST['lang']; + $_SESSION['lang'] = $_POST['lang']; +} elseif (isset($_SESSION['lang']) && is_string($_SESSION['lang']) && in_array($_SESSION['lang'], array_keys($ALLOWED_LANGUAGES))) { + $mlocale = $_SESSION['lang']; } else { $mlocale = LANGUE; // Replace config language by browser language if possible - foreach ($ALLOWED_LANGUAGES as $k => $v ) { - if (substr($k,0,2)==substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) { + foreach ($ALLOWED_LANGUAGES as $k => $v) { + if (substr($k, 0, 2) == substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)) { $mlocale = $k; + break; } } } -$locale = $mlocale . '.utf8';//unix format -if (strtoupper(substr(PHP_OS,0,3))=='WIN'){ +/* Tell PHP which locale to use */ +$domain = 'Studs'; +$locale = $mlocale . '.utf8'; //unix format + +if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') { putenv("LC_ALL=$mlocale"); //Windows env. needed to switch between languages - switch ($mlocale){ - case 'fr_FR' : $locale = "fra";break; //$locale in windows locale format, needed to use php function that handle text : strftime() - case 'en_GB' : $locale = "english";break; //see http://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.90%29.aspx - case 'de_DE' : $locale = "deu";break; - case 'es_ES' : $locale = "esp";break; + switch ($mlocale) { + case 'fr_FR' : + $locale = "fra"; + break; //$locale in windows locale format, needed to use php function that handle text : strftime() + case 'en_GB' : + $locale = "english"; + break; //see http://msdn.microsoft.com/en-us/library/39cwe7zf%28v=vs.90%29.aspx + case 'de_DE' : + $locale = "deu"; + break; + case 'es_ES' : + $locale = "esp"; + break; } } - -putenv('LANGUAGE=');//sert à quoi? +putenv('LANG=' . $locale); setlocale(LC_ALL, $locale); -setlocale(LC_TIME, $locale); -setlocale(5, $locale); // 5 = LC_MESSAGES (but LC_MESSAGES is not always present) - -$domain = 'Studs'; -bindtextdomain($domain, 'locale'); +bindtextdomain($domain, ROOT_DIR . 'locale'); bind_textdomain_codeset($domain, 'UTF-8'); textdomain($domain); -/* temp, for compatibility :*/ -$a = explode('_', $mlocale); -$_SESSION['langue'] = strtoupper($a[0]); - -/* */ -$lang = ($_SESSION['langue']!='') ? strtolower($_SESSION['langue']) : 'fr'; - +/* */ +$html_lang = substr($locale, 0, 2); /* Date Format */ $date_format['txt_full'] = _('%A, den %e. %B %Y'); //summary in choix_date.php and removal date in choix_(date|autre).php $date_format['txt_short'] = _('%A %e %B %Y'); // radio title $date_format['txt_day'] = _('%a %e'); $date_format['txt_date'] = _('%Y-%m-%d'); -if (strtoupper(substr(PHP_OS,0,3))=='WIN'){ //%e can't be used on Windows platform, use %#d instead - foreach($date_format as $k => $v) { - $date_format[$k] = preg_replace('#(? $v) { + $date_format[$k] = preg_replace('#(?assign('SERVER_URL', Utils::get_server_name()); $smarty->assign('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']); $smarty->assign('TITLE_IMAGE', IMAGE_TITRE); $smarty->assign('use_nav_js', file_exists($_SERVER['DOCUMENT_ROOT'] . '/nav/nav.js')); -$smarty->assign('lang', $lang); +$smarty->assign('html_lang', $html_lang); $smarty->assign('langs', $ALLOWED_LANGUAGES); $smarty->assign('date_format', $date_format); diff --git a/bandeaux.php b/bandeaux.php index a2c4caf..d9c2bd2 100644 --- a/bandeaux.php +++ b/bandeaux.php @@ -57,12 +57,12 @@ function bandeau_titre($titre) function liste_lang() { - global $ALLOWED_LANGUAGES; global $lang; + global $ALLOWED_LANGUAGES; global $html_lang; $str = ''; foreach ($ALLOWED_LANGUAGES as $k => $v ) { - if (substr($k,0,2)==$lang) { + if (substr($k,0,2)==$html_lang) { $str .= '' . "\n" ; } else { $str .= '' . "\n" ; diff --git a/choix_autre.php b/choix_autre.php index c16ed50..9c04118 100644 --- a/choix_autre.php +++ b/choix_autre.php @@ -232,7 +232,7 @@ if (empty($_SESSION['form']->title) || empty($_SESSION['form']->admin_name) || (

' . _("To make a generic poll you need to propose at least two choices between differents subjects.") . '

' . _("You can add or remove additional choices with the buttons") . ' ' . _("Remove") . ' ' . _("Add") . '

'; if ($config['user_can_add_img_or_link']) { - echo '

' . _("It's possible to propose links or images by using ") . '' . _("the Markdown syntax") . '.

'; + echo '

' . _("It's possible to propose links or images by using ") . '' . _("the Markdown syntax") . '.

'; } echo ' ' . "\n"; diff --git a/tpl/head.tpl b/tpl/head.tpl index 05c5b58..3b8bbac 100644 --- a/tpl/head.tpl +++ b/tpl/head.tpl @@ -1,5 +1,5 @@ - + @@ -17,7 +17,7 @@ - + {if !empty($nav_js)} diff --git a/tpl/header.tpl b/tpl/header.tpl index 7dbb582..1fa06c2 100644 --- a/tpl/header.tpl +++ b/tpl/header.tpl @@ -4,7 +4,7 @@