DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:browserlanguagedetection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tips:browserlanguagedetection [2013-09-25 17:40] – [A Improved Using of the top Tip] littlebattips:browserlanguagedetection [2018-04-24 01:41] (current) Dr-Yukon
Line 1: Line 1:
 ====== Browser Language Detection ====== ====== Browser Language Detection ======
 +
 +:!: OBSOLETE
 +
 To have the Wiki language being select based on available languages set on user's browser, perform these actions: To have the Wiki language being select based on available languages set on user's browser, perform these actions:
  
Line 10: Line 13:
 with with
 <code php> <code php>
-// $conf['lang'       = 'en';              //your language+$conf['lang'       = 'en';              //your language
 $conf['lang_default'] = 'pt'; // default language $conf['lang_default'] = 'pt'; // default language
 $conf['lang_enabled'] = array('pt', 'es', 'de', 'en'); $conf['lang_enabled'] = array('pt', 'es', 'de', 'en');
Line 60: Line 63:
 </code> </code>
 ---- ----
-Edit **inc/parser.php**, and replace+Edit **inc/parser.php**, **doku.php** and **media.php** /this file is obsolete now/ in the same way, and replace
 <code php> <code php>
 require_once("lang/en/lang.php"); require_once("lang/en/lang.php");
Line 74: Line 77:
 </code> </code>
 ---- ----
-Edit **doku.php**, and replace+Edit **conf/dokuwiki.php** again, and comment %%$conf['lang']%% line:
 <code php> <code php>
-  require_once("lang/en/lang.php"); +// $conf['lang'       = 'en';              //your language
-  require_once("lang/".$conf['lang']."/lang.php");+
 </code> </code>
-with 
-<code php> 
-  require_once("lang/en/lang.php"); 
-  // DWD Modify -> Add: Set Language from Browser Language Settings. 
-  $conf['lang'] = detect_browser_language(); 
-  // DWD Modify End. 
-  require_once("lang/".$conf['lang']."/lang.php"); 
-</code> 
----- 
-Edit **media.php**, and replace 
-<code php> 
-require_once("lang/en/lang.php"); 
-require_once("lang/".$conf['lang']."/lang.php"); 
-</code> 
-with 
-<code php> 
-require_once("lang/en/lang.php"); 
-// DWD Modify -> Add: Set Language from Browser Language Settings. 
-$conf['lang'] = detect_browser_language(); 
-// DWD Modify End. 
-require_once("lang/".$conf['lang']."/lang.php"); 
-</code> 
- 
- 
- 
  
 +==== Discussion ====
  
-===== Discussion ===== 
 I prefer this new function rather than the old one because it is more precise in parsing the browser acceptable languages and because it either returns a language found by match or the default language. Although it seems the former code was also fixed to return the same results. It also doesn't convert the HTTP_ACCEPT_LANGUAGE to lowercase, which I didn't see as necessary. -- 2004.11.03. I prefer this new function rather than the old one because it is more precise in parsing the browser acceptable languages and because it either returns a language found by match or the default language. Although it seems the former code was also fixed to return the same results. It also doesn't convert the HTTP_ACCEPT_LANGUAGE to lowercase, which I didn't see as necessary. -- 2004.11.03.
  
Line 335: Line 312:
 ===== A Improved Using of the top Tip ===== ===== A Improved Using of the top Tip =====
  
-I use Release 2013-05-10a "Weatherwax" and [[http://www.dokuwiki.org/plugin:translation|Translation Plugin]] to set a multi-language wiki. User should visit different home page according to his browser's accepted language priority. I found some shortage of the top tip below:+I use Release 2013-05-10a "Weatherwax" and [[plugin:translation|Translation Plugin]] to set a multi-language wiki. User should visit different home page according to his browser's accepted language priority. I found some shortage of the top tip below:
   * Can't be used in current DokuWiki;   * Can't be used in current DokuWiki;
   * Can't match language code correctly sometime, e.g., browser accepted languages "zh-tw,zh,en-us,en" will match pattern string "zh" in the available languages "zh,zh-tw,en", so the highest priority browser accepted language "zh-tw" is missed;   * Can't match language code correctly sometime, e.g., browser accepted languages "zh-tw,zh,en-us,en" will match pattern string "zh" in the available languages "zh,zh-tw,en", so the highest priority browser accepted language "zh-tw" is missed;
Line 436: Line 413:
  
 -- posted 2013-09-25 [[http://www.learndiary.com/mobilemate/|MobileMate created by littlebat]] -- posted 2013-09-25 [[http://www.learndiary.com/mobilemate/|MobileMate created by littlebat]]
 +
 +==== Discussion ====
 +Please post your comment about "A Improved Using of the top Tip" below.
 +
 +I think there are two goals with the above - one appears to be allow for translation and the other is simply to provide a per-user localised interface for the users of dokuwiki.
 +
 +I want the latter function and I achieved it by using the detect_browser_language() function above. I placed it in inc/init.php
 +I then hacked init_lang (in init.php). I haven't really used dokuwiki before so please can someone let me know a better place to "hack" this browser language override.
 +
 +
 +<code>
 +function init_lang($langCode) {
 +    //prepare language array
 +    global $lang, $config_cascade;
 +    $lang = array();
 +    $langCode = detect_browser_language();   // HACK this line in to override language setting with browser language
 +
 +    //load the language files
 +    require(DOKU_INC.'inc/lang/en/lang.php');
 +    foreach ($config_cascade['lang']['core'] as $config_file) {
 +        if (@file_exists($config_file . 'en/lang.php')) {
 +            include($config_file . 'en/lang.php');
 +        }
 +    }
 +
 +    if ($langCode && $langCode != 'en') {
 +        if (file_exists(DOKU_INC."inc/lang/$langCode/lang.php")) {
 +            require(DOKU_INC."inc/lang/$langCode/lang.php");
 +        }
 +        foreach ($config_cascade['lang']['core'] as $config_file) {
 +            if (@file_exists($config_file . "$langCode/lang.php")) {
 +                include($config_file . "$langCode/lang.php");
 +            }
 +        }
 +    }
 +}
 +</code> (dylan 2016/05/19)
tips/browserlanguagedetection.1380123658.txt.gz · Last modified: 2013-09-25 17:40 by littlebat

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki