Developer forum

Forum » Templates » Template translation function

Template translation function


Reply
In my Newsletter v3 module I'm using a xslt template to edit newsletter recipients (NewsletterRecipientEdit.xslt).

I would like to use the template translation function, but I can't just use the tag @Translate(File name, "Filnavn") in a xslt file.

Can anyone give me an example or sample file on how to do this?

Replies

 
Nicolai Høeg Pedersen
Reply

Translations
When working with the Dynamicweb CMS built-in translationsystem it’s very easy to use in XSLT since all translations are saved in XML files.
Just like the XML files generated by ‘savetemplatexml=true’, the translation files are stored in the actual Template specific folders.  So all translations to page templates are stored in ‘/Templates/Page/translations.xml’
 

So how we use it ?
Using it is pretty straight forward.
All you need to do is load the translations.xml for the given template type, find the wanted key and grabbing the output.
1. Loading translations.xml
2. Getting the current language
3. Call the translation XSLT template


Loading translations.xml
<xsl:variable name="localtranslationfile" select="document('http://demo.core.dynamicweb.dk/Files/Templates/Page/Translations.xml')" /> or use @import.

Getting the current language
<xsl:variable name="currentlanguage" select="/Template/GlobalTags/Global.Area.LongLang" />

Call the translation template

<xsl:call-template name="translate">
 <xsl:with-param name="translationkey" select="'ProductSearchTitle'" />
 <xsl:with-param name="defaulttranslation" select="'Product Search'" />
</xsl:call-template>


The 2 parameters are:
translationkey:  which is the search key in translation.xml
defaulttranslation: which is the translation to be used if the key is not found in translation.xml
The actual translation template looks like this:
<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>

 

 
Reply
Hi Nicolai,

Just clarify one thing for me. Loading the translation file, you have to change the url to our solution correct?

This is an issue for us because we have a development machine and domain, and when everything is ready, we move the solution to the production server (or client server). If we have to update the absolute file path, we have to go through all the templates again, hence my request to put the domain in the global tags

Best Regards,
Nuno
 
Reply
Hmm, I'm getting "Error loading xslt".
I might have done something wrong. Do you mind taking a look at my file?
I filled your code into line 10-15 and the first name to be translated "Dit navn" line 33-44.
 
Nicolai Høeg Pedersen
Reply
@Nuno.

Yes - the uri should be changed.

@m.stigaard

Your file looks OK. Try removing the http:// part and make the link relative:

Like:
select="document('/Files/Templates/NewsLetterV3/Translations.xml')"
 or
select="document('Files/Templates/NewsLetterV3/Translations.xml')"
or
select="document(Translations.xml')"
 
Nicolai Høeg Pedersen
Reply
And make sure you are running on a 19.1.* version...
 
Reply
I get this error:

Error loading xslt:
Saved to /Files/XmlXsltParserError.xml
System.Xml.Xsl.XslLoadException: 'xsl:template' cannot be a child of the 'td' element.

I'm not that strong in xslt, so I might have placed the section "<xsl:template name="translate">" in a wrong place?
 
Nicolai Høeg Pedersen
Reply
You need to have the <xsl:template name="translate"> outside the <xsl:template match="/Template"> - and use the call-template inside to call the translate template.

Also define your variables outside the xsl:template objects.
 
Reply
Okay, now it get the translation part. I'll attatch the file in case someone can find it usefull.

I loaded the translation xml like this:
<xsl:variable name="translate" select="document('../Translations.xml')" />

Some questions:
Line 18: EditRecipientUrl is translated with double 'amp' (action="/Default.aspx?ID=364&amp;amp;Action=1&amp;amp;PID=386") Why? I can't submit the form. Bug?

Line 44: Why is AccessUserName_validator not translated? (and the other _validator values) - Bug?

Line 140 & 151: How do I self-close the input tag with a backslash?

Michael :-)
 
Reply
Hi,

I can help with some of the questions
Line 44: Why is AccessUserName_validator not translated? (and the other _validator values) - Bug? - You set the value in the paragraph module settings, so there's no need for translation

Line 140 & 151: How do I self-close the input tag with a backslash? - to achieve this your template must output in xml format. change on line 6 from method="html" to method="xml". There is a downside though. You cannot have empty elements like:
<div class="xpto"></div>. As a workaround do this <div><xsl:comment /></div>

as for the problem in line 18, I'll let someone from Dynamicweb answer :P

Best Regards,
Nuno
 
Reply
Thanks! Changing html to xml solved that lille problem.

I was unclear about AccessUserName_validator: I know the value is set in the paragraph module settings, but my values is not loaded with this xslt file and is also empty in the TestData.xml file. The values works fine when using a html template.
 
Reply
Well, if it's empty... :s

better wait until someone from Dw answer to that too

//nuno
 
Tymen NNM
Reply

I'm am trying to accomplish the same thing but I'm using using an Navigation XSLT that works on the NavigationTree. I have followed the instructions from Nicolai in the first post, but it seems the value of currentlanguage is empty:

<xsl:variable name="currentlanguage" select="/Template/GlobalTags/Global.Area.LongLang" /> 

I have been trying all sorts of variations to try and display the valua of Global.Area.LongLang, but it does not display any value. For example:

<div><xsl:value-of select="Global.Area.LongLang" />!</div> 

When I harcode the currentlangage in (i.e. en-EN) the template in works fine and the correct translation is given. So it seems only the loading of the correct language fails.

 
Tymen NNM
Reply

Ok I fixed it. smiley The path is different from within the Navigation xml. It should be:

/NavigationTree/Settings/GlobalTags/Global.Area.LongLang

 

You must be logged in to post in the forum