Conversation
|
@FSchubert92 Would you be able to verify that this would solve your use case? |
Why Also, it could be useful to distinguish literal parts of the message vs variables... One approach is using an array of parts such as the one defined in Ecma-402 (ref). For example: mf.compile('select {num, plural, one{one} other{#}}')({ num: 42 })
// [{ type: "literal", vaue: "select " }, { type: "variable", value: 42 }] |
|
Another comment: mf.compile('select {num, plural, one{one} other{#}}')({ num: 42 })
// "select 42"
mf.compileParts('select {num, plural, one{one} other{#}}')({ num: 42 })
// [{ type: "literal", vaue: "select " }, { type: "variable", value: 42 }] |
|
Nested arrays: The runtime code is significantly simpler if the array isn't flattened for select cases. Now, admittedly for the given example the compiler could (and possibly should) skip the [wrapper], but it would still need to apply it for plural or select cases with a compound value. Also, as far as I can tell at least Vue and React don't really mind traversing a nested array.
Btw, just to be clear, I'm happy to continue this conversation and even happier if the shape of this PR improves as a result. My starting point here is to find a decent way to allow variables to pass through without stringification, but if we get to solve other problems along the way, that's even better. |
|
@eemeli Yes, this option should fit our use case! |
|
My 0.02 cents:
I would say this could be easily handled as a further iteration and given the limited context of this change (allowing vue-i18n interpolation with a custom formatter) this PR is definitely good as it is for now |
|
I just wanted to quickly ask whether there are some news on this issue? |
|
This looks likely to become a part of the major release, of which I could probably package a beta/RC release within a week or so. Mostly because it'd be a bit silly to publish a new method just before removing such methods. I'm still considering the exact API, to allow for the parts variant suggested by @rxaviers as a later addition without any breaking changes. Maybe something like this? const mf = new MessageFormat('en', { returnType: 'values' })Alternative naming suggestions are rather welcome. The default value of the option would almost certainly be |
|
Rebased on current |
|
thanks for adding this, I see it's in the beta branch. seems the typings for typescript was missed with regards to this option |
Use the new `messageformat` interface[1] that changes the data type from compiled messageformat functions to arrays, instead of the old behavior that converted everything to strings. The old behaviour of `messageformat` could not be used without some string manipulation hack to make component interpolation to work. This also takes into consideration the simplified interpolation, slot-based interpolation, and the _deprecated_ `place`/`places` interpolation styles. **Other changes** * Update dependencies; there was a bug in the vue-i18n version that prevented slot-based interpolation to correctly pass `values` to custom formatters. * Update all vue-related dependencies to remove compromised packages reported by `npm audit` [1] messageformat/messageformat#242
Use the new `messageformat` interface[1] that changes the data type from compiled messageformat functions to arrays, instead of the old behavior that converted everything to strings. The old behaviour of `messageformat` could not be used without some string manipulation hack to make component interpolation to work. This also takes into consideration the simplified interpolation, slot-based interpolation, and the _deprecated_ `place`/`places` interpolation styles. **Other changes** * Update dependencies; there was a bug in the vue-i18n version that prevented slot-based interpolation to correctly pass `values` to custom formatters. * Update all vue-related dependencies to remove compromised packages reported by `npm audit` [1] messageformat/messageformat#242
Use the new `messageformat` interface[1] that changes the data type from compiled messageformat functions to arrays, instead of the old behavior that converted everything to strings. The old behaviour of `messageformat` could not be used without some string manipulation hack to make component interpolation to work. This also takes into consideration the simplified interpolation, slot-based interpolation, and the _deprecated_ `place`/`places` interpolation styles. **Other changes** * Update dependencies; there was a bug in the vue-i18n version that prevented slot-based interpolation to correctly pass `values` to custom formatters. * Update all vue-related dependencies to remove compromised packages reported by `npm audit` [1] messageformat/messageformat#242
* feat(examples/formatting): update custom formatter example Use the new `messageformat` interface[1] that changes the data type from compiled messageformat functions to arrays, instead of the old behavior that converted everything to strings. The old behaviour of `messageformat` could not be used without some string manipulation hack to make component interpolation to work. This also takes into consideration the simplified interpolation, slot-based interpolation, and the _deprecated_ `place`/`places` interpolation styles. **Other changes** * Update dependencies; there was a bug in the vue-i18n version that prevented slot-based interpolation to correctly pass `values` to custom formatters. * Update all vue-related dependencies to remove compromised packages reported by `npm audit` [1] messageformat/messageformat#242 * fmt(examples/formatting): remove unused function param from custom formatter
* feat(examples/formatting): update custom formatter example Use the new `messageformat` interface[1] that changes the data type from compiled messageformat functions to arrays, instead of the old behavior that converted everything to strings. The old behaviour of `messageformat` could not be used without some string manipulation hack to make component interpolation to work. This also takes into consideration the simplified interpolation, slot-based interpolation, and the _deprecated_ `place`/`places` interpolation styles. **Other changes** * Update dependencies; there was a bug in the vue-i18n version that prevented slot-based interpolation to correctly pass `values` to custom formatters. * Update all vue-related dependencies to remove compromised packages reported by `npm audit` [1] messageformat/messageformat#242 * fmt(examples/formatting): remove unused function param from custom formatter
* feat(examples/formatting): update custom formatter example Use the new `messageformat` interface[1] that changes the data type from compiled messageformat functions to arrays, instead of the old behavior that converted everything to strings. The old behaviour of `messageformat` could not be used without some string manipulation hack to make component interpolation to work. This also takes into consideration the simplified interpolation, slot-based interpolation, and the _deprecated_ `place`/`places` interpolation styles. **Other changes** * Update dependencies; there was a bug in the vue-i18n version that prevented slot-based interpolation to correctly pass `values` to custom formatters. * Update all vue-related dependencies to remove compromised packages reported by `npm audit` [1] messageformat/messageformat#242 * fmt(examples/formatting): remove unused function param from custom formatter
* feat(examples/formatting): update custom formatter example Use the new `messageformat` interface[1] that changes the data type from compiled messageformat functions to arrays, instead of the old behavior that converted everything to strings. The old behaviour of `messageformat` could not be used without some string manipulation hack to make component interpolation to work. This also takes into consideration the simplified interpolation, slot-based interpolation, and the _deprecated_ `place`/`places` interpolation styles. **Other changes** * Update dependencies; there was a bug in the vue-i18n version that prevented slot-based interpolation to correctly pass `values` to custom formatters. * Update all vue-related dependencies to remove compromised packages reported by `npm audit` [1] messageformat/messageformat#242 * fmt(examples/formatting): remove unused function param from custom formatter
Fixes #241
This adds a new method
MessageFormat#setArrayOutput(), which makes compiled functions return arrays instead of strings, allowing for variable values to not be stringified. Some examples: