changeset 693:095d1972dad9

Added ability to switch languages without context change.
author Vladimir Homutov <vl@nginx.com>
date Fri, 28 Sep 2012 11:14:47 +0000
parents 31ba6a104186
children 2ced25001893
files GNUmakefile xml/menu.xml xsls/article.xsls xsls/menu.xsls xsls/style.xsls
diffstat 5 files changed, 102 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -20,11 +20,32 @@ define	XSLScript
 	if [ ! -s $(2) ]; then rm $(2); fi; test -s $(2)
 endef
 
+# List of available languages
+LANGS:=$(find xml/ -mindepth 1 -maxdepth 1 -type d -not -name '.svn' \
+		-exec basename {} \; | xargs)
+
+# process xslt and create/pass some variables into XSLT processor:
+#
+# ORIGIN	 xml source of processed document ($o in shell script)
+# NOLANGORIGIN	 html document without xml/lang prefix ($ox in shell script)
+# TRANS		 list of languages to which document is translated ($trans)
+#
+# list of all languages is used to scan thorough to determine if current
+# document has translation in each of them.
+#
 define	XSLT
 	xmllint --noout --valid $2
 	xsltproc -o $3							\
 		$(shell f=`echo $2 | sed 's,^xml/,,;s,[^/]*/,en/,'`;	\
-		[ -f xml/$$f ] && echo --stringparam ORIGIN "$$f")	\
+			ox=`echo $2 | sed 's,^xml/,,;s,[^/]*/,,'`;	\
+			o=`echo $$ox | sed 's,\.xml,\.html,'`;		\
+			trans=`for LANG in $(LANGS);			\
+			do						\
+			[ -f xml/$$LANG/$$ox ] && echo -ne "$$LANG ";	\
+			done`;						\
+		[ -f xml/$$f ] && echo --stringparam ORIGIN "$$f"; 	\
+		echo --stringparam NOLANGORIGIN "$$o";			\
+		echo --stringparam TRANS \""$$trans\"")			\
 		$(shell p="$4"; [ -n "$$p" ] &&				\
 		echo --stringparam $${p%%=*} $${p#*=})			\
 		$1 $2
--- a/xml/menu.xml
+++ b/xml/menu.xml
@@ -9,14 +9,14 @@
 
 <menu lang="cn">
 
-<item href="/en/"> english </item>
-<item href="/ru/"> русский </item>
+<item href="/en/" switchlang="en"> english </item>
+<item href="/ru/" switchlang="ru"> русский </item>
 <item />
 
 <item> 简体中文 </item>
-<item href="/he/"> עברית </item>
-<item href="/ja/"> 日本語 </item>
-<item href="/tr/"> türkçe </item>
+<item href="/he/" switchlang="he"> עברית </item>
+<item href="/ja/" switchlang="ja"> 日本語 </item>
+<item href="/tr/" switchlang="tr"> türkçe </item>
 <item />
 
 <item href="/" lang="en"> 新闻 </item>
@@ -42,13 +42,13 @@
 <menu lang="en">
 
 <item> english </item>
-<item href="/ru/"> русский </item>
+<item href="/ru/" switchlang="ru"> русский </item>
 <item />
 
-<item href="/cn/"> 简体中文 </item>
-<item href="/he/"> עברית </item>
-<item href="/ja/"> 日本語 </item>
-<item href="/tr/"> türkçe </item>
+<item href="/cn/" switchlang="cn"> 简体中文 </item>
+<item href="/he/" switchlang="he"> עברית </item>
+<item href="/ja/" switchlang="ja"> 日本語 </item>
+<item href="/tr/" switchlang="tr"> türkçe </item>
 <item />
 
 <item href="/"> news </item>
@@ -85,14 +85,14 @@
 
 <menu lang="he">
 
-<item href="/en/"> english </item>
-<item href="/ru/"> русский </item>
+<item href="/en/" switchlang="en"> english </item>
+<item href="/ru/" switchlang="ru"> русский </item>
 <item />
 
-<item href="/cn/"> 简体中文 </item>
+<item href="/cn/" switchlang="cn"> 简体中文 </item>
 <item> עברית </item>
-<item href="/ja/"> 日本語 </item>
-<item href="/tr/"> türkçe </item>
+<item href="/ja/" switchlang="ja"> 日本語 </item>
+<item href="/tr/" switchlang="tr"> türkçe </item>
 <item />
 
 <item href="/" lang="אנגלית"> חדשות </item>
@@ -115,14 +115,14 @@
 
 <menu lang="ja">
 
-<item href="/en/"> english </item>
-<item href="/ru/"> русский </item>
+<item href="/en/" switchlang="en"> english </item>
+<item href="/ru/" switchlang="ru"> русский </item>
 <item />
 
-<item href="/cn/"> 简体中文 </item>
-<item href="/he/"> עברית </item>
+<item href="/cn/" switchlang="cn"> 简体中文 </item>
+<item href="/he/" switchlang="he"> עברית </item>
 <item> 日本語 </item>
-<item href="/tr/"> türkçe </item>
+<item href="/tr/" switchlang="tr"> türkçe </item>
 <item />
 
 <item href="/" lang="en"> ニュース </item>
@@ -146,14 +146,14 @@
 
 <menu lang="ru">
 
-<item href="/en/"> english </item>
+<item href="/en/" switchlang="en"> english </item>
 <item> русский </item>
 <item />
 
-<item href="/cn/"> 简体中文 </item>
-<item href="/he/"> עברית </item>
-<item href="/ja/"> 日本語 </item>
-<item href="/tr/"> türkçe </item>
+<item href="/cn/" switchlang="cn"> 简体中文 </item>
+<item href="/he/" switchlang="he"> עברית </item>
+<item href="/ja/" switchlang="ja"> 日本語 </item>
+<item href="/tr/" switchlang="tr"> türkçe </item>
 <item />
 
 <item href="/" lang="en"> новости </item>
@@ -179,13 +179,13 @@
 
 <menu lang="tr">
 
-<item href="/en/"> english </item>
-<item href="/ru/"> русский </item>
+<item href="/en/" switchlang="en"> english </item>
+<item href="/ru/" switchlang="ru"> русский </item>
 <item />
 
-<item href="/cn/"> 简体中文 </item>
-<item href="/he/"> עברית </item>
-<item href="/ja/"> 日本語 </item>
+<item href="/cn/" switchlang="cn"> 简体中文 </item>
+<item href="/he/" switchlang="he"> עברית </item>
+<item href="/ja/" switchlang="ja"> 日本語 </item>
 <item> türkçe </item>
 <item />
 
--- a/xsls/article.xsls
+++ b/xsls/article.xsls
@@ -13,9 +13,11 @@ X:strip-space elements = "article sectio
   -- a current directory of a XSLT script is where the script is stored,
   -- but not where XSLT processor has been started to run the script
   -->
-X:param XML = "'../xml'"; 
+X:param XML = "'../xml'";
 X:param YEAR;
 X:param ORIGIN;
+X:param NOLANGORIGIN;
+X:param TRANS;
 
 X:var LINK = "/article/@link | /module/@link";
 X:var LANG = "/article/@lang | /module/@lang";
--- a/xsls/menu.xsls
+++ b/xsls/menu.xsls
@@ -21,25 +21,56 @@ X:template = "menu/item" {
         }
 
     } else {
-        <a>
-        X:attribute "href" {
+
+        <!--
+          -- If a menu item has the switchlang attribute, then it will point
+          -- to the same document in the specified language.
+          -- The document will be taken from $NOLANGORIGIN variable.
+          -->
+        X:if "@switchlang != ''" {
 
-            X:if "starts-with(@href, $DIRNAME)" {
-               X:if "substring-after(@href, $DIRNAME) = ''" {
-                  ./
-
-               } else {
-                  !{ substring-after(@href, $DIRNAME) }
-               }
+            <!--
+              -- Check if list of available translations ($TRANS) contains
+              -- the language we are going to generate link to.
+              -- If yes - generate link, otherwise just name the language.
+              -->
+            X:if "contains($TRANS, @switchlang)" {
+                <a>
+                 X:attribute "href" {
+                  X:if "$ROOT != '' " {
+                     !{ concat($ROOT, '/', @switchlang, '/', $NOLANGORIGIN) }
+                  } else {
+                     !{ concat(@switchlang, '/', $NOLANGORIGIN) }
+                  }
+                 }
+                 !{ normalize-space(text()) }
+                </a>
 
             } else {
-               !{ concat($ROOT, @href) }
+               <a class="notrans"> !{ normalize-space(text()) } </a>
             }
+        } else {
+
+            <a>
+            X:attribute "href" {
+
+                X:if "starts-with(@href, $DIRNAME)" {
+                    X:if "substring-after(@href, $DIRNAME) = ''" {
+                        ./
+                    } else {
+                        !{ substring-after(@href, $DIRNAME) }
+                    }
+
+                } else {
+
+                    !{ concat($ROOT, @href) }
+                }
+            }
+            !{ normalize-space(text()) }
+            </a>
+
+            X:if "@lang" { X:text { [} !{@lang} X:text {]}}
         }
-        !{ normalize-space(text()) }
-        </a>
-
-        X:if "@lang" { X:text { [} !{@lang} X:text {]}}
 
         <br/>
     }
--- a/xsls/style.xsls
+++ b/xsls/style.xsls
@@ -24,6 +24,8 @@ X:template style (lang) {
                             font-size:      80%; }
         div#banner        { background:     #EEEEEE;
                             padding:        10pt 10pt 10pt 0; }
+        a.notrans         { color:          gray;
+                            text-decoration:none; }
         span.initial      { font-size:      200%;
                             float:          left;
                             padding-right:  10pt;}