fluid icon indicating copy to clipboard operation
fluid copied to clipboard

CultureInfo for ToNumberValue

Open darman96 opened this issue 3 years ago • 5 comments

There should be a way to specify a CultureInfo for ToNumberValue (if there isn't one already?) I'd propose an overload which accepts a CultureInfo as parameter or a global option to set a CultureInfo.

darman96 avatar Mar 14 '22 16:03 darman96

Did you looked into the docs?

hishamco avatar Mar 14 '22 17:03 hishamco

The ToNumberValue method seems to be unaffected by the CultureInfo set in the TemplateOptions. We have a custom filter which gets a string as input that contains decimal numbers in german formatting, e.g. "3,5" and ToNumberValue would parse this string as 35 regardless of the CultureInfo.

darman96 avatar Mar 14 '22 19:03 darman96

Source of the problem: https://github.com/sebastienros/fluid/blob/main/Fluid/Values/StringValue.cs#L139

There are a few other locations using InvariantCulture explicitly instead of the configured culture. We need to think about each case, it's not obvious to me that it should use it for converting values, I would not want to miss existing scenarios that are opposite to this. First thing would be to check what other engines do (Ruby, LiquidJS).

In the meantime as a mitigation, you can create a custom filter that will do the correct conversion for you.

sebastienros avatar Mar 14 '22 19:03 sebastienros

test cases: {{ '3,5' | plus: 1 }}

Note that the liquid template has to use the invariant numbers natively, it's part of the language. {{ 1.5 | plus: '1,5' }}

Related question, do date/time filters use the configured culture by default or an invariant one. There are options to render with local formats.

sebastienros avatar Mar 14 '22 19:03 sebastienros

Looks like Shopify's Liquid expects an invariant string as the input: https://github.com/Shopify/liquid/blob/68c3827ef21945c48c273e40a7cb5ac68b68eb30/lib/liquid/utils.rb#L55

I am not opposed to using the configured culture even for inputs, but that would mean that it's now forcing all inputs to be formatted in the configured culture. We need to think about side effects. At least it is not a breaking change for InvariantCulture since this is the current default.

sebastienros avatar Mar 14 '22 19:03 sebastienros