[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: New Wiki 'Feature'



Hello everyone,

I have prepared a patch that should facilitate the addition of different
translations to the DW wiki. I am submitting this to the mailing list for
review.

Some words about what the patch does: it contains a new shell script
(local/localize) that can be used to add/edit/remove translations. This
stores the language code (en, fr, ...), the Wiki group (English, French,
...), the display name (English, Français, ...) and the PmWiki localization
filename (PmWikiFr.XLPage, ...) in a text file which can be loaded by the
scripts/languages.php script. This PHP script can be included in the
local/config.php script and will take care of generating the language bar.

Additionally, the script will also redirect users to the localization that
matches their browser settings and load the PmWiki specific localization. It
is based on the work that Ana did for including the language bar.

The patch is attached to this mail. I would appreciate any thoughts or
comments.

Thierry
diff -Nur wiki.orig/local/config.php wiki/local/config.php
--- wiki.orig/local/config.php	2004-11-29 17:55:05.000000000 +0100
+++ wiki/local/config.php	2005-01-16 17:05:08.000000000 +0100
@@ -85,58 +85,7 @@
 $PageTemplateFmt = "pub/skins/dw/dw.tmpl";
 
 
-#By Anja.
-function ChooseLanguage()
-{
-echo "<a class=\"wikilink\" href=\"http://women.alioth.debian.org/wiki/index.php/English/HomePage\";>English</a>"; 
-echo " - ";
-echo "<a class=\"wikilink\" href=\"http://women.alioth.debian.org/wiki/index.php/Spanish/HomePage\";>Espa�/a>";
-echo " - ";
-echo "<a class=\"wikilink\" href=\"http://women.alioth.debian.org/wiki/index.php/German/HomePage\";>Deutsch</a>";
-echo " - ";
-echo "<a class=\"wikilink\" href=\"http://women.alioth.debian.org/wiki/index.php/French/HomePage\";>Fran�s</a>";
-echo " - ";
-echo "<a class=\"wikilink\" href=\"http://women.alioth.debian.org/wiki/index.php/Portuguese/HomePage\";>Portugu�/a>";
-}
-
-/*
-List of languages with translation:
-PmWikiBg.XLPage
-PmWikiCa.XLPage
-PmWikiCs.XLPage
-PmWikiEt.XLPage
-PmWikiIt.XLPage
-PmWikiNl.XLPage
-PmWikiNo.XLPage
-PmWikiPo.XLPage
-PmWikiPtBr.XLPage
-PmWikiRo.XLPage
-PmWikiSv.XLPage
-PmWikiTr.XLPage
-PmWikiTw.XLPage
-PmWikiZhCn.XLPage
-*/
-
-
-/*
-Doesnt work :(
-XLPage('de','PmWikiDe.XLPage');
-XLPage('es','PmWikiEs.XLPage');
-XLPage('fr','PmWikiFr.XLPage');
-XLPage('ru','PmWikiRu.XLPage');
-*/
-
-#Show translate strings in browser language.
-#By Anja
-$lang=substr($HTTP_ACCEPT_LANGUAGE,0,2);
-
-if ($lang == "es")
-	XLPage('es','PmWikiEs.XLPage');
-elseif ($lang == "de")
-	XLPage('de','PmWikiDe.XLPage');
-elseif ($lang == "fr")
-	XLPage('fr','PmWikiFr.XLPage');
-elseif ($lang == "pt")
-	XLPage('pt-br','PmWikiPtBr.XLPage');
+include_once("scripts/languages.php");
+
 
 ?>
diff -Nur wiki.orig/local/languages.conf wiki/local/languages.conf
--- wiki.orig/local/languages.conf	1970-01-01 01:00:00.000000000 +0100
+++ wiki/local/languages.conf	2005-01-16 21:21:44.000000000 +0100
@@ -0,0 +1,5 @@
+en:English:English
+es:Spanish:Espa&ntilde;ol:PmWikiEs.XLPage
+de:German:Deutsch:PmWikiDe.XLPage
+fr:French:Fran&ccedil;ais:PmWikiFr.XLPage
+pt-br:Portuguese:Portugu&ecirc;s:PmWikiPtBr.XLPage
diff -Nur wiki.orig/local/localize wiki/local/localize
--- wiki.orig/local/localize	1970-01-01 01:00:00.000000000 +0100
+++ wiki/local/localize	2005-01-16 21:20:33.000000000 +0100
@@ -0,0 +1,236 @@
+#!/bin/sh -e
+#
+# Copyright (C) 2005 Thierry Reding (thierry@doppeltgemoppelt.de)
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation; either version 2 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program; if not, write to the Free Software
+#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+umask 022
+
+confdir="`pwd`/$(dirname $0)"
+conf="$confdir/languages.conf"
+
+function usage()
+{
+    cat << EOF
+usage: $0 command language_code [wiki_group] [name] [localization]
+
+where [command] is one of the following:
+
+  add        Adds a localization for the specified language code. The Wiki
+             group also needs to be specified. The name of the localization
+             defaults to the Wiki group and the localization defaults to
+             none.
+  
+  edit       Edits the localization for the specified language code. All
+             parameters are changed, which means that no defaults apply.
+  
+  remove     Removes the localization for the specified language code.
+  purge      Removes the localization for the specified language code,
+             including all the localized content.
+             
+  show       Shows all available localizations.
+
+EOF
+}
+
+function show_localization()
+{
+    lang=`echo "$1" | cut -d : -f 1`
+    group=`echo "$1" | cut -d : -f 2`
+    name=`echo "$1" | cut -d : -f 3`
+    l10n=`echo "$1" | cut -d : -f 4`
+
+    if [ -z "$l10n" ]; then
+        l10n='default'
+    fi
+
+    echo "Language code: $lang"
+    echo " localization: $l10n"
+    echo "        group: $group"
+    echo "         name: $name"
+    echo ""
+}
+
+function has_localization()
+{
+    if [ -f "$conf" ]; then
+        for line in `cat $conf`; do
+            lang=`echo "$line" | cut -d : -f 1`
+            if [ "$lang" == "$1" ]; then
+                return 1
+            fi
+        done
+    fi
+
+    return 0
+}
+
+function echo_localization()
+{
+    echo -n "$1:$2:$3"
+    if [ -n "$4" ]; then
+        echo -n ":$4"
+    fi
+    echo
+}
+
+#for option do
+#    if [ -z "$command" ]; then
+#        case "$option" in
+#            --* | -*)
+#                echo "$(basename $0): Invalid command \`$option'."
+#                exit 1
+#                ;;
+            
+#            *)
+#                command="$option"
+#                continue
+#                ;;
+#        esac
+#    fi
+
+#    if [ -n "$prev_arg" ]; then
+#        case "$option" in
+#            --* | -*)
+#                echo "$(basename $0): Invalid argument \`$option' to option \`$prev_arg'."
+#                exit 1
+#                ;;
+#        esac
+#        eval "$prev_arg=\$option"
+#        prev_arg=
+#    fi
+
+#    case "$option" in
+#        --language | --lang | -l)
+#        prev_arg=lang
+#        ;;
+
+#        --group | --grp | -g)
+#        prev_arg=group
+#        ;;
+
+#        --name | -n)
+#        prev_arg=name
+#        ;;
+
+#        --localization | --l10n)
+#        prev_arg=l10n
+#        ;;
+#    esac
+#done
+
+command="$1"
+language="$2"
+group="$3"
+name="$4"
+l10n="$5"
+
+case "$command" in
+    add)
+        # language code and Wiki group are mandatory
+        if [ -z "$language" ] || [ -z "$group" ]; then
+            usage > /dev/stderr
+            exit 1
+        fi
+
+        # if no name is specified, default to Wiki group name
+        if [ -z "$name" ]; then
+            name=$group
+        fi
+
+        # check if there already is a localization for the specified
+        # language
+        has_localization $language
+        if [ $? == 0 ]; then
+            echo_localization $language $group $name $l10n >> "$conf"
+        else
+            echo "Localization for '$language' already exists."
+        fi
+        ;;
+
+    edit)
+        if [ -z "$language" ]; then
+            echo "Please specify which localization to edit."
+            exit 1
+        fi
+        
+        tmp=`mktemp -p "$confdir"`
+        cp "$conf" "$tmp"
+        echo -n > "$conf"
+        
+        for line in `cat "$tmp"`; do
+            lang=`echo "$line" | cut -d : -f 1`
+
+            if [ "$lang" == "$language" ]; then
+                echo_localization $language $group $name $l10n >> "$conf"
+            else
+                g=`echo "$line" | cut -d : -f 2`
+                n=`echo "$line" | cut -d : -f 3`
+                l=`echo "$line" | cut -d : -f 4`
+
+                echo_localization $lang $g $n $l >> "$conf"
+            fi
+        done
+
+        rm -f "$tmp"
+        ;;
+
+    remove)
+        if [ -z "$language" ]; then
+            echo "Please specify which localization to remove."
+            exit 1
+        fi
+        
+        tmp=`mktemp -p "$confdir"`
+        cp "$conf" "$tmp"
+        echo -n > "$conf"
+        
+        for line in `cat "$tmp"`; do
+            lang=`echo "$line" | cut -d : -f 1`
+
+            if [ "$lang" != "$language" ]; then
+                g=`echo "$line" | cut -d : -f 2`
+                n=`echo "$line" | cut -d : -f 3`
+                l=`echo "$line" | cut -d : -f 4`
+
+                echo_localization $lang $g $n $l >> "$conf"
+            fi
+        done
+
+        rm -f "$tmp"
+        ;;
+
+    purge)
+        ;;
+
+    show)
+        if [ -f "$conf" ]; then
+            for line in `cat "$conf" 2> /dev/null`; do
+                show_localization "$line"
+            done
+        fi
+        ;;
+
+    help)
+        usage
+        ;;
+
+    *)
+        usage > /dev/stderr
+        ;;
+esac
+
+exit 0
diff -Nur wiki.orig/scripts/languages.php wiki/scripts/languages.php
--- wiki.orig/scripts/languages.php	1970-01-01 01:00:00.000000000 +0100
+++ wiki/scripts/languages.php	2005-01-16 21:58:16.000000000 +0100
@@ -0,0 +1,133 @@
+<?php
+
+/**
+ * Copyright (C) 2005 Thierry Reding (thierry@doppeltgemoppelt.de)
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
+ * This script facilitates the creation of different localizations for the
+ * PmWiki. It was originally written for the PmWiki on
+ * women.alioth.debian.org/wiki but can probably be adapted for other
+ * PmWikis aswell. Note that localization in this context does not mean the
+ * same as PmWiki localization, but the localization of the Wiki's content.
+ *
+ * One of the bigger features of the script is that it loads language data
+ * from an external file, thus making it easier to add/remove/edit
+ * localizations.
+ *
+ * This languages file is plain text, and has data for one language per
+ * line. Each field is separated by a colon (:). The first field is the
+ * language code (en, de, fr, pt-br, ...), the second field the PmWiki group
+ * used for the content localization, the third field the display name of
+ * the language and the fourth field contains the name of the PmWiki XLPage
+ * file for the corresponding language.
+ */
+
+// utility function that returns true if the 'string' starts with 'search'
+function startsWith($string, $search)
+{
+    return (strpos($string, $search) === 0);
+}
+
+// Loads languages from a file.
+function LoadLanguages($filename)
+{
+    $fp = fopen($filename, "r");
+    while ($fp && !feof($fp))
+    {
+        $line = fgets($fp);
+        if ($line)
+        {
+            list($lang, $group, $name, $xlpage) = explode(":", trim($line));
+            $languages[$lang] = array(
+                'group' => $group, 'name' => $name, 'xlpage' => $xlpage
+            );
+        }
+    }
+
+    fclose($fp);
+    return $languages;
+}
+
+function ChooseLanguage()
+{
+    global $languages, $ScriptUrl;
+    $prefix = "";
+    
+    foreach ($languages as $lang => $params)
+    {
+        $grp = $params['group'];
+        $name = $params['name'];
+
+        echo $prefix;
+        echo "<a class=\"wikilink\" href=\"$ScriptUrl/$grp/HomePage\">$name</a>";
+        $prefix = " - ";
+    }
+}
+
+$languages = LoadLanguages("local/languages.conf");
+
+// get a list of languages as requested by the browser
+if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
+    $browser_languages = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
+else
+    // default to english if the browser doesn't request any other language
+    $browser_languages[] = 'en';
+
+// strip the language priority from all languages requested by the browser;
+// this script only matches the first language possible
+if (isset($browser_languages) && is_array($browser_languages))
+{
+    foreach ($browser_languages as $key => $value)
+    {
+        $lang = &$browser_languages[$key];
+
+        $index = strpos($lang, ";");
+        if ($index > 0)
+            $lang = substr($lang, 0, $index);
+    }
+}
+
+// try to find a match between languages that the browser accepts and
+// languages that the Wiki has been translated into
+$found = false;
+foreach ($browser_languages as $blang)
+{
+    foreach ($languages as $lang => $params)
+    {
+        if (startsWith($blang, $lang))
+        {
+            // load PmWiki localization
+            if (isset($params['xlpage']) && !is_null($params['xlpage']))
+                XLPage($lang, $params['xlpage']);
+            
+            $group = $params['group'];
+            $found = true;
+            break;
+        }
+    }
+
+    if ($found)
+        break;
+}
+
+// redirect to the localized Wiki (only do so if the URL doesn't actually
+// point to a specific localization yet)
+$url  = "http://"; . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
+if (startsWith($ScriptUrl, $url))
+    header("Location: $ScriptUrl/$group/HomePage");
+
+?>

Reply to: