From 3e6180183bdc01edfa01dc4b966daa4081c29604 Mon Sep 17 00:00:00 2001 From: Friendika Date: Mon, 23 May 2011 17:18:36 -0700 Subject: [PATCH] improved browser language detect, set user language on login --- boot.php | 7 ++++-- include/auth.php | 4 +++- include/pgettext.php | 51 ++++++++++++++++++++++++++++++++++++++++++++ index.php | 16 +------------- mod/openid.php | 6 +++++- 5 files changed, 65 insertions(+), 19 deletions(-) diff --git a/boot.php b/boot.php index 1a6bf622c4..48d0d79ad6 100644 --- a/boot.php +++ b/boot.php @@ -4,7 +4,7 @@ set_time_limit(0); ini_set('pcre.backtrack_limit', 250000); -define ( 'FRIENDIKA_VERSION', '2.2.988' ); +define ( 'FRIENDIKA_VERSION', '2.2.989' ); define ( 'DFRN_PROTOCOL_VERSION', '2.21' ); define ( 'DB_UPDATE_VERSION', 1054 ); @@ -2707,6 +2707,8 @@ function unamp($s) { }} + + if(! function_exists('lang_selector')) { function lang_selector() { global $lang; @@ -2748,4 +2750,5 @@ function is_site_admin() { if(local_user() && x($a->user,'email') && x($a->config,'admin_email') && ($a->user['email'] === $a->config['admin_email'])) return true; return false; -}} \ No newline at end of file +}} + diff --git a/include/auth.php b/include/auth.php index 1c430406e9..382d2c6587 100644 --- a/include/auth.php +++ b/include/auth.php @@ -232,9 +232,11 @@ else { $_SESSION['cid'] = $a->cid; } + $l = get_language(); - q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1", + q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1", dbesc(datetime_convert()), + dbesc($l), intval($_SESSION['uid']) ); diff --git a/include/pgettext.php b/include/pgettext.php index 2ffee70bcc..37e09b54da 100644 --- a/include/pgettext.php +++ b/include/pgettext.php @@ -1,8 +1,59 @@ 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; + } + + // sort list based on value + arsort($langs, SORT_NUMERIC); + } + } + + if(isset($langs) && count($langs)) { + foreach ($langs as $lang => $v) { + if(file_exists("view/$lang") && is_dir("view/$lang")) { + $preferred = $lang; + break; + } + } + } + + if(isset($preferred)) + return $preferred; + + return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en'); +}} + + + // load string translation table for alternate language if(! function_exists('load_translation_table')) { diff --git a/index.php b/index.php index 05eef6c6e0..eb75585c48 100644 --- a/index.php +++ b/index.php @@ -29,22 +29,8 @@ $install = ((file_exists('.htconfig.php') && filesize('.htconfig.php')) ? false @include(".htconfig.php"); -/** - * - * Get the language setting directly from system variables, bypassing get_config() - * as database may not yet be configured. - * - * If possible, we use the value from the browser. - * - */ - -if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $langs = preg_split("/[,-]/",$_SERVER['HTTP_ACCEPT_LANGUAGE'],2); - $lang = $langs[0]; -} else { - $lang = ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en'); -} +$lang = get_language(); load_translation_table($lang); diff --git a/mod/openid.php b/mod/openid.php index c3c6c11e9e..7bf8c299c8 100644 --- a/mod/openid.php +++ b/mod/openid.php @@ -103,11 +103,15 @@ function openid_content(&$a) { $_SESSION['cid'] = $a->cid; } - q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d LIMIT 1", + $l = get_language(); + + q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1", dbesc(datetime_convert()), + dbesc($l), intval($_SESSION['uid']) ); + header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"'); if(($a->module !== 'home') && isset($_SESSION['return_url'])) goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);