From 9375fc45e667ec0f4b379b5cc0cf49dd3aa336a2 Mon Sep 17 00:00:00 2001 From: LarsGit223 Date: Sun, 13 Jan 2019 13:32:44 +0100 Subject: [PATCH] Added option to configure the UI language per user --- multilingual/action.php | 55 ++++++++++++++++++++-------------- multilingual/conf/default.php | 1 + multilingual/conf/metadata.php | 1 + 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/multilingual/action.php b/multilingual/action.php index 28740a2..1dadad2 100644 --- a/multilingual/action.php +++ b/multilingual/action.php @@ -39,10 +39,8 @@ function getInfo(){ function register(&$controller) { if($this->getConf('start_redirect')) { $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'multilingual_start'); - } - if($this->getConf('use_browser_lang')) { - $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'multilingual_ui'); } + $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'multilingual_ui'); } function multilingual_start($event, $args) { @@ -63,25 +61,38 @@ function multilingual_ui(&$event, $args) { global $ID; global $lang; global $conf; - - $enabled_languages = preg_split("/ /", $this->getConf('enabled_langs') ); - $languages = preg_split("/,/", preg_replace('/\(;q=\d+\.\d+\)/i', '', getenv('HTTP_ACCEPT_LANGUAGE'))); - // Could use a check here against the dokuwiki's supported languages - $old_language = $conf['lang']; - foreach ($languages as $language) { - if (in_array($language, $enabled_languages)) { - $conf['lang'] = $language; - break; - } - } - // Rebuild language array if necessary - if ( $old_language != $conf['lang'] ) { - $lang = array(); - require_once(DOKU_INC.'inc/lang/en/lang.php'); - if ( $conf['lang'] && $conf['lang'] != 'en' ) { - require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php'); - } - } + global $INFO; + + $old_language = $conf['lang']; + + $user_settings = $this->getConf('user_settings'); + $pattern = '/'.$INFO['client'].'="(.*?)";/'; + $rebuild = false; + if (preg_match($pattern, $user_settings, $matches) == 1) { + $options = explode(';', $matches[1]); + if (!empty($options[0])) { + dbglog('Plugin multilangual: changing language for user "'.$INFO['client'].'" to "'.$options[0].'"'); + $conf['lang'] = $options[0]; + $rebuild = true; + } + } + if ($rebuild == false && $this->getConf('use_browser_lang')) { + $enabled_languages = preg_split("/ /", $this->getConf('enabled_langs') ); + $languages = preg_split("/,/", preg_replace('/\(;q=\d+\.\d+\)/i', '', getenv('HTTP_ACCEPT_LANGUAGE'))); + // Could use a check here against the dokuwiki's supported languages + foreach ($languages as $language) { + if (in_array($language, $enabled_languages)) { + $conf['lang'] = $language; + break; + } + } + } + if ($rebuild == true) { + // Rebuild language array if necessary + if ( $old_language != $conf['lang'] ) { + init_lang($conf['lang']); + } + } return true; } } diff --git a/multilingual/conf/default.php b/multilingual/conf/default.php index dde2422..556e2fc 100644 --- a/multilingual/conf/default.php +++ b/multilingual/conf/default.php @@ -8,4 +8,5 @@ $conf['start_redirect'] = false; // Redirect to the language start parge $conf['skiptrans'] = ''; $conf['about'] = ''; +$conf['user_settings'] = ''; diff --git a/multilingual/conf/metadata.php b/multilingual/conf/metadata.php index 34b8d3b..3a322bd 100644 --- a/multilingual/conf/metadata.php +++ b/multilingual/conf/metadata.php @@ -11,4 +11,5 @@ $meta['start_redirect'] = array('onoff'); $meta['skiptrans'] = array('string'); $meta['about'] = array('string','_pattern' => '/^(|[\w:\-]+)$/'); +$meta['user_settings'] = array(''); ?>