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

[API CHANGE] Add optional graceful handling of missing variables

Open Frooxius opened this issue 2 years ago • 1 comments

In application I am working on, we have a lot of user generated content which gets updated and changed over time.

This results in some of the configured variables to get out of date. This makes the formatting functions throw lots of exceptions, which litters the logs and it's not quite desirable.

I have added an optional boolean argument, which allows the formatting to quietly ignore missing variables where desired and just assume they're default values, which is more preferable behavior for my use-case.

NOTE: This includes commits from my other two PR's, since I had to do those to be able to compile and run tests.

Frooxius avatar Nov 06 '23 17:11 Frooxius

Hey @Frooxius , thanks for the PR, especially those locale-specific unit test fixes! 🙌

I don't think we should add a new parameter to FormatMessage for this. I do have an idea though. How about we allow passing in a custom FormatterLibrary, and make a change to FormatterLibrary's constructor to accept a allowMissingVariables boolean (false by default), as well as all the built-in formatters to accept this new configuration option? We would need test cases that cover the new behavior, of course.

What do you think?

jeffijoe avatar Nov 06 '23 18:11 jeffijoe