Copied to clipboard

Flag this post as spam?

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


  • Eran 292 posts 436 karma points
    Aug 14, 2010 @ 18:31
    Eran
    0

    How to Convert Resource item (resx) with line breaks to JS variable ?

    hello,

    i'm using dictionary item, and i want to enable the customer to enter some text with line breaks.
    (customer use backoffice to edit the dictionary text, and hit enter when he wants to insert line break.)
    till here nothing special.

    the problem is that i assign the dictionary item value to js variable, it causing js error, because it can't process correctly the linebreaks.

    this code is NOT working (when the dictionary item contains line breaks):

    var errName =  "<xsl:value-of select='umbraco.library:GetDictionaryItem("error.name")' disable-output-escaping='yes' />";

    i tried umbraco.library:ReplaceLineBreaks, xslt replace and js replace to replace the resx line breaks with "<br />" but with no help.  (i think that  the solution is to replace the linebreaks with br, but probably i'm doing it wrong..).

    any suggestions?

    Thanks!

     

  • Sascha Wolter 615 posts 1101 karma points
    Aug 15, 2010 @ 17:47
    Sascha Wolter
    0

    Hi eran,

    How about trying something like this:

    <xsl:variable name="errorNameString" select="string(umbraco.library:GetDictionaryItem('error.name')" />
    <xsl:variable name="errorName" select="Exslt.ExsltStrings:replace($errorNameString, '\n', '&lt;br /&gt;')" />

    So the idea being to replace the line breaks by hand. Unfortunately I don't know which type of line breaks get saved with dictionary items, so you probably have to try out a bit, e.g. with \n\r.

    Hope that helps,
    Sascha

  • Eran 292 posts 436 karma points
    Aug 15, 2010 @ 18:27
    Eran
    0

    Thanks for your time, but the problem is still exist. i can't find the type of line break that get saved in the dictionary item.

    i tried using replace function:

    <xsl:variable name="errorName" select="Exslt.ExsltStrings:replace($errorNameString, '\n', '&lt;br /&gt;')" />

    with different combination, like: "\r\n", "\u", "\0d\0a", ".%0D%0A" and couple more, but with no success..

    Thanks,

    Eran.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Aug 15, 2010 @ 19:41
    Morten Bock
    0

    Another way to go could be to render the text in a hidden <div> or <textarea>, and then setting your variable to the text contained in that html element. That way you wont have to worry about it.

    Or you could UrlEncode the text in your xslt, and the UrlDecode it in your javascript when setting the variable.

  • Eran 292 posts 436 karma points
    Aug 15, 2010 @ 23:16
    Eran
    0

    thanks morten - i tried to use umbraco.library:urlEncode that works, but after that i needed to decode it in javascript. because as i understand there is no js function for that i use some decode function that i found on the web. still, with no success.

    also, i tried the second method with the hidden div and with no success also.

    i'm very frustrated, but i spent too much time on this, and i must move on. i hope to get back on that when i have more time because its somthing simple and it is very annoying.. (this is some enhancment that i want to do on my Contact Form Packages that i released).

    Thanks for the help and if someone have more ideas i will love to here.

    Eran.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Aug 16, 2010 @ 01:17
    Chriztian Steinmeier
    0

    Hi eran,

    This could be a long shot, but since it's XML you should try this:

    <xsl:variable name="errorName" select="Exslt.ExsltStrings:replace($errorNameString, '&x0A;', '&lt;br /&gt;')" />

    An XML parser must normalize all linefeeds into a single &#x0A; character, regardless of platform - I don't know how the value from a Dictionary item is returned, but it's worth a try...

    /Chriztian

     

  • Eran 292 posts 436 karma points
    Aug 16, 2010 @ 11:11
    Eran
    0

    @Chrizian, thanks, i declare new entity:

    <!ENTITY linebreak "&#x0D0A;">

    and also tried

    <!ENTITY linebreak "&#x0A;">

    and then:

    <xsl:variable name="errorNameString" select="umbraco.library:GetDictionaryItem('error.name')" />
    <xsl:variable name="errorName" select="Exslt.ExsltStrings:replace($errorNameString, '&linebreak;', '&lt;br /&gt;')" />

     

    when i print the value of the $errorName, it displayed it without the linebreak. but also it display the $errorNameString without line break...
    it seems that it ignore the linebreak.

    but when i try to put the value in the js variable, it breaks the code:

    var errName =  "<xsl:value-of select='$errorName' />";
  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Aug 16, 2010 @ 17:29
    Morten Bock
    0

    I would still go with printing out the content to an html element, and then read it back from there. Could you post the code that you used when trying this? What was the problem with it? Should be pretty straight forward.

  • Eran 292 posts 436 karma points
    Jan 05, 2011 @ 22:07
    Eran
    0

     

    i managed to do the line break cleaning.
    i choose to develop xslt extenstion, that handle this.

    this is the code in the xslt extension:

    public static string GetDictionaryItem(string item, int lang)         
    {            
       
    string translation;
       
    try {  
           translation
    = new umbraco.cms.businesslogic.Dictionary.DictionaryItem(item).Value(lang);
       
    }            
       
    catch (Exception){
          translation
    = string.Empty;
       
    }
       
    // need to check for empty string to show keyname when item not translated
       
    if (string.IsNullOrEmpty(translation))        
           translation
    = item;        
       
    return translation;            
    }

    and in the xslt:

     <xsl:variable name="languageID" select="RVContactForm.XsltExtensions:GetLanguageIdFromNodeId($currentPage/@id)" />
     <script type="text/javascript">
        var errName =  "<xsl:value-of select='RVContactForm.XsltExtensions:GetDictionaryItem("RV-ContactForm.error.name",$languageID)'/>";
     </script>

     

    everything is tested and works, and the line breaks is removed (and i can even add other string manipulations).

    Thanks,

    Eran.

     

Please Sign in or register to post replies

Write your reply to:

Draft