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:34] – 123.255.41.127 | tips:browserlanguagedetection [2018-04-24 01:41] (current) – Dr-Yukon | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Browser Language Detection ====== | ||
+ | :!: OBSOLETE | ||
+ | |||
+ | To have the Wiki language being select based on available languages set on user's browser, perform these actions: | ||
+ | |||
+ | ===== Tip ===== | ||
+ | |||
+ | Edit **conf/ | ||
+ | <code php> | ||
+ | $conf[' | ||
+ | </ | ||
+ | with | ||
+ | <code php> | ||
+ | $conf[' | ||
+ | $conf[' | ||
+ | $conf[' | ||
+ | </ | ||
+ | **Note**: sort the array %%$conf[' | ||
+ | ---- | ||
+ | Edit **inc/ | ||
+ | <code php> | ||
+ | // 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('/ | ||
+ | |||
+ | $bol_language_detected = false; | ||
+ | |||
+ | // Check if browser language is compatible with pre-defined content languages. | ||
+ | foreach ($conf[' | ||
+ | if (preg_match('/' | ||
+ | $tmp_str_language_detected = $tmp_arr_language; | ||
+ | $bol_language_detected = true; | ||
+ | break; | ||
+ | } | ||
+ | } // ForEach pre-defined content 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. | ||
+ | </ | ||
+ | ---- | ||
+ | Edit **inc/ | ||
+ | <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(" | ||
+ | </ | ||
+ | ---- | ||
+ | Edit **conf/ | ||
+ | <code php> | ||
+ | // $conf[' | ||
+ | </ | ||
+ | |||
+ | ==== 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' | ||
+ | |||
+ | :?: Has anyone been using this code, and if such with what results? | ||
+ | |||
+ | Some things to consider: | ||
+ | - All languages need to use the same language encoding or you will break things most certainly. | ||
+ | - The english language file should be included always before including any other language as this is the only file which is complete for sure | ||
+ | - store the selected language in the session perhaps ... | ||
+ | |||
+ | I'll think about to include this idea in DokuWiki. [[andi@splitbrain.org|Andi]] | ||
+ | |||
+ | If you add this in DokuWiki please make all UTF-8, that would save a lot of headache [[gullevek@gullevek.org|Clemens]] | ||
+ | |||
+ | It would be a wonderful idea! [[http:// | ||
+ | |||
+ | >I think this way should be much better to choose language | ||
+ | >\\ fredrik | ||
+ | >< | ||
+ | $bol_language_detected = false; | ||
+ | |||
+ | // Check if browser language is compatible with pre-defined content languages. | ||
+ | foreach ($languages as $tmp_arr_language) { | ||
+ | if (in_array($tmp_arr_language, | ||
+ | $tmp_str_language_detected = $tmp_arr_language; | ||
+ | $bol_language_detected = true; | ||
+ | break; | ||
+ | } | ||
+ | } // ForEach pre-defined content language.</ | ||
+ | |||
+ | > Fredrik, why the 3 changes you suggest make the code better, can you please elaborate on it? | ||
+ | * using split with preg_replace instead of just preg_replace. | ||
+ | * loop for each $languages instead of $conf[' | ||
+ | * check if in_array instead of preg_match. | ||
+ | --- // | ||
+ | |||
+ | And what happens if the user has set 2 prefered languages for example us_uk and us with different weights, the first one being more preffered. We don't have translation for the first lang but have for the second. Will it stay on en or will pick the not so preffered translation? | ||
+ | --- [[vvitkov@gmail.com|Zeridon]] 28.06.2225 | ||
+ | |||
+ | ---- | ||
+ | A quick hack (and a dirty one) for passing the language by URL to the session: In this way you can call eg http:// | ||
+ | |||
+ | // | ||
+ | <code php> | ||
+ | if (isset($_GET[' | ||
+ | $_SESSION[' | ||
+ | } | ||
+ | </ | ||
+ | // | ||
+ | <code php> | ||
+ | //comment the regular language setting | ||
+ | // | ||
+ | |||
+ | // and insert after the session_start line | ||
+ | require_once(DOKU_INC.' | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | **Another quick hack** | ||
+ | ... extending the previous one: | ||
+ | |||
+ | In file '' | ||
+ | |||
+ | <code php> | ||
+ | //load the language files | ||
+ | require_once(DOKU_INC.' | ||
+ | /* you have to comment these... | ||
+ | if ( $conf[' | ||
+ | require_once(DOKU_INC.' | ||
+ | } | ||
+ | */ | ||
+ | |||
+ | </ | ||
+ | |||
+ | put this: | ||
+ | |||
+ | <code php> | ||
+ | // HACK - Language Selection | ||
+ | if(isset($_GET[' | ||
+ | $_SESSION[' | ||
+ | } | ||
+ | if(isset($_SESSION[' | ||
+ | require_once(DOKU_INC.' | ||
+ | $conf[' | ||
+ | } else { | ||
+ | require_once(DOKU_INC.' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | and move this block: | ||
+ | |||
+ | <code php> | ||
+ | // make session rewrites XHTML compliant | ||
+ | @ini_set(' | ||
+ | |||
+ | // init session | ||
+ | session_name(" | ||
+ | if (!headers_sent()) session_start(); | ||
+ | |||
+ | // kill magic quotes | ||
+ | if (get_magic_quotes_gpc()) { | ||
+ | if (!empty($_GET)) | ||
+ | if (!empty($_POST)) | ||
+ | if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); | ||
+ | if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); | ||
+ | if (!empty($_SESSION)) remove_magic_quotes($_SESSION); | ||
+ | @ini_set(' | ||
+ | } | ||
+ | @set_magic_quotes_runtime(0); | ||
+ | @ini_set(' | ||
+ | </ | ||
+ | |||
+ | ...after this line: | ||
+ | |||
+ | <code php> | ||
+ | @include_once(DOKU_INC.' | ||
+ | </ | ||
+ | |||
+ | Then, you should be able to set the language with '' | ||
+ | |||
+ | -- [[http:// | ||
+ | Thanks Bohumir! | ||
+ | |||
+ | ---- | ||
+ | |||
+ | **Here is another one:** | ||
+ | |||
+ | without storing language in a session, but rather in a cookie and having people choose their own language for the interface instead of having browser-detection or namespace fiddling (since in reality imho nobody has the time to completely "copy and translate all content" | ||
+ | |||
+ | //in / | ||
+ | <code php> | ||
+ | if (isset($_GET[' | ||
+ | $conf[' | ||
+ | setcookie(" | ||
+ | $getrid=" | ||
+ | $script = str_replace( $getrid, "", | ||
+ | $loc=" | ||
+ | header( $loc); | ||
+ | } | ||
+ | if (isset($_COOKIE[" | ||
+ | $conf[' | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | As far as I know the configuration manager overwrites all changes to **local.conf**, | ||
+ | |||
+ | Then create some language links somewhere in your template (/ | ||
+ | |||
+ | <code php> | ||
+ | <div class=" | ||
+ | <? | ||
+ | if((strpos($_SERVER[' | ||
+ | $lref="< | ||
+ | }else{ | ||
+ | $lref="< | ||
+ | } | ||
+ | echo $lref." | ||
+ | echo $lref." | ||
+ | echo $lref." | ||
+ | echo $lref." | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | and some displaying stuff in your css | ||
+ | |||
+ | <code css> | ||
+ | div.dokuwiki .langselect { | ||
+ | font-size: 90%; | ||
+ | text-align: | ||
+ | margin-top: | ||
+ | } | ||
+ | |||
+ | div.dokuwiki .langselect a { | ||
+ | color: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ---- | ||
+ | \\ | ||
+ | **Will any of these approaches work on the latest version of Dokuwiki? :** | ||
+ | |||
+ | Many files are not in the same place and look very different. Is there a plugin to allow this or is this feature now built into Dokuwiki? | ||
+ | |||
+ | Any replies or links to recent updates would be most welcomed. | ||
+ | |||
+ | Thanks. | ||
+ | |||
+ | Posted : 2009/MAY/3 | ||
+ | |||
+ | \\ | ||
+ | ---- | ||
+ | |||
+ | 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 . " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ |