Forum Discussion
luk
8 years agoBoss
Problem 1) Plural of nouns can also be handled via Lithium Text Strings:
// your lithium string (add in Text Editor in the Studio section) would look // something like this: // custom.best-answer = Best {0,choice,0#Answers|1#Answer|1<Answers} <li class="item">${bestAnswersCount} ${text.format('custom.best-answer', count)}</li>
Problem 2) Shortening of numbers, I usually do this with the follwing function (maybe overkill for your usecase):
<#function math_shorten number> <#local units = [ {"factor": 1, "unit": ""}, {"factor": 1000, "unit": "K"}, {"factor": 1000000, "unit": "M"}, {"factor": 1000000000, "unit": "G"}, {"factor": 1000000000000, "unit": "T"}, {"factor": 1000000000000000, "unit": "P"}, {"factor": 1000000000000000000, "unit": "E"} ] /> <#local order = number?abs?round?c?length /> <#local i = ((order - 1) / 3)?floor /> <#if (i < 0)> <#local i = 0 /> </#if> <#local result = (number / (units[i].factor))?string("0.#") + units[i].unit /> <#return result /> </#function> // you would replace your ${bestAnswersCount} with the following: ${math_shorten(count)} // then you would finally wind up with: <li class="item">${math_shorten(count)} ${text.format('custom.best-answer', count)}</li>
The shortening function is pretty resilient, it will handle negative numbers, cut off a .0 ending etc., the only thing it will break with is if you exceed the maximum possible unit, e.g. you end up asking for an item in the units sequence that does not exists, the likelihood of this happening is very low though...
Related Content
- 12 years ago
- 11 years ago