I have a property where my client enters the number of kilometers that a car have driven. He would like the field to auto seperate numbers above 999 with a . (dot) as is tradition in Denmark.
Ie: 1.000 km instead of 1000 km
How can i do that in XSLT on a property value? Searching the forum didn't give me much to go by :/
I'm not sure how the select the language.. do you have an example on this? Should I use the umbraco library method or?
I really haven't worked with uComponents yet, but yes it might be useful and it has many features I will need in the future. But I would like if it possible to get the language just with xslt ...
Thanks for the tip with ###,###.00 ...so you can keep zero's at the end.. when I use ###,###.00 it works on the English version.. but it the xslt crash on the Danish version of site..
The "crashing" on the danish version is because you need to specify the pattern string in "danish" (i.e., using the characters you've specified for the named decimal-format you're using) - so you'll need to use ###.###,00 in the danish version.
Yes I have tried with ###.###,00 but it will crash xslt on the English version of site then... I think the way Lee suggests should use the different pattern for the file size with comma and dot.
I also get e.g. 11.36 MB in English, but somehow I get more decimals in Danish, 11,35964 MB ... it seems to give me to right output, but not same number of decimals..
OK - what I actually mean is that you need to use BOTH patterns - but the right one for each language - does that make sense?
I've done something like this before:
<!-- :: Decimal formats :: -->
<xsl:decimal-format name="prices-da"
decimal-separator=","
grouping-separator="."
NaN="(Ikke tilgængelig)"
/>
<xsl:decimal-format name="prices-en"
decimal-separator="."
grouping-separator=","
NaN="(Not available)"
/>
<!-- Build a single variable $formats to hold info for use e.g. when formatting prices -->
<xsl:variable name="format-mapper">
<formats>
<prices name="english" currency="GBP" xml:lang="en">
<pattern>#,###.00</pattern>
</prices>
<!-- Must be last as this is the one we want to fallback to! -->
<prices name="danish" currency="DKK" xml:lang="da">
<pattern>#.###,00</pattern>
</prices>
</formats>
</xsl:variable>
<xsl:variable name="formats" select="msxml:node-set($format-mapper)/formats" />
<!-- Formats the contents of any element as a price using the currently selected format -->
<xsl:template match="*" mode="formatted-price">
<xsl:variable name="currentFormat" select="($formats/prices[lang($lang)] | $formats/prices[lang('da')])[1]" />
<xsl:value-of select="substring(concat($currentFormat/@currency, ' '), (string(number(.)) = 'NaN') * 5 + 1, 4)" />
<xsl:value-of select="format-number(., $currentFormat/pattern, concat('prices-', $currentFormat/@xml:lang))" />
</xsl:template>
<!-- Build a single variable $formats to hold info for use e.g. when formatting prices --> <xsl:variablename="format-mapper"> <formats> <pricesname="english"currency="GBP"xml:lang="en"> <pattern>#,###.00</pattern> </prices> <!-- Must be last as this is the one we want to fallback to! --> <pricesname="danish"currency="DKK"xml:lang="da"> <pattern>#.###,00</pattern> </prices> </formats> </xsl:variable> <xsl:variablename="formats"select="msxml:node-set($format-mapper)/formats"/>
Can you see how I shall modify the code so it fits?
Here's something along the lines of how I'd do it:
<!-- :: Decimal formats :: -->
<xsl:decimal-format name="da" decimal-separator="," grouping-separator="." />
<xsl:decimal-format name="en" decimal-separator="." grouping-separator="," />
<!-- Build a single variable $formats to hold info for use e.g. when formatting numbers -->
<xsl:variable name="format-mapper">
<formats>
<pattern xml:lang="en">#,###.00</pattern>
<!-- Fallback must be last -->
<pattern xml:lang="da">#.###,00</pattern>
</formats>
</xsl:variable>
<xsl:variable name="formats" select="msxml:node-set($format-mapper)/formats" />
<!-- Lookup table for size thresholds -->
<xsl:variable name="sizes-mapper">
<size id="giga" abbr=" GB">1073741824</size>
<size id="mega" abbr=" MB">1048576</size>
<size id="kilo" abbr=" KB">1024</size>
<size id="byte" abbr=" Bytes">1</size>
</xsl:variable>
<xsl:variable name="sizes" select="msxml:node-set($sizes-mapper)" />
<!-- Formats the contents of any element as a storage value using the currently selected format -->
<xsl:template match="*" mode="storage">
<xsl:variable name="currentFormat" select="($formats/pattern[lang($lang)] | $formats/pattern[lang('da')])[1]" />
<xsl:variable name="divisor" select="$sizes/size[. <= current()][1]" />
<xsl:value-of select="format-number((. div $divisor), $currentFormat, $currentFormat/@xml:lang)" />
<xsl:value-of select="$divisor/@abbr" />
</xsl:template>
So when you need to output a storage value, just apply-templates to it in the "storage" mode, e.g.:
Thousand seperator in property value
Hi there
I have a property where my client enters the number of kilometers that a car have driven. He would like the field to auto seperate numbers above 999 with a . (dot) as is tradition in Denmark.
Ie: 1.000 km instead of 1000 km
How can i do that in XSLT on a property value? Searching the forum didn't give me much to go by :/
Hi Martin,
Because the Danish way differs from the "norm", you will need to do 2 things in your XSLT.
1. Declare the "decimal-format" towards the top of your XSLT, like so:
This effectively swaps over the decimal and thousands group separators.
2. Use a function called "format-number()", like so...
The second param defines the formatting, the third param references the decimal-format you declared earlier.
Cheers, Lee.
@Lee: So who died and made english the "norm" :-)
/Chriztian
Wow, hand on heart, I stand corrected... seems that there are more countries that use thousand-separator point and decimal comma, than my "norm"!
http://en.wikipedia.org/wiki/Decimal_mark#Hindu-Arabic_numeral_system
LOL!
Cheers, Lee.
Hi Lee
Thanks a bunch. My client went on holiday so i totally forgot this :)
You're a star
Hi Lee..
Nice tip :)
Do you have a suggestion, when having a multilingual site?
<xsl:decimal-format name="danish" decimal-separator="," grouping-separator="." />
<xsl:variable name="size" select="./umbracoBytes" />
<xsl:variable name="sizeAndSuffix">
<xsl:choose>
<xsl:when test="$size >= 1073741824">
<xsl:value-of select="format-number($size div 1073741824,'###.###,##', 'danish')"/>
<xsl:text> GB</xsl:text>
</xsl:when>
<xsl:when test="$size >= 1048576">
<xsl:value-of select="format-number($size div 1048576,'###.###,##', 'danish')"/>
<xsl:text> MB</xsl:text>
</xsl:when>
<xsl:when test="$size >= 1024">
<xsl:value-of select="format-number($size div 1024,'###.###,##', 'danish')"/>
<xsl:text> KB</xsl:text>
</xsl:when>
<xsl:when test="$size > 0 and $size < 1024">
<xsl:value-of select="format-number($size div 0,'###.###,##', 'danish')"/>
<xsl:text> Bytes</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0 Bytes</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
I would like to use the Danish format, when you have selected Danish as language and English format for the English version of site.
Bjarne
Hi Bjarne,
Here is one way to do it (there are other ways too).
Have both references to the decimal format:
Figure out which language is being used and assign that to a variable:
Then you can use that variable in the "format-number" function:
Alternatively, if you are using uComponents, then there is an XsltExtension in the IO library called FormatFileSize - it might be useful?
Cheers, Lee
Okay, thanks.. I see the idea..
I'm not sure how the select the language.. do you have an example on this? Should I use the umbraco library method or?
I really haven't worked with uComponents yet, but yes it might be useful and it has many features I will need in the future.
But I would like if it possible to get the language just with xslt ...
Bjarne
Hi Bjarne,
Difficult to offer a solution with knowing more about your set-up, but here's a suggestion...
If the Danish URLs are on a ".dk" domain, then you could check for that? Otherwise it fallback on English?
Cheers, Lee.
Well, both languages are on fonnesberg.com, but under /en and /da ...
So I have press photos here: http://fonnesberg.com/en/press/download/photos.aspx which are available for customers and others to use as press material.
But perhaps I can use the same way to check if the url contains a .com/da as well?
Bjarne
Hi Bjarne,
Swap the test condition with this...
Cheers, Lee.
Thanks, it almost works now.. :)
But there is a small problem with the decimals.. when I use ###.### I get two decimal in English.. and with ###.###,## I get two decimals in Danish..
If you look at this page in Danish: http://fonnesberg.com/da/presse/download/billeder/efteraar-2011.aspx I get e.g. 11,36 MB ... on the English version: http://fonnesberg.com/en/press/download/photos/autumn-2011.aspx 11.35964 MB ... if I use ###.### I get 11 MB and 11.36 MB ...
hmmmm... try "###,###.##"?
If you always wanted to show the decimal places, then use "###,###.00".
hm.. still not same numbers of decimals :/
Even not with ###,###
Thanks for the tip with ###,###.00 ...so you can keep zero's at the end.. when I use ###,###.00 it works on the English version.. but it the xslt crash on the Danish version of site..
Hmmm... not sure what the answer is. (but I didn't want to leave you hanging here.) If I think of anything, I'll let you know.
Hi Bjarne,
The "crashing" on the danish version is because you need to specify the pattern string in "danish" (i.e., using the characters you've specified for the named decimal-format you're using) - so you'll need to use ###.###,00 in the danish version.
/Chriztian
Hi Chriztian
Yes I have tried with ###.###,00 but it will crash xslt on the English version of site then...
I think the way Lee suggests should use the different pattern for the file size with comma and dot.
I also get e.g. 11.36 MB in English, but somehow I get more decimals in Danish, 11,35964 MB ... it seems to give me to right output, but not same number of decimals..
Bjarne
OK - what I actually mean is that you need to use BOTH patterns - but the right one for each language - does that make sense?
I've done something like this before:
And then to format a price:
Let me know if it's total gibberish to you :-)
/Chriztian
Yes, it makes more sense now :)
Well, I have this right now:
<xsl:decimal-format name="danish" decimal-separator="," grouping-separator="." />
<xsl:decimal-format name="english" decimal-separator="." grouping-separator="," />
<xsl:variable name="language">
<xsl:choose>
<xsl:when test="starts-with(umbraco.library:RequestServerVariables('URL'), '/da')">danish</xsl:when>
<xsl:otherwise>english</xsl:otherwise>
</xsl:choose>
</xsl:variable>
and this inside my loop..
<xsl:variable name="size" select="./umbracoBytes" />
<xsl:variable name="sizeAndSuffix">
<xsl:choose>
<xsl:when test="$size >= 1073741824">
<xsl:value-of select="format-number($size div 1073741824,'###,###.##', $language)"/>
<xsl:text> GB</xsl:text>
</xsl:when>
<xsl:when test="$size >= 1048576">
<xsl:value-of select="format-number($size div 1048576,'###,###.##', $language)"/>
<xsl:text> MB</xsl:text>
</xsl:when>
<xsl:when test="$size >= 1024">
<xsl:value-of select="format-number($size div 1024,'###,###.##', $language)"/>
<xsl:text> KB</xsl:text>
</xsl:when>
<xsl:when test="$size > 0 and $size < 1024">
<xsl:value-of select="format-number($size div 0,'###,###.##', $language)"/>
<xsl:text> Bytes</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0 Bytes</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
how much of your code above would I have to integrate then?
Bjarne
I guess I need to use the first part you wrote Chriztian:
Can you see how I shall modify the code so it fits?
You can see how it's working here: http://fonnesberg.com/en/press/download/photos.aspx and here: http://fonnesberg.com/da/presse/download/billeder.aspx when you hover the mouse over the thumbnails, you'll see the name and filesize of image.. but you can also see that on the english page I got two decimals and on the danish site version about 4-5 decimals..
Bjarne
Hi Bjarne,
Here's something along the lines of how I'd do it:
So when you need to output a storage value, just apply-templates to it in the "storage" mode, e.g.:
/Chriztian
Okay.. is it possible to still use the sizeAndSuffix variable:
<xsl:variable name="size" select="./umbracoBytes" />
<xsl:variable name="sizeAndSuffix">
<xsl:choose>
<xsl:when test="$size >= 1073741824">
<xsl:value-of select="format-number($size div 1073741824,'###,###.##', $language)"/>
<xsl:text> GB</xsl:text>
</xsl:when>
<xsl:when test="$size >= 1048576">
<xsl:value-of select="format-number($size div 1048576,'###,###.##', $language)"/>
<xsl:text> MB</xsl:text>
</xsl:when>
<xsl:when test="$size >= 1024">
<xsl:value-of select="format-number($size div 1024,'###,###.##', $language)"/>
<xsl:text> KB</xsl:text>
</xsl:when>
<xsl:when test="$size > 0 and $size < 1024">
<xsl:value-of select="format-number($size div 0,'###,###.##', $language)"/>
<xsl:text> Bytes</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0 Bytes</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
But I don't know if the I then still should have the $language variable there...
How much should be placed inside the loop?
Bjarne
is working on a reply...