Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jan Vermeerbergen 79 posts 112 karma points
    Sep 20, 2012 @ 11:16
    Jan Vermeerbergen
    0

    Use dictionary items in multilanguage site in 1 tree

    Just trying to get my head around this:

    I am developing a site in 2 languages. The client doesn't want to 2 trees (one for each language). Is there any way I can still use dictionary items in my XSLT's and somehow pass along which language I want to display?

    Thanks

    J

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Sep 20, 2012 @ 12:35
    Chriztian Steinmeier
    0

    Hi Jan,

    I don't think you can get the "correct" behaviour this way (i.e. leveraging the .NET culture info) - but there is of course more than one way to skin a cat :-)

    You can try this setup, where you have separate tabs for each language and postfixed properties (title, title_fr, title_de etc.)

    Note that the default language has no postfix.

    In your XSLT you can do this then to magically select the right property:

    <!-- Grab this however you want, e.g. from QueryString or other setting -->
    <xsl:variable name="currentLanguage" select="'fr'" /> 
    
    <xsl:template match="/">
    
        <xsl:apply-templates select="$currentPage/title" mode="translate" />
    
    </xsl:template>
    
    <!-- Example template for 'title' property -->
    <xsl:template match="title | title_fr | title_de">
        <h2><xsl:value-of select="." /></h2>
    </xsl:template>
    
    <xsl:template match="*" mode="translate">
        <!-- Find the translated property to output -->
        <xsl:variable name="translatedProperty" select="../*[name() = concat(name(current()), '_', $currentLanguage)]" />
        <xsl:if test="normalize-space($translatedProperty)">
            <xsl:apply-templates select="$translatedProperty" />
        </xsl:if>
    
        <!-- Fallback to the default (non-prefixed) language when no translation available -->
        <xsl:apply-templates select="self::*[not(normalize-space($translatedProperty))]" />
    </xsl:template>

    (You don't need a matching template for every property if you just need to write the content)

    If you need this functionality in more than one macro you can save the "translator" template in a separate file and use the <xsl:include> statement to pull it into the ones using it.  

    /Chriztian

     

Please Sign in or register to post replies

Write your reply to:

Draft