Localizing XSLT templates

Hello everybody,

I’ve been quite busy this period and didn’t have a chance to post more useful notes on my blog, but finally I found some time for this. Today we’re gonna talk about how to localize your XSLT templates (please read about how to localize normal HTML templates here).

Well, the technique is quite simple though. All you need to know is that you can’t use the Translate tag inside your XSLT templates. Instead of using this tag you need to define a special template like the following:

<xsl:template name="translate">
    <xsl:param name="translationkey" />
    <xsl:param name="defaulttranslation" />
    <xsl:choose>
        <xsl:when test="$translationfile/translations/
            key[@name=$translationkey]">

            <xsl:value-of select="$translationfile/
                key[@name=$translationkey]/
                translation[@culture=$currentlanguage]" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$defaulttranslation" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

As you can see the template has two parameters: translation key (the first argument of the Translate tag) and default translation (the second argument of the Translate tag).
There are also two variables that are used inside the template: $translationfile and $currentlanguage. They are defined as following (put the definition at the top of your XSLT template):

<xsl:variable name="translationfile" select="document('http://localhost/
    Files/Templates/[ModuleSystemName]/Translations.xml')/translations" />
<xsl:variable name="currentlanguage" select="/Template/GlobalTags/
    Global.Area.LongLang" />

Here is a trick. In order to use translations we need to load corresponding XML-document and assign its contents to the value of some variable. There are two drawbacks of this methods that you should know about:
  1. You always have to include the host-name component inside the “document” function (you can’t write something like “document(‘/Files/…’) assuming that the absolute path will be resolved).
  2. For different page/paragraph/module templates you need to provide the correct path to the translation file (e.g. /Files/Templates/Weblog/Translations.xml and /Files/Templates/ForumV2/Translations.xml for Weblog and ForumV2 templates respectively).

And finally, here’s an example of using the template:

<xsl:call-template name="translate">
    <xsl:with-param name="translationkey" select="'ExtranetLoginText'" />
    <xsl:with-param name="defaulttranslation" 
        select="'You're logged in as:'" />
</xsl:call-template>

Please note that the template call must look exactly like shown above. Otherwise the parser will be unable to recognize this template and you won’t be able to use the translation page on the backend (it simply won’t find any keys in your template).

Cheers! 😉

5 Comments

  1. Posted February 28, 2013 at 1:29 am | Permalink | Reply

    I just like the valuable info you provide for your articles.
    I will bookmark your weblog and take a look at once more here frequently.

    I’m moderately certain I’ll be informed many new stuff proper right here!
    Best of luck for the next!

  2. Posted May 10, 2013 at 1:17 am | Permalink | Reply

    With havin so much content and articles do you ever
    run into any issues of plagorism or copyright violation?
    My site has a lot of unique content I’ve either authored myself or outsourced but it looks like a lot of it is popping it up all over the web without my permission. Do you know any solutions to help stop content from being stolen? I’d definitely
    appreciate it.

  3. Posted May 14, 2013 at 1:24 pm | Permalink | Reply

    I was curious if you ever considered changing the structure of your blog?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

  4. Posted March 5, 2014 at 6:08 am | Permalink | Reply

    I have been exploring for a little for any high-quality articles
    or blog posts in this sort of space . Exploring in Yahoo I at last stumbled upon this web
    site. Studying this info So i am satisfied to exhibit
    that I have an incredibly just right uncanny feeling I came upon just what
    I needed. I such a lot surely will make sure to do not omit this web site and give it a look regularly.

  5. Posted September 30, 2014 at 4:19 pm | Permalink | Reply

    Very great post. I just stumbled upon your weblog and wanted to mention that I have truly enjoyed surfing around your blog posts.
    After all I’ll be subscribing in your feed and I hope you write once more
    soon!

One Trackback

  1. […] This post was mentioned on Twitter by Nicky Christensen, netsi1964. netsi1964 said: #Dynamicweb Localizing XSLT templates: http://t.co/WWwwIve […]

Post a Comment

Required fields are marked *

*
*