From 7aa4f5297db47d7287ea47943771c013761761cd Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sat, 1 Oct 2016 15:55:18 +0200 Subject: [PATCH 1/5] when the admin creates a new account, use the system language for this accounts --- mod/admin.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mod/admin.php b/mod/admin.php index 4dc6064a7d..8fb0df24c4 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -1127,18 +1127,20 @@ function admin_page_dbsync(&$a) { * @param App $a */ function admin_page_users_post(&$a){ - $pending = (x($_POST, 'pending') ? $_POST['pending'] : array()); - $users = (x($_POST, 'user') ? $_POST['user'] : array()); - $nu_name = (x($_POST, 'new_user_name') ? $_POST['new_user_name'] : ''); - $nu_nickname = (x($_POST, 'new_user_nickname') ? $_POST['new_user_nickname'] : ''); - $nu_email = (x($_POST, 'new_user_email') ? $_POST['new_user_email'] : ''); + $pending = (x($_POST, 'pending') ? $_POST['pending'] : array()); + $users = (x($_POST, 'user') ? $_POST['user'] : array()); + $nu_name = (x($_POST, 'new_user_name') ? $_POST['new_user_name'] : ''); + $nu_nickname = (x($_POST, 'new_user_nickname') ? $_POST['new_user_nickname'] : ''); + $nu_email = (x($_POST, 'new_user_email') ? $_POST['new_user_email'] : ''); + $nu_language = get_config('system', 'language'); check_form_security_token_redirectOnErr('/admin/users', 'admin_users'); if(!($nu_name==="") && !($nu_email==="") && !($nu_nickname==="")) { require_once('include/user.php'); - $result = create_user(array('username'=>$nu_name, 'email'=>$nu_email, 'nickname'=>$nu_nickname, 'verified'=>1)); + $result = create_user(array('username'=>$nu_name, 'email'=>$nu_email, + 'nickname'=>$nu_nickname, 'verified'=>1, 'language'=>$nu_language)); if(! $result['success']) { notice($result['message']); return; From 5cfca894c1c891dec0f96eb2d86aefdea1ff1ec5 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sat, 1 Oct 2016 15:55:56 +0200 Subject: [PATCH 2/5] On registration use the browser preferred language for the new account --- mod/register.php | 1 + 1 file changed, 1 insertion(+) diff --git a/mod/register.php b/mod/register.php index 6fc5887ef5..4f5652f2f7 100644 --- a/mod/register.php +++ b/mod/register.php @@ -52,6 +52,7 @@ function register_post(&$a) { $arr['blocked'] = $blocked; $arr['verified'] = $verified; + $arr['language'] = get_browser_language(); $result = create_user($arr); From aba8313b210499440eb2237baa42a503ad4d4cc3 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sat, 1 Oct 2016 15:57:37 +0200 Subject: [PATCH 3/5] don't save browser selected language on login --- include/security.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/security.php b/include/security.php index 2d6db95f4f..b52ddeb2df 100644 --- a/include/security.php +++ b/include/security.php @@ -79,11 +79,9 @@ function authenticate_success($user_record, $login_initial = false, $interactive header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"'); if($login_initial || $login_refresh) { - $l = get_browser_language(); - q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d", + q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d", dbesc(datetime_convert()), - dbesc($l), intval($_SESSION['uid']) ); From ed631ba959ee0c33d6378420c5b4b635673be3a8 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Sun, 2 Oct 2016 09:26:35 +0200 Subject: [PATCH 4/5] fallback to generic language for specific (de-CH => de) --- include/pgettext.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/pgettext.php b/include/pgettext.php index 3c389ce2e5..ed924fa337 100644 --- a/include/pgettext.php +++ b/include/pgettext.php @@ -26,16 +26,29 @@ function get_browser_language() { $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); if (count($lang_parse[1])) { + for ($i=0; $i2 ) { + if (! in_array(substr($lang_parse[1][$i], 0, 2), $lang_parse[1] ) ) { + array_push($lang_parse[1], substr($lang_parse[1][$i], 0, 2)); + if (floatval($lang_parse[4][$i])>0) + array_push($lang_parse[4], strval(floatval($lang_parse[4][$i])-0.0001)); + else + array_push($lang_parse[4], '0.9999'); + } + } + } // create a list like "en" => 0.8 $langs = array_combine($lang_parse[1], $lang_parse[4]); // set default to 1 for any without q factor foreach ($langs as $lang => $val) { if ($val === '') $langs[$lang] = 1; + if ($val === '0') $langs[$lang] = 1; } // sort list based on value arsort($langs, SORT_NUMERIC); + print_r($langs); } } From bb423ff9b4e8471e34f484ec591987305a57aab1 Mon Sep 17 00:00:00 2001 From: Tobias Diekershoff Date: Mon, 3 Oct 2016 21:12:47 +0200 Subject: [PATCH 5/5] rework fallback code --- include/pgettext.php | 53 +++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/include/pgettext.php b/include/pgettext.php index ed924fa337..f1eb75752c 100644 --- a/include/pgettext.php +++ b/include/pgettext.php @@ -1,13 +1,7 @@ 2 ) { - if (! in_array(substr($lang_parse[1][$i], 0, 2), $lang_parse[1] ) ) { - array_push($lang_parse[1], substr($lang_parse[1][$i], 0, 2)); - if (floatval($lang_parse[4][$i])>0) - array_push($lang_parse[4], strval(floatval($lang_parse[4][$i])-0.0001)); - else - array_push($lang_parse[4], '0.9999'); + $lang_list[] = strtolower($lang_parse[1][$i]); + if ( strlen($lang_parse[1][$i])>3 ) { + $dashpos = strpos($lang_parse[1][$i], '-'); + if (! in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list ) ) { + $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos)); } } } - // create a list like "en" => 0.8 - $langs = array_combine($lang_parse[1], $lang_parse[4]); - - // set default to 1 for any without q factor - foreach ($langs as $lang => $val) { - if ($val === '') $langs[$lang] = 1; - if ($val === '0') $langs[$lang] = 1; - } - - // sort list based on value - arsort($langs, SORT_NUMERIC); - print_r($langs); } } - if(isset($langs) && count($langs)) { - foreach ($langs as $lang => $v) { - if(file_exists("view/lang/$lang") && is_dir("view/lang/$lang")) { - $preferred = $lang; - break; - } + // check if we have translations for the preferred languages and pick the 1st that has + for ($i=0; $iconfig['system']['language'])) ? $a->config['system']['language'] : 'en'); }}