Copied to clipboard

Flag this post as spam?

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


  • Roger 195 posts 474 karma points
    Sep 10, 2013 @ 18:04
    Roger
    0

    If current URL contains

    Hi all,

    just a quick one if anyone can help please...

    I am trying to check whether the current URL contains a culture name and if so, write a style sheet to screen.

    The cultures in the URLs are either en or cy so a typical URL would be:

    www.thissite.com/en/thispage.aspx or www.thissite.com/cy/thispage.aspx

    My XSLT is:

    <xsl:variable name="url" select="umbraco.library:RequestServerVariables('URL')" />
    <xsl:variable name="culture" select="cy" />

        <xsl:if test="contains($url, $culture)">
            <link href="/css/cy.css" rel="stylesheet" type="text/css" />

    </xsl:if>

    The stylesheet is writing to screen on both URL's

    Can anyone shed some light on this please?

    Thanks!

  • Roger 195 posts 474 karma points
    Sep 10, 2013 @ 18:27
    Roger
    100

    I've just sorted the solution:

    <xsl:variable name="currentURL" select="umbraco.library:RequestServerVariables('URL')" />
    <xsl:variable name="cultureRequest" select="contains($currentURL, '/cy')" />

        <xsl:if test="$cultureRequest">
            <link href="/css/cy.css" rel="stylesheet" type="text/css" />
    </xsl:if>

  • Chriztian Steinmeier 2741 posts 8406 karma points MVP 6x admin c-trib
    Sep 11, 2013 @ 09:37
    Chriztian Steinmeier
    0

    Hi Roger,

    Just a quick note with another way to deal with this:

    The "cy" in the URL probably comes from a node, right? E.g. I usually do this:

    • Content
      • website.com (Website node)
        • Products (Textpage node)
      • website.dk (Website node)
        • Produkter (Textpage node) ...

    And then I have an umbracoUrlName property on the Website nodes where I put the language code, which then automatically gives me the /da or en/ part of the URLs.

    So in XSLT I can add a stylesheet like the one in your example, just by taking the umbracoUrlName property of the Website ancestor, however deep the current page may be, e.g.:

    <xsl:variable name="languageCode" select="$currentPage/ancestor-or-self::Website/umbracoUrlName" />
    <link href="/css/{$languageCode}.css" rel="stylesheet" type="text/css" />
    

    The advantage to this is of course that you're not inspecting the URL, and you don't have to add a new code fork for every new language you add (and you won't accidentally get the cy.css file on pages called /cycling /cyanide-stain-removal etc. :-)

    /Chriztian

  • Roger 195 posts 474 karma points
    Sep 11, 2013 @ 10:36
    Roger
    0

    Hi Chriztian,

    The cultures come from hostnames set against 2 homepage nodes, en and cy set to en-GB and cy-GB (see attached)
    So when the language selector is used it sets www.mysite.com/en or /cy

    Testing the code you wrote above writes this to the page regardless of which language is selected:

    <link href="/css/.css" rel="stylesheet" type="text/css" />

    Thanks

    Roger

  • Roger 195 posts 474 karma points
    Sep 11, 2013 @ 10:37
    Roger
    0

     

     

  • Chriztian Steinmeier 2741 posts 8406 karma points MVP 6x admin c-trib
    Sep 11, 2013 @ 10:52
    Chriztian Steinmeier
    0

    Hi Roger,

    Yes - we're using the same setup, I can see - I'm just using an umbracoUrlName property so I don't have to put "en" and "cy" as the names of my "Website" nodes.

    To make it work in your setup, you should take the @nodeName instead (and I've made it use @level instead of the DocumenTypeAlias so it's more portable):

    <xsl:variable name="languageCode" select="$currentPage/ancestor-or-self::*[@level = 1]/@nodeName" />
    <link href="/css/{$languageCode}.css" rel="stylesheet" type="text/css" />
    

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft