ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Internationalization using language files
Submitter: Paul Prescod (other recipes)
Last Updated: 2001/06/27
Version no: 1.0
Category: Parameters

 

5 stars 2 vote(s)


Approved

Description:


XSLT stylesheets often contain "boiler-plate text" like "Footnote",
"Total" etc. This text is typically specific to a particular language.
This recipe shows how to localize boilerplate text to a particular
language.

Source: Text Source


English.xml:

<phrases>
<phrase name="hello">Hello There</phrase>
<phrase name="goodbye">Goodbye</phrase>
<phrase name="footnote">Footnote</phrase>
<phrase name="total">Total</phrase>
</phrases>

French.xml:

<phrases>
<phrase name="hello">Bonjour</phrase>
<phrase name="goodbye">Au Revoir</phrase>
<phrase name="footnote">Note de Pied</phrase>
<phrase name="total">Totale</phrase>
</phrases>

locale.xsl:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="language-file">english.xml</xsl:param>

<xsl:template match="/">
    <translated-phrases>
    <xsl:value-of select="$language-file"/>
        <translation>
            <xsl:call-template name="translate">
                <xsl:with-param name="word">hello</xsl:with-param>
            </xsl:call-template>
        </translation>
        <translation>
            <xsl:call-template name="translate">
                <xsl:with-param name="word">goodbye</xsl:with-param>
            </xsl:call-template>
        </translation>
    </translated-phrases>
</xsl:template>

<xsl:key name="translations"
    match="phrases/phrase"
    use="@name"/>
<xsl:template name="translate">
    <xsl:param name="word"/>
    <xsl:for-each select="document($language-file)">
        <xsl:value-of select="key('translations', $word)"/>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

The license for this recipe is available here.

Discussion:

Chris Bayes suggests another technique which uses a single language file:
http://www.biglist.com/lists/xsl-list/archives/200008/msg01300.html

I slightly prefer to have multiple files because it means that independent people (typically one per language) can maintain them easily.



Add comment

Number of comments: 3

An alternate recipe, Tim Watts, 2001/08/23

I have done language replacment also, but rather than
the XML given in the example of :

  Hello There
  Goodbye Ya'll


I use XML simular to:

  Hello There
  Goodbye Ya'll


By doing this I can set a global variable of the XML file *once*


and then call it like so:


That makes for a lot less code, as can be seen below.






    
    ~langname~
        
            
        
        
            
        
    




(I'm not sure if the xsl is being shown because of the way 
the html tags are stripped from the code - even though I 
wrap the whole thing in pre tags)

Add comment

Keep minimal XML tags, but still simple code., J Wynia, 2002/02/25
Creating a new tag for each translation will leave you with a rather cumbersome set of tags. Here's another way.
XML

<label>
     <name>hello</name>
     <translation>Hello!</translation>
</label>
XSLT
<xsl:value-of select="/labels/label[name='hello']/translation"/>

Add comment

The code from my earlier comment, Tim Watts, 2001/08/23

OK, here is the code after escaping all the < and >'s

(I'm never doing that again!!!)

XML
<phrases>
  <hello>Hello There</hello>
  <goodbye>Goodby Ya'll</goodbye>
</phrases>


XSL
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="language" select="document(~langname~.xml'))/phrases" />

<xsl:template match="/">
    <translated-phrases>
    ~langname~
        <translation>
            <xsl:value-of select="$language/hello" />
        </translation>
        <translation>
            <xsl:value-of select="$language/goodbye" />
        </translation>
    </translated-phrases>
</xsl:template>

</xsl:stylesheet>

Add comment



Highest rated recipes:

1. Search and Replace

2. Generating a newline

3. Internationalization ...

4. Restricting processing ...

5. Result Pagination with ...

6. Fetching information ...

7. Getting text children of ...

8. Creating empty elements




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.