]> granicus.if.org Git - docbook-dsssl/commitdiff
Added xsl:keys for improved performance of localization texts look up. Performance...
authorJirka Kosek <jirka@kosek.cz>
Tue, 13 Jul 2010 18:10:49 +0000 (18:10 +0000)
committerJirka Kosek <jirka@kosek.cz>
Tue, 13 Jul 2010 18:10:49 +0000 (18:10 +0000)
xsl/common/l10n.xsl

index 7fc142936ed70cced4fc711aa6339979221d0bcd..1ade6a1e5b1e8b256c031ecd6d84b1739b6eabfb 100644 (file)
 <xsl:param name="l10n.xml" select="document('../common/l10n.xml')"/>
 <xsl:param name="local.l10n.xml" select="document('')"/>
 
+<xsl:key name="l10n-lang" match="l:l10n" use="@language"/>
+<xsl:key name="l10n-gentext" match="l:l10n/l:gentext" use="concat(../@language, '#', @key)"/>
+<xsl:key name="l10n-dingbat" match="l:l10n/l:dingbat" use="concat(../@language, '#', @key)"/>
+<xsl:key name="l10n-context" match="l:l10n/l:context" use="concat(../@language, '#', @name)"/>
+
 <xsl:template name="l10n.language">
   <xsl:param name="target" select="."/>
   <xsl:param name="xref-context" select="false()"/>
     </xsl:choose>
   </xsl:variable>
 
-  <xsl:choose>
-    <xsl:when test="$l10n.xml/l:i18n/l:l10n[@language=$adjusted.language]">
-      <xsl:value-of select="$adjusted.language"/>
-    </xsl:when>
-    <!-- try just the lang code without country -->
-    <xsl:when test="$l10n.xml/l:i18n/l:l10n[@language=substring-before($adjusted.language,'_')]">
-      <xsl:value-of select="substring-before($adjusted.language,'_')"/>
-    </xsl:when>
-    <!-- or use the default -->
-    <xsl:otherwise>
-      <xsl:message>
-        <xsl:text>No localization exists for "</xsl:text>
+  <xsl:for-each select="$l10n.xml">   <!-- We need to change context in order to get key work -->
+    <xsl:choose>
+      <xsl:when test="key('l10n-lang', $adjusted.language)">
         <xsl:value-of select="$adjusted.language"/>
-        <xsl:text>" or "</xsl:text>
+      </xsl:when>
+      <!-- try just the lang code without country -->
+      <xsl:when test="key('l10n-lang', substring-before($adjusted.language,'_'))">
         <xsl:value-of select="substring-before($adjusted.language,'_')"/>
-        <xsl:text>". Using default "</xsl:text>
+      </xsl:when>
+      <!-- or use the default -->
+      <xsl:otherwise>
+        <xsl:message>
+          <xsl:text>No localization exists for "</xsl:text>
+          <xsl:value-of select="$adjusted.language"/>
+          <xsl:text>" or "</xsl:text>
+          <xsl:value-of select="substring-before($adjusted.language,'_')"/>
+          <xsl:text>". Using default "</xsl:text>
+          <xsl:value-of select="$l10n.gentext.default.language"/>
+          <xsl:text>".</xsl:text>
+        </xsl:message>
         <xsl:value-of select="$l10n.gentext.default.language"/>
-        <xsl:text>".</xsl:text>
-      </xsl:message>
-      <xsl:value-of select="$l10n.gentext.default.language"/>
-    </xsl:otherwise>
-  </xsl:choose>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:for-each>
 </xsl:template>
 
 <xsl:template name="l10.language.name">
     <xsl:call-template name="l10n.language"/>
   </xsl:param>
 
-  <xsl:variable name="local.l10n.gentext"
-                select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:gentext[@key=$key])[1]"/>
-
-  <xsl:variable name="l10n.gentext"
-                select="($l10n.xml/l:i18n/l:l10n[@language=$lang]/l:gentext[@key=$key])[1]"/>
+  <xsl:for-each select="$l10n.xml">  <!-- We need to switch context in order to make key() work -->
+    <xsl:variable name="local.l10n.gentext"
+                  select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:gentext[@key=$key])[1]"/>
 
-  <xsl:choose>
-    <xsl:when test="$local.l10n.gentext">
-      <xsl:value-of select="$local.l10n.gentext/@text"/>
-    </xsl:when>
-    <xsl:when test="$l10n.gentext">
-      <xsl:value-of select="$l10n.gentext/@text"/>
-    </xsl:when>
-    <xsl:otherwise>
-      <xsl:message>
-        <xsl:text>No "</xsl:text>
-        <xsl:value-of select="$lang"/>
-        <xsl:text>" localization of "</xsl:text>
-        <xsl:value-of select="$key"/>
-        <xsl:text>" exists</xsl:text>
-        <xsl:choose>
-          <xsl:when test="$lang = 'en'">
-             <xsl:text>.</xsl:text>
-          </xsl:when>
-          <xsl:otherwise>
-             <xsl:text>; using "en".</xsl:text>
-          </xsl:otherwise>
-        </xsl:choose>
-      </xsl:message>
+    <xsl:variable name="l10n.gentext"
+                  select="key('l10n-gentext', concat($lang, '#', $key))[1]"/>
 
-      <xsl:value-of select="($l10n.xml/l:i18n/l:l10n[@language='en']/l:gentext[@key=$key])[1]/@text"/>
-    </xsl:otherwise>
-  </xsl:choose>
+    <xsl:choose>
+      <xsl:when test="$local.l10n.gentext">
+        <xsl:value-of select="$local.l10n.gentext/@text"/>
+      </xsl:when>
+      <xsl:when test="$l10n.gentext">
+        <xsl:value-of select="$l10n.gentext/@text"/>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:message>
+          <xsl:text>No "</xsl:text>
+          <xsl:value-of select="$lang"/>
+          <xsl:text>" localization of "</xsl:text>
+          <xsl:value-of select="$key"/>
+          <xsl:text>" exists</xsl:text>
+          <xsl:choose>
+            <xsl:when test="$lang = 'en'">
+               <xsl:text>.</xsl:text>
+            </xsl:when>
+            <xsl:otherwise>
+               <xsl:text>; using "en".</xsl:text>
+            </xsl:otherwise>
+          </xsl:choose>
+        </xsl:message>
+
+        <xsl:value-of select="key('l10n-gentext', concat('en', '#', $key))[1]/@text"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:for-each>
 </xsl:template>
 
 <xsl:template name="gentext.element.name">
     <xsl:call-template name="l10n.language"/>
   </xsl:param>
 
-  <xsl:variable name="local.l10n.dingbat"
-                select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:dingbat[@key=$dingbat])[1]"/>
+  <xsl:for-each select="$l10n.xml">  <!-- We need to switch context in order to make key() work -->
+    <xsl:variable name="local.l10n.dingbat"
+                  select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang]/l:dingbat[@key=$dingbat])[1]"/>
 
-  <xsl:variable name="l10n.dingbat"
-                select="($l10n.xml/l:i18n/l:l10n[@language=$lang]/l:dingbat[@key=$dingbat])[1]"/>
+    <xsl:variable name="l10n.dingbat"
+                  select="key('l10n-dingbat', concat($lang, '#', $dingbat))[1]"/>
 
-  <xsl:choose>
-    <xsl:when test="$local.l10n.dingbat">
-      <xsl:value-of select="$local.l10n.dingbat/@text"/>
-    </xsl:when>
-    <xsl:when test="$l10n.dingbat">
-      <xsl:value-of select="$l10n.dingbat/@text"/>
-    </xsl:when>
-    <xsl:otherwise>
-      <xsl:message>
-        <xsl:text>No "</xsl:text>
-        <xsl:value-of select="$lang"/>
-        <xsl:text>" localization of dingbat </xsl:text>
-        <xsl:value-of select="$dingbat"/>
-        <xsl:text> exists; using "en".</xsl:text>
-      </xsl:message>
-
-      <xsl:value-of select="($l10n.xml/l:i18n/l:l10n[@language='en']/l:dingbat[@key=$dingbat])[1]/@text"/>
-    </xsl:otherwise>
-  </xsl:choose>
+    <xsl:choose>
+      <xsl:when test="$local.l10n.dingbat">
+        <xsl:value-of select="$local.l10n.dingbat/@text"/>
+      </xsl:when>
+      <xsl:when test="$l10n.dingbat">
+        <xsl:value-of select="$l10n.dingbat/@text"/>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:message>
+          <xsl:text>No "</xsl:text>
+          <xsl:value-of select="$lang"/>
+          <xsl:text>" localization of dingbat </xsl:text>
+          <xsl:value-of select="$dingbat"/>
+          <xsl:text> exists; using "en".</xsl:text>
+        </xsl:message>
+
+        <xsl:value-of select="key('l10n-dingbat', concat('en', '#', $dingbat))[1]/@text"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:for-each>
 </xsl:template>
 
 <xsl:template name="gentext.startquote">
   </xsl:param>
   <xsl:param name="verbose" select="1"/>
 
-  <xsl:variable name="local.localization.node"
-                select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang])[1]"/>
+  <xsl:for-each select="$l10n.xml">  <!-- We need to switch context in order to make key() work -->
 
-  <xsl:variable name="localization.node"
-                select="($l10n.xml/l:i18n/l:l10n[@language=$lang])[1]"/>
+    <xsl:variable name="local.localization.node"
+                  select="($local.l10n.xml//l:i18n/l:l10n[@language=$lang])[1]"/>
 
-  <xsl:if test="count($localization.node) = 0
-                and count($local.localization.node) = 0
-                and $verbose != 0">
-    <xsl:message>
-      <xsl:text>No "</xsl:text>
-      <xsl:value-of select="$lang"/>
-      <xsl:text>" localization exists.</xsl:text>
-    </xsl:message>
-  </xsl:if>
+    <xsl:variable name="localization.node"
+                  select="key('l10n-lang', $lang)[1]"/>
 
-  <xsl:variable name="local.context.node"
-                select="$local.localization.node/l:context[@name=$context]"/>
-
-  <xsl:variable name="context.node"
-                select="$localization.node/l:context[@name=$context]"/>
-
-  <xsl:if test="count($context.node) = 0
-                and count($local.context.node) = 0
-                and $verbose != 0">
-    <xsl:message>
-      <xsl:text>No context named "</xsl:text>
-      <xsl:value-of select="$context"/>
-      <xsl:text>" exists in the "</xsl:text>
-      <xsl:value-of select="$lang"/>
-      <xsl:text>" localization.</xsl:text>
-    </xsl:message>
-  </xsl:if>
+    <xsl:if test="count($localization.node) = 0
+                  and count($local.localization.node) = 0
+                  and $verbose != 0">
+      <xsl:message>
+        <xsl:text>No "</xsl:text>
+        <xsl:value-of select="$lang"/>
+        <xsl:text>" localization exists.</xsl:text>
+      </xsl:message>
+    </xsl:if>
 
-  <xsl:variable name="local.template.node"
-                select="($local.context.node/l:template[@name=$name
-                                                        and @style
-                                                        and @style=$xrefstyle]
-                        |$local.context.node/l:template[@name=$name
-                                                        and not(@style)])[1]"/>
+    <xsl:variable name="local.context.node"
+                  select="$local.localization.node/l:context[@name=$context]"/>
 
-  <xsl:variable name="template.node"
-                select="($context.node/l:template[@name=$name
-                                                  and @style
-                                                  and @style=$xrefstyle]
-                        |$context.node/l:template[@name=$name
-                                                  and not(@style)])[1]"/>
+    <xsl:variable name="context.node"
+                  select="key('l10n-context', concat($lang, '#', $context))[1]"/>
 
-  <xsl:choose>
-    <xsl:when test="$local.template.node/@text">
-      <xsl:value-of select="$local.template.node/@text"/>
-    </xsl:when>
-    <xsl:when test="$template.node/@text">
-      <xsl:value-of select="$template.node/@text"/>
-    </xsl:when>
-    <xsl:otherwise>
-      <xsl:choose>
-        <xsl:when test="contains($name, '/')">
-          <xsl:call-template name="gentext.template">
-            <xsl:with-param name="context" select="$context"/>
-            <xsl:with-param name="name" select="substring-after($name, '/')"/>
-            <xsl:with-param name="origname" select="$origname"/>
-            <xsl:with-param name="purpose" select="$purpose"/>
-            <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
-            <xsl:with-param name="referrer" select="$referrer"/>
-            <xsl:with-param name="lang" select="$lang"/>
-            <xsl:with-param name="verbose" select="$verbose"/>
-          </xsl:call-template>
-        </xsl:when>
-        <xsl:when test="$verbose = 0">
-          <!-- silence -->
-        </xsl:when>
-        <xsl:otherwise>
-          <xsl:message>
-            <xsl:text>No template for "</xsl:text>
-            <xsl:value-of select="$origname"/>
-            <xsl:text>" (or any of its leaves) exists
-in the context named "</xsl:text>
-            <xsl:value-of select="$context"/>
-            <xsl:text>" in the "</xsl:text>
-            <xsl:value-of select="$lang"/>
-            <xsl:text>" localization.</xsl:text>
-          </xsl:message>
-        </xsl:otherwise>
-      </xsl:choose>
-    </xsl:otherwise>
-  </xsl:choose>
+    <xsl:if test="count($context.node) = 0
+                  and count($local.context.node) = 0
+                  and $verbose != 0">
+      <xsl:message>
+        <xsl:text>No context named "</xsl:text>
+        <xsl:value-of select="$context"/>
+        <xsl:text>" exists in the "</xsl:text>
+        <xsl:value-of select="$lang"/>
+        <xsl:text>" localization.</xsl:text>
+      </xsl:message>
+    </xsl:if>
+
+    <xsl:variable name="local.template.node"
+                  select="($local.context.node/l:template[@name=$name
+                                                          and @style
+                                                          and @style=$xrefstyle]
+                          |$local.context.node/l:template[@name=$name
+                                                          and not(@style)])[1]"/>
+
+    <xsl:variable name="template.node"
+                  select="($context.node/l:template[@name=$name
+                                                    and @style
+                                                    and @style=$xrefstyle]
+                          |$context.node/l:template[@name=$name
+                                                    and not(@style)])[1]"/>
+
+    <xsl:choose>
+      <xsl:when test="$local.template.node/@text">
+        <xsl:value-of select="$local.template.node/@text"/>
+      </xsl:when>
+      <xsl:when test="$template.node/@text">
+        <xsl:value-of select="$template.node/@text"/>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:choose>
+          <xsl:when test="contains($name, '/')">
+            <xsl:call-template name="gentext.template">
+              <xsl:with-param name="context" select="$context"/>
+              <xsl:with-param name="name" select="substring-after($name, '/')"/>
+              <xsl:with-param name="origname" select="$origname"/>
+              <xsl:with-param name="purpose" select="$purpose"/>
+              <xsl:with-param name="xrefstyle" select="$xrefstyle"/>
+              <xsl:with-param name="referrer" select="$referrer"/>
+              <xsl:with-param name="lang" select="$lang"/>
+              <xsl:with-param name="verbose" select="$verbose"/>
+            </xsl:call-template>
+          </xsl:when>
+          <xsl:when test="$verbose = 0">
+            <!-- silence -->
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:message>
+              <xsl:text>No template for "</xsl:text>
+              <xsl:value-of select="$origname"/>
+              <xsl:text>" (or any of its leaves) exists
+  in the context named "</xsl:text>
+              <xsl:value-of select="$context"/>
+              <xsl:text>" in the "</xsl:text>
+              <xsl:value-of select="$lang"/>
+              <xsl:text>" localization.</xsl:text>
+            </xsl:message>
+          </xsl:otherwise>
+        </xsl:choose>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:for-each>
 </xsl:template>
 
 <!-- silently test if a gentext template exists -->