Add an XLST file to the “Developer” section of your Umbraco project. You can call it lang_lib.xslt or another name of your choice. Below is a complete sample of the code that needs to be included:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl=http://www.w3.org/1999/XSL/Transform
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
xmlns:kv_trans ="urn:kv_trans"
exclude-result-prefixes="msxml umbraco.library kv_trans">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="initlang">en</xsl:param>
<xsl:variable name="dlang">
<xsl:choose>
<xsl:when test="umbraco.library:RequestQueryString('lang') != '' ">
<xsl:value-of select="umbraco.library:RequestQueryString('lang')" />
</xsl:when>
<xsl:when test="umbraco.library:Session('lang') != ''">
<xsl:value-of select="umbraco.library:Session('lang')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$initlang" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="flang">
<xsl:if test="$dlang != '' and $dlang != $initlang ">
<xsl:value-of select="concat('_',$dlang)" />
</xsl:if>
</xsl:variable>
<xsl:template name="setlang">
<xsl:value-of select="umbraco.library:setSession('lang',$dlang) " />
</xsl:template>
</xsl:stylesheet>
Reference to this XSLT file is to be included on all pages that needs translation. More on that later.
What does it do?
A variable, $dlang, is assigned the resolved language code. This variable can be used for dictionary lookups, hence the “d”. Resolving the language code is done by checking the querystring and the session for a defined language. Notive that the querystring has priority, so that changing the language is as simple as passing a new query parameter.
If the language is not found, it uses the initial language “initlang” defined at the top of the stylesheet. You can here change “en” to a default language-code that fits your needs.
Another variable - $flang – is also set, but only if its not the initial language. The $flang is created by concatenating an underscore and the $dlang, and is used for localized field-value fetches – hence the “f”. (more on this later)
Finally, a named template method “setlang” is defined. This method should be called at the top of all pages that contains the option of language-selection – in practise all pages. To make sure its called for all pages, and only once, place the call in an XSLT used in one of the top-most master templates. The call is simply done with a “call-template” element like this:
<xsl:template match="/">
<xsl:call-template name="setlang" />
...
</xsl:template>
Obviously, the library file must also be included in the XSLT file for the call to work. (See below.)