messageformat.net icon indicating copy to clipboard operation
messageformat.net copied to clipboard

Bool True vs true

Open saithis opened this issue 7 years ago β€’ 5 comments

We encountered an incompatibillity with messageformat.js. When you use a boolean fΓΌr a select, then messageformat.js converts it to true/false for the key, but messageformat.net converts it to True/False.

I think lowercase true/false is the better option an it should be fixed here. (Maybe an option should be introduced, then it could be fixed while maintaining backwards compatibility.)

saithis avatar Oct 08 '18 11:10 saithis

Thats a .NET thing πŸ˜€ what does the spec say?

jeffijoe avatar Oct 08 '18 11:10 jeffijoe

http://userguide.icu-project.org/formatparse/messages: Use a "select" argument to select sub-messages via a fixed set of keywords.

Sounds like only strings should be used. But as your readme states compatibility with messageformat.js, I think booleans should be allowed and converted to lowercase.

saithis avatar Oct 08 '18 12:10 saithis

But as your readme states compatibility with messageformat.js, I think booleans should be allowed and converted to lowercase.

I state somewhat compatibility. πŸ˜‰

If the spec does not dictate how booleans should be treated, then it is up to the underlying platform to provide stringification. JS uses lowercase, .NET uses pascal case. I think we should stick with that, as doing myBool.ToString().ToLowercase() isn't that big of a deal in userland code. πŸ˜„

If you feel strongly that this should be addressed specifically by this library, please submit an API proposal (and even better, a Pull Request! It is Hacktober after all πŸ˜‰ )

jeffijoe avatar Oct 08 '18 13:10 jeffijoe

I would suggest an option to provide a type-converter to the message formatter, so anyone could provide their own approach/implementation for this. It would not only be useful with select (to convert to string from any value type), but also e.g. when you have a number value placeholder (e.g. { x, number }, { x, plural, ... } or { x, selectordinal, ... }) and the value provided is not a number, the type-converter would be used (to convert to number). Similarly for a date/time placeholder (e.g. when a date is provided in a string in a weird/custom format or using a custom class).

hilari0n avatar Jul 05 '22 13:07 hilari0n

What would the API for that look like @hilari0n ?

jeffijoe avatar Jul 05 '22 13:07 jeffijoe

@glen-84 do you have an opinion on this?

jeffijoe avatar Oct 11 '23 11:10 jeffijoe

PHP (8.2.1) also seems to use a string conversion/cast, so boolean true actually matches 1. Boolean false is an empty string, so it probably can't even be matched.

In that sense, this library is consistent. However, I'm not sure if there is a proper specification for MessageFormat 1.0. It might also be worth checking the Java and/or C(++) implementations. If they also use some form of string conversion, then I don't think that the default behaviour of this library should change.

As you've mentioned, if the user wants to use true/false keys, then they can pass in a true/false string instead of a boolean.

Custom type converters (or similar) would be a separate feature request.

glen-84 avatar Oct 11 '23 15:10 glen-84

If anything, we could extend the new CustomValueFormatter to include a TryFormatDefault which can be called when formatting {simple} values without a format or style.

Will close this issue for now. Thanks!

jeffijoe avatar Oct 11 '23 16:10 jeffijoe