Merge pull request #2842 from tobiasd/20160929-lng

lang handling for #2752
This commit is contained in:
Michael Vogel 2016-10-04 05:22:38 +02:00 committed by GitHub
commit 860f11e71b
4 changed files with 35 additions and 34 deletions

View file

@ -1,13 +1,7 @@
<?php
require_once("include/dba.php");
/**
* translation support
*/
/**
* @brief translation support
*
* Get the language setting directly from system variables, bypassing get_config()
* as database may not yet be configured.
@ -16,8 +10,12 @@ require_once("include/dba.php");
*
*/
require_once("include/dba.php");
if(! function_exists('get_browser_language')) {
/**
* @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
*/
function get_browser_language() {
if (x($_SERVER,'HTTP_ACCEPT_LANGUAGE')) {
@ -25,32 +23,34 @@ function get_browser_language() {
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
$lang_list = [];
if (count($lang_parse[1])) {
// 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;
// go through the list of prefered languages and add a generic language
// for sub-linguas (e.g. de-ch will add de) if not already in array
for ($i=0; $i<count($lang_parse[1]); $i++) {
$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));
}
}
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
if(isset($langs) && count($langs)) {
foreach ($langs as $lang => $v) {
// check if we have translations for the preferred languages and pick the 1st that has
for ($i=0; $i<count($lang_list); $i++) {
$lang = $lang_list[$i];
if(file_exists("view/lang/$lang") && is_dir("view/lang/$lang")) {
$preferred = $lang;
break;
}
}
}
if(isset($preferred))
return $preferred;
// in case none matches, get the system wide configured language, or fall back to English
$a = get_app();
return ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
}}

View file

@ -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'])
);

View file

@ -1132,13 +1132,15 @@ function admin_page_users_post(&$a){
$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;

View file

@ -52,6 +52,7 @@ function register_post(&$a) {
$arr['blocked'] = $blocked;
$arr['verified'] = $verified;
$arr['language'] = get_browser_language();
$result = create_user($arr);