Copied to clipboard

Flag this post as spam?

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


  • Floffy 26 posts 33 karma points
    Jan 13, 2010 @ 22:13
    Floffy
    0

    Getting a list of languages

    Hi,

    is there a way to get a list of languages (from the Settings tab)?

    I'm trying to create a languages selector marco, and aiming at a output like:

    (Based on a site with English, Danish and German installed)

    <ul>
      <li>en</li>
      <li>da</li>
      <li>de</li>
    </ul>

     

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Jan 13, 2010 @ 22:50
    Thomas Höhler
    1

    There is no build in feature for xslt, but you can either create your own xslt extension or use a .net usercontrol, both pupolating the languages from the umbraco.cms.businesslogic.language.Language.getAll function:

    public static Language[] getAll
    {
    get
    {
    List<Language> list = new List<Language>();
    using (IRecordsReader reader = SqlHelper.ExecuteReader("select id from umbracoLanguage", new IParameter[0]))
    {
    while (reader.Read())
    list.Add(new Language(reader.GetShort("id")));
    }
    return list.ToArray();
    }
    }

    hth, Thomas


  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 13, 2010 @ 22:54
    Lee Kelleher
    1

    Hi Floffy,

    There is currently no umbraco.library function that will give you a list of languages used.

    You would need to write an XSLT extension to do this.  Luckily, "here's something I prepared earlier" ...

    public static XPathNodeIterator GetLanguages()
    {
        XmlDocument xd = new XmlDocument();
        xd.LoadXml("<languages/>");
        try
        {
            Language[] languages = Language.getAll;
    
            foreach (Language language in languages)
            {
                XmlNode node = xmlHelper.addTextNode(xd, "language", String.Empty);
                node.Attributes.Append(xmlHelper.addAttribute(xd, "id", language.id.ToString()));
                node.Attributes.Append(xmlHelper.addAttribute(xd, "culture", language.CultureAlias));
                node.Attributes.Append(xmlHelper.addAttribute(xd, "name", language.FriendlyName));
    
                xd.DocumentElement.AppendChild(node);
            }
        }
        catch (Exception ex)
        {
            xd.DocumentElement.AppendChild(xmlHelper.addTextNode(xd, "error", ex.Message));
        }
        return xd.CreateNavigator().Select("/");
    }

    You can either compile this into your own DLL, or use a custom script block within the XSLT, more info about that here.

    Good luck, Lee.

  • Floffy 26 posts 33 karma points
    Jan 14, 2010 @ 22:54
    Floffy
    0

    Hi Lee,

    I used your code and  compiled it into a dll, and registred it (as descriped here: http://en.wikibooks.org/wiki/Umbraco/Create_xslt_exstension_like_umbraco.Library_in_C)

    It might be that is getting late but i can't figure out how to use it in my xslt? (all I'm getting is nothing)

    I tryed something like this: (LanguageMetods:GetLanguages() calls your method)

    <ul>
    <xsl:for-each select="LanguageMetods:GetLanguages()">
      <li> <xsl:value-of select="@culture"/> </li>
    </xsl:for-each>
    </ul>

     

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Jan 15, 2010 @ 09:18
    Thomas Höhler
    1

    just use a copy of to see the complete results what you get:

    <xsl:copy-of select="LanguageMetods:GetLanguages()"/>

    But in the code from aboe you get a structure like this:

    <languages>
    <language id="" name="" culture=""/>
    <language id="" name="" culture=""/>
    </languages>

    So your code should be like this:

    <ul>
    <xsl:for-each select="LanguageMetods:GetLanguages()/languages/language">
     
    <li> <xsl:value-of select="@culture"/> </li>
    </xsl:for-each>
    </ul>

    hth, Thomas

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jan 15, 2010 @ 10:35
    Lee Kelleher
    0

    Thanks Thomas, much appreciated. :-D

  • Floffy 26 posts 33 karma points
    Jan 16, 2010 @ 14:05
    Floffy
    0

    Awesome guys, got it up and running

  • Ivan 165 posts 543 karma points
    Mar 25, 2013 @ 18:55
    Ivan
    0

    Hi guys.

    Any improvements since 2010?

    The explanations above look kind of advanced. Isn't there built-in language collection in Umbraco?

Please Sign in or register to post replies

Write your reply to:

Draft