Written by torleik     For umbraco versions: umbraco3.0

How-to
How to create multilingual sites with only a single navigation-tree, > using 'tabbed translation' and the dictionary.

Chapters

5) Use the dictionary with an XSLT extension

When you create entries in the dictionary, you do as normally in Umbraco – create the entry using the key and adding a translation for each language.

dictionary

In this example, the website title is a dictionary item, using the key “sitetitle”.

To refer to this key, we want to simply use a library function to look up by key and language. Unfortunately, the current umbraco:library does not support that, so lets just make our own extension and add this support.

Create a Visual Studio project - Class library - and include a reference to the umbraco.dll file.

Create a class to hold the translation code. You could call it “Translations”. Create a public method that will be the method you invoke from the XSLT files. Call it “translate”. My implementation looks like this:

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Xml.XPath;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.language;
// Adds dictionary lookup as an extension for XSLT files. /TORK 2008.04.30
namespace kraftvaerk.umbraco
{
    public class Translations
    {
    /// <summary>
    /// Look up a translated string in the Umbraco dictionary using the provided key and language code
    /// Returns the translated string if found and not empty. Else, it returns the key and the language
    /// surrounded with "*" for easy detection and correction.
    /// </summary>
    /// <param name="key">The key by which the dictionary entry was made</param>
    /// <param name="language">The desired language code- as it appears on the dictionary tabs</param>
    /// <returns>The translated string, if found and not empty. returns>
    public static string translate(string key, String language)
    {
        int langid = 1;
        if (language.Length != 2) language = "en"; // hardcoded default language.
        Reimplement as desired.
        Language lang = Language.GetByCultureCode(language);
        langid = lang.id;
        string trans = GetDictItemByLang(key, langid);
        if (trans.Length > 0) return trans;
        return ("*" + key + "(" + language + ")" + "*");
    }

    /// <summary>
    /// Lookup the key in the Dictionary, then get the value for the correct language-id
    /// </summary>
    /// <param name="key">The key by which the dictionary entry was made</param>
    /// <param name="languageid">The id of the choosen language</param>
    /// <returns>The translated string or the empty string. returns>
    private static string GetDictItemByLang(string key, int languageid)
    {
        try { return new Dictionary.DictionaryItem(key).Value(languageid); }
        catch { return string.Empty; }
        }

    }

}

Please change the namespace :-)

Add the extension declaration to the \config\xsltExtensions.config file.

If you have no other extensions registered, it should look something like this, when you’re done:

<?xml version="1.0" encoding="utf-8" ?>
<XsltExtensions>
    <ext assembly="/bin/kv_umb_lib" type="kraftvaerk.umbraco.Translations" alias="kv_trans" />
</XsltExtensions>

Change the assembly name, the namespace and the alias as appropriate to your needs.


Brilliant umbraco hosting provided by FAB-IT