Translations of this page?:

Multilingual sites with DokuWiki (patch)

Appendix to article "Multilingual sites with DokuWiki".

diff -uNr dokuwiki-2005-09-22.orig/conf/dokuwiki.php dokuwiki-2005-09-22/conf/dokuwiki.php
--- dokuwiki-2005-09-22.orig/conf/dokuwiki.php	2005-09-22 21:21:13.000000000 +0400
+++ dokuwiki-2005-09-22/conf/dokuwiki.php	2006-02-20 11:45:18.000000000 +0300
@@ -11,7 +11,8 @@
 
 $conf['umask']       = 0111;              //set the umask for new files
 $conf['dmask']       = 0000;              //directory mask accordingly
-$conf['lang']        = 'en';              //your language
+$conf['lang_default']= 'en';              //default language
+$conf['lang_enabled']= array('en');       //supported languages
 $conf['basedir']     = '';                //absolute dir from serveroot - blank for autodetection
 $conf['baseurl']     = '';                //URL to server including protocol - blank for autodetect
 $conf['savedir']     = './data';          //where to store all the files
@@ -86,6 +87,10 @@
                                          //  'page'    - the revised page itself
                                          //  'rev'     - page showing all revisions
                                          //  'current' - most recent revision of page
+$conf['multilingual']= 0;                //multilingual mode
+                                         //    0 - off; all content is in default language
+                                         //    1 - multilingual interface only
+                                         //    2 - multilingual interface and content
 
 //Set target to use when creating links - leave empty for same window
 $conf['target']['wiki']      = '';
diff -uNr dokuwiki-2005-09-22.orig/doku.php dokuwiki-2005-09-22/doku.php
--- dokuwiki-2005-09-22.orig/doku.php	2005-09-22 21:21:06.000000000 +0400
+++ dokuwiki-2005-09-22/doku.php	2006-02-20 12:14:57.000000000 +0300
@@ -26,6 +26,7 @@
   $RANGE = $_REQUEST['lines'];
   $HIGH  = $_REQUEST['s'];
   if(empty($HIGH)) $HIGH = getGoogleQuery();
+  if(isset($_GET['lang'])) $LANG = $_GET['lang'];
 
   $TEXT  = cleanText($_POST['wikitext']);
   $PRE   = cleanText($_POST['prefix']);
@@ -50,6 +51,10 @@
     exit;
   }
 
+  // detect current language settings
+  $conf['lang'] = detect_language();
+  change_language();
+
   //make infos about the selected page available
   $INFO = pageinfo();
 
diff -uNr dokuwiki-2005-09-22.orig/inc/common.php dokuwiki-2005-09-22/inc/common.php
--- dokuwiki-2005-09-22.orig/inc/common.php	2005-10-01 17:45:54.000000000 +0400
+++ dokuwiki-2005-09-22/inc/common.php	2006-02-20 12:06:12.000000000 +0300
@@ -1146,4 +1146,75 @@
   return str_replace('\\'.$char,$char,$string);
 }
 
+/**
+ * Detect language for current page
+ *
+ * @author Alexey Remizov <alexey@remizov.pp.ru>
+ */
+function detect_language(){
+  global $ID;
+  global $LANG;
+  global $conf;
+
+  // check multilingual settings
+  if($conf['multilingual'] == 0){          // if multilingual mode switched off,
+    return $conf['lang_default'];          // use default language as current
+  }
+  // process multilingual settings
+  if($conf['multilingual'] == 2){ // if full multilingual mode used ...
+    $lang_candidate = gettopNS($ID); // try to get current language from page ID
+    if(in_array($lang_candidate,$conf['lang_enabled'])){ // if page ID forces language settings ...
+      return $lang_candidate;                            // use top NS as current language
+    }
+  }
+  // if valid GET parameter is presented, use it as current language
+  if(isset($LANG) && in_array($LANG,$conf['lang_enabled'])) return $LANG;
+  // try to detect user's language settings
+  // .. check cookie  
+  if(isset($_COOKIE['DokuLang'])){                          // if present
+    if(in_array($_COOKIE['DokuLang'],$conf[lang_enabled])){ // valid DokuLang cookie ...
+      return $_COOKIE['DokuLang'];                          // use cookie's value as current language
+    }
+  }
+  // .. check browser's variables
+  if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
+    $accept_language = explode(';',$_SERVER['HTTP_ACCEPT_LANGUAGE'],2);
+    $accept_language = explode(',',$accept_language[0]);
+    foreach($accept_language as $lang_candidate){
+      if(in_array($lang_candidate,$conf['lang_enabled'])){
+        return $lang_candidate;
+      }
+    }
+  }
+  // if all detections fails, return default language
+  return $conf['lang_default'];
+}
+
+/**
+ * Set DokuWiki and user environments
+ * according to $conf['lang']
+ *
+ * @author Alexey Remizov <alexey@remizov.pp.ru>
+ */
+function change_language(){
+  global $conf;
+  global $lang;
+
+  setcookie('DokuLang',$conf['lang'],2147483647);
+  $_SESSION['lang'] = $conf['lang'];
+  require(DOKU_INC.'inc/lang/en/lang.php');
+  include(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
+}
+
+/**
+ * Returns name of start wikipage
+ *
+ * @author Alexey Remizov <alexey@remizov.pp.ru>
+ */
+function get_start(){
+  global $conf;
+
+  return ($conf['multilingual'] == 2) ? $conf['lang'].':'.$conf['start'] : $conf['start'];
+}
+
 //Setup VIM: ex: et ts=2 enc=utf-8 :
diff -uNr dokuwiki-2005-09-22.orig/inc/init.php dokuwiki-2005-09-22/inc/init.php
--- dokuwiki-2005-09-22.orig/inc/init.php	2005-09-22 21:21:13.000000000 +0400
+++ dokuwiki-2005-09-22/inc/init.php	2006-02-20 11:54:03.000000000 +0300
@@ -20,14 +20,6 @@
   require_once(DOKU_CONF.'dokuwiki.php');
   @include_once(DOKU_CONF.'local.php');
 
-  //prepare language array
-  global $lang;
-  $lang = array();
-
-  //load the language files
-  require_once(DOKU_INC.'inc/lang/en/lang.php');
-  require_once(DOKU_INC.'inc/lang/'.$conf['lang'].'/lang.php');
-
   // define baseURL
   if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
   if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
diff -uNr dokuwiki-2005-09-22.orig/inc/pageutils.php dokuwiki-2005-09-22/inc/pageutils.php
--- dokuwiki-2005-09-22.orig/inc/pageutils.php	2005-09-22 21:21:13.000000000 +0400
+++ dokuwiki-2005-09-22/inc/pageutils.php	2006-02-20 11:57:49.000000000 +0300
@@ -50,7 +50,10 @@
     $id = urldecode($id);
     $id = cleanID($id);
   }
-  if(empty($id) && $param=='id') $id = $conf['start'];
+  if(empty($id)){
+    if($conf['multilingual'] == 2) $conf['lang'] = detect_language();
+    $id = get_start();
+  }
 
   return $id;
 }
@@ -98,6 +101,18 @@
 }
 
 /**
+ * Return top namespace of a wiki ID
+ *
+ * @author Alexey Remizov <alexey@remizov.pp.ru>
+ */
+function gettopNS($id){
+ if(strpos($id,':')!==false){
+   return substr($id,0,strpos($id,':'));
+ }
+ return $id;
+}
+
+/**
  * Return namespacepart of a wiki ID
  *
  * @author Andreas Gohr <andi@splitbrain.org>
diff -uNr dokuwiki-2005-09-22.orig/inc/template.php dokuwiki-2005-09-22/inc/template.php
--- dokuwiki-2005-09-22.orig/inc/template.php	2005-10-21 15:43:58.000000000 +0400
+++ dokuwiki-2005-09-22/inc/template.php	2006-02-20 17:02:03.771503328 +0300
@@ -269,13 +269,13 @@
 function tpl_getparent($ID){
   global $conf;
 
-  if ($ID != $conf['start']) {
+  if ($ID != get_start()) {
     $idparts = explode(':', $ID);
     $pn = array_pop($idparts);    // get the page name
 
     for ($n=0; $n < 2; $n++) {
       if (count($idparts) == 0) {
-        $ID = $conf['start'];     // go to topmost page
+        $ID = get_start();        // go to topmost page
         break;
       }else{
         $ns = array_pop($idparts);     // get the last part of namespace
@@ -555,8 +555,8 @@
   print $lang['breadcrumb'].': ';
 
   //always print the startpage
-  if( $a_part[0] != $conf['start'] )
-    tpl_link(wl($conf['start']),$conf['start'],'title="'.$conf['start'].'"');
+  if( $a_part[0] != get_start() )
+    tpl_link(wl(get_start()),get_start(),'title="'.get_start().'"');
 
   $page = '';
   foreach ($parts as $part){
@@ -927,4 +927,27 @@
   print "<img $att />";
 }
 
+/**
+ * Print link to the page in specified language
+ *
+ * @author Alexey Remizov <alexey@remizov.pp.ru>
+ */
+function tpl_lang($lang,$title){
+  global $ID;
+  global $conf;
+
+  if(($conf['multilingual'] == 0) || ($lang == $conf['lang'])){
+    print '<span class="language"'.(($lang == $conf['lang'])?' id="current"':'').'>'.$title.'</span>';
+    return;
+  }
+  if($conf['multilingual'] == 2){
+    $lang_page = gettopNS($ID);
+    if(in_array($lang_page,$conf['lang_enabled'])){
+      print '<a class="language" href="'.DOKU_BASE.cleanID(substr_replace($ID,$lang,0,strlen($lang_page))).'" title="'.$title.'">'.$title.'</a>';
+      return;
+    }
+  }
+  print '<a class="language" href="'.DOKU_BASE.$ID.'?lang='.$lang.'" title="'.$title.'">'.$title.'</a>';
+}
+
 //Setup VIM: ex: et ts=2 enc=utf-8 :
tips/multilingual_content/patch.txt · Last modified: 2008/08/09 16:31 by clorr
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Imprint Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsGitXRefTranslate