tips:browserlanguagedetection
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tips:browserlanguagedetection [2009-05-03 09:35] – 123.255.41.127 | tips: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[' | + | $conf[' |
$conf[' | $conf[' | ||
$conf[' | $conf[' | ||
Line 60: | Line 63: | ||
</ | </ | ||
---- | ---- | ||
- | Edit **inc/ | + | Edit **inc/ |
<code php> | <code php> | ||
require_once(" | require_once(" | ||
Line 74: | Line 77: | ||
</ | </ | ||
---- | ---- | ||
- | Edit **doku.php**, and replace | + | Edit **conf/ |
<code php> | <code php> | ||
- | require_once(" | + | // $conf[' |
- | require_once(" | + | |
</ | </ | ||
- | with | ||
- | <code php> | ||
- | require_once(" | ||
- | // DWD Modify -> Add: Set Language from Browser Language Settings. | ||
- | $conf[' | ||
- | // DWD Modify End. | ||
- | require_once(" | ||
- | </ | ||
- | ---- | ||
- | Edit **media.php**, | ||
- | <code php> | ||
- | require_once(" | ||
- | require_once(" | ||
- | </ | ||
- | with | ||
- | <code php> | ||
- | require_once(" | ||
- | // DWD Modify -> Add: Set Language from Browser Language Settings. | ||
- | $conf[' | ||
- | // DWD Modify End. | ||
- | require_once(" | ||
- | </ | ||
- | |||
- | |||
- | |||
+ | ==== 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' | 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' | ||
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 / | ||
+ | |||
+ | * :!: **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 / | ||
+ | <file php> | ||
+ | <?php | ||
+ | //added for language support | ||
+ | $conf[' | ||
+ | if (isset($_GET[' | ||
+ | $conf[' | ||
+ | setcookie(" | ||
+ | $getrid=" | ||
+ | $script = str_replace( $getrid, "", | ||
+ | $loc=" | ||
+ | header( $loc); | ||
+ | } | ||
+ | if (isset($_COOKIE[" | ||
+ | $conf[' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | * :!: **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: | ||
+ | |||
+ | -- posted 2009-07-31 by [[http:// | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== A Improved Using of the top Tip ===== | ||
+ | |||
+ | I use Release 2013-05-10a " | ||
+ | * Can't be used in current DokuWiki; | ||
+ | * Can't match language code correctly sometime, e.g., browser accepted languages " | ||
+ | * Should according to the browser' | ||
+ | |||
+ | And, I use nice URLs of .htaccess file, the two lines must be commented as below: | ||
+ | < | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | I only edit one file " | ||
+ | <code php> | ||
+ | <?php | ||
+ | /** | ||
+ | * Forwarder to doku.php | ||
+ | * | ||
+ | * @license | ||
+ | * @author | ||
+ | */ | ||
+ | |||
+ | $conf[' | ||
+ | $conf[' | ||
+ | // 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('/ | ||
+ | $languages = strtolower($languages); | ||
+ | // print($languages.'< | ||
+ | $bol_language_detected = false; | ||
+ | | ||
+ | $browser_langsarray = preg_split("/ | ||
+ | // print_r($browser_langsarray); | ||
+ | // print('< | ||
+ | |||
+ | foreach ($browser_langsarray as $tmp_browser_lang) { | ||
+ | if ($tmp_browser_lang == ' | ||
+ | $tmp_browser_lang = ' | ||
+ | } | ||
+ | |||
+ | if ($tmp_browser_lang == ' | ||
+ | $tmp_browser_lang = ' | ||
+ | } | ||
+ | |||
+ | if (substr($tmp_browser_lang, | ||
+ | $tmp_browser_lang = ' | ||
+ | } | ||
+ | | ||
+ | // print(' | ||
+ | foreach ($conf[' | ||
+ | // print(' | ||
+ | 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[' | ||
+ | } // If browser set with compatible language. | ||
+ | } // detect_browser_language | ||
+ | // DWD Modify End. | ||
+ | |||
+ | $browser_lang = detect_browser_language(); | ||
+ | // | ||
+ | |||
+ | if ($browser_lang == ' | ||
+ | header(" | ||
+ | } | ||
+ | |||
+ | if ($browser_lang == ' | ||
+ | header(" | ||
+ | } | ||
+ | |||
+ | if ($browser_lang == ' | ||
+ | header(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | -- posted 2013-09-25 [[http:// | ||
+ | |||
+ | ==== 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/ | ||
+ | I then hacked init_lang (in init.php). I haven' | ||
+ | |||
+ | |||
+ | < | ||
+ | function init_lang($langCode) { | ||
+ | //prepare language array | ||
+ | global $lang, $config_cascade; | ||
+ | $lang = array(); | ||
+ | $langCode = detect_browser_language(); | ||
+ | |||
+ | //load the language files | ||
+ | require(DOKU_INC.' | ||
+ | foreach ($config_cascade[' | ||
+ | if (@file_exists($config_file . ' | ||
+ | include($config_file . ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if ($langCode && $langCode != ' | ||
+ | if (file_exists(DOKU_INC." | ||
+ | require(DOKU_INC." | ||
+ | } | ||
+ | foreach ($config_cascade[' | ||
+ | if (@file_exists($config_file . " | ||
+ | include($config_file . " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |
tips/browserlanguagedetection.1241336107.txt.gz · Last modified: 2009-07-31 11:42 (external edit)