Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using translation strings in componentlinks

I've been trying to solve this problem since this morning, and I know I'm missing something obvious here but I can't seem to find it.

We are using an XML file that is published to the server which contains translations of all the standard words, such as 'read more'. It is a page with a component that is localized in the appropriate publication.

In our Razor templates we use the following code below a plain news Summary item which in turn links to the full item.

<a tridion:href="@news.ID" class="more" ><%=DefaultLabels.TranslatedTerm(((HomePage)Page).Location, "read_more")%></a>

Thing is, the server tag works just fine, but gets resolved as

<tridion:ComponentLink runat="server" PageURI="tcm:15-407-64" ComponentURI="tcm:15-1475" TemplateURI="tcm:0-0-0" AddAnchor="false" LinkText="&lt;%= DefaultLabels.TranslatedTerm(((HomePage)Page).Location, &#34;read_more&#34;) %&gt;" LinkAttributes=" class=&#34;more&#34;" TextOnFail="true"/>

As you might notice, it gets written as plain text on the page (no surprise there because the LinkText parameter is declared as String in the first place according to the liveDocs).

If I take away the

tridion:href

in the first example, and write it as

href

It works fine, the code resolves into a translated string and it's even linked... to nothing more than the TCM ID of the component, not the right page with the full news item on it.

I've tried creating a function in Razor, tried replacing the linkText, tried setting the ComponentLink in the template itself, but to no avail. I feel that it should work with just a minor adjustment to this template's code, but I fail to see it and I've started looking at custom TBB's to handle the code.

Anyone have an idea how to resolve this?

EDIT:

Chris' answer was actually the one I was looking for in this particular situation, but I feel that I should point out that Priyank's function is something that should be regarded as such as well. So thank you both for the help, it made my life somewhat easier now!

like image 369
MDa Avatar asked Aug 27 '12 15:08

MDa


2 Answers

I hope this razor function will help you lot. This is very helpful function to render the link tag from the component link or external link.

@helper RenderLink(
    dynamic link,                        // the link to render. Handles components + internal / external links
    string cssClass = null,              // optional custom CSS class
    string title = null                 // optional link text (default is the title of the component being linked to)
    ) 
{
    if(link == null) 
    {
          return;
    }

    if (title == null) 
    {   
        title = link.title;
    }

    string classAttr = string.IsNullOrEmpty(cssClass) ? "" : " class='" + cssClass + "'";
    dynamic href;
    string tridionLink = "";
    string targetAttr = "";

    if (link.Schema.Title == "External Link") 
    {
        href = link.link;
    }
    else if (link.Schema.Title == "Internal Link")
    {
        href = link.link;
        tridionLink = "tridion:";
    } 
    else 
    {
        href = link;
        tridionLink = "tridion:";
    }    

    if(link.target != null) 
    {
        targetAttr = link.target == "New window" || link.target == "Popup" ? " target='_blank'" : "";
    }    

    <a @(tridionLink)href="@href"@classAttr@targetAttr>@title</a> 
}
like image 114
Priyank Gupta Avatar answered Nov 01 '22 08:11

Priyank Gupta


I would suggest not using the default templates for resolving your links, rather output the link yourself something like this:

<tridion:ComponentLink runat="server" PageURI="tcm:15-407-64" 
    ComponentURI="tcm:15-1475" TemplateURI="tcm:0-0-0" 
    AddAnchor="false" LinkAttributes=" class=&#34;more&#34;" 
    TextOnFail="true">
        <%=DefaultLabels.TranslatedTerm(((HomePage)Page).Location, &#34;read_more&#34;) %>
</tridionComponentLink>

Better still you might consider outputting TCDL rather than the Taglib/ServerControl

like image 41
Chris Summers Avatar answered Nov 01 '22 09:11

Chris Summers