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 [2009-05-03 09:35] 123.255.41.127tips: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 303: Line 280:
 ---- ----
  
 +The **Here is another one** approach worked for me. Filenames have changed and you have to edit them this way:
 +
 +  * instead of //in /conf/local.conf.protected// put the code in // /conf/local.protected.php //, this will prevent modifications by the configuration manager. Create the file if it doesn't exist.
 +
 +  * :!: **problem 1 [solved]** :!: the default language option is removed from the local.php after saving configuration. \\my workaround was to put the default language in the **lang.protected.php** file. Here's my /conf/local.protected.php: 
 +<file php>
 +<?php
 +//added for language support
 +$conf['lang'] = 'it';
 +if (isset($_GET['plang'])){
 + $conf['lang']=$_GET['plang'];
 + setcookie("plang",$_GET['plang']);
 + $getrid="plang=".$_GET['plang']; //strip plang
 + $script = str_replace( $getrid, "",$_SERVER['REQUEST_URI']);
 + $loc="location:http://".getenv(HTTP_HOST).$script;
 + header( $loc);
 +}
 +if (isset($_COOKIE["plang"])){
 + $conf['lang']=$_COOKIE["plang"];
 +}
 +</file>
 +
 +  * :!: **problem 2 [solved]** :!: conflicts with multilingual plugin browser language preference option; if you want to choose the language and you're using the plugin you have to disable the option, then everything works fine.
 +
 +**Comment:** this solution doesn't work if the browser doesn't accept cookies.
 +
 +-- posted 2009-07-31 by [[http://www.ls-lug.org/wiki/user:bjaast|bjaast]]
 +
 +----
 +
 +===== A Improved Using of the top Tip =====
 +
 +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 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;
 +  * Should according to the browser's accepted language priority to search the website's available language page, not vice versa. 
 +
 +And, I use nice URLs of .htaccess file, the two lines must be commented as below:
 +<code>
 +#RewriteRule ^$                        doku.php  [L]
 +#RewriteRule ^index.php$               doku.php
 +</code>
 +
 +I only edit one file "index.php" to redirect to the related home page. The entire "index.php" as below, welcome comment it:
 +<code php>
 +<?php
 +/**
 + * Forwarder to doku.php
 + *
 + * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 + * @author     Andreas Gohr <andi@splitbrain.org>
 + */
 + 
 +$conf['lang_default'] = 'en'; // default language
 +$conf['lang_enabled'] = array('zh', 'zh-tw', 'en');
 +// DWD Modify -> Add: Set Language from Browser Language Settings.
 +  /*
 +  #=============================================================================#
 +  # Function: detect_browser_language()                                         #
 +  #-----------------------------------------------------------------------------#
 +  # Purpose: This function detects the user browser language.                   #
 +  #   If no valid language is found then it returns the default content language#
 +  # that is set on the configuration file                                       #
 +  #                                                                             #
 +  # Arguments:                                                                  #
 +  # Optionals:                                                                  #
 +  # Result: returns a string with the content language.                         #
 +  #                                                                             #
 +  #=============================================================================#
 +  */
 +  function detect_browser_language() {
 +    global $conf;
 + 
 +    $languages = preg_replace('/(;q=\d+.\d+)/i', '', getenv('HTTP_ACCEPT_LANGUAGE'));
 +    $languages = strtolower($languages);
 +//    print($languages.'<br>');
 +    $bol_language_detected = false;
 +    
 +    $browser_langsarray = preg_split("/[^\-a-z]/", $languages);
 +//    print_r($browser_langsarray);
 +//    print('<br>');
 + 
 +    foreach ($browser_langsarray as $tmp_browser_lang) {
 +      if ($tmp_browser_lang == 'zh-cn' || $tmp_browser_lang == 'zh-sg') {
 +        $tmp_browser_lang = 'zh';
 +      }
 +     
 +      if ($tmp_browser_lang == 'zh-hk') {
 +        $tmp_browser_lang = 'zh-tw';
 +      }
 +       
 +      if (substr($tmp_browser_lang,0,3) == 'en-') {
 +        $tmp_browser_lang = 'en';
 +      }
 +                
 +//      print('tmp_browser:'.$tmp_browser_lang.'<br>');
 +      foreach ($conf['lang_enabled'] as $tmp_arr_language) {
 +//        print('tmp_enabled:'.$tmp_arr_language.'<br>');
 +        if ($tmp_browser_lang == $tmp_arr_language) {
 +          $tmp_str_language_detected = $tmp_browser_lang;
 +          $bol_language_detected = true;
 +          break 2;
 +        }
 +      } // ForEach pre-defined content language.
 +    }// ForEach browser accept language.
 +     
 +    // If browser is set with compatible language return matching content path
 +    // Else return default content language path.
 +    if ($bol_language_detected) {
 +      return $tmp_str_language_detected;
 +    } else {
 +      return $conf['lang_default'];
 +    } // If browser set with compatible language.
 +  } // detect_browser_language
 +// DWD Modify End.
 +
 +$browser_lang = detect_browser_language();
 +//print($browser_lang.'<br>');
 +
 +if ($browser_lang == 'zh') {
 +  header("Location: /mobilemate/zh/");
 +}
 +
 +if ($browser_lang == 'zh-tw') {
 +  header("Location: /mobilemate/zh-tw/");
 +}
 +
 +if ($browser_lang == 'en') {
 +  header("Location: /mobilemate/about");
 +}
 +</code>
 +
 +-- 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.1241336107.txt.gz · Last modified: 2009-07-31 11:42 (external edit)

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