Skip to content

formatToParts-like Iterator #41

Closed
Closed
@stasm

Description

@stasm

Is your feature request related to a problem? Please describe.
Rather than format a message to a string, it's sometimes useful to work with an iterable of formatted parts. This is conceptually similar to NumberFormat.prototype.formatToParts and others. This approach allows greater flexibility on the side of the calling code, and opens up opportunities to design tighter integrations with other libraries.

For instance, in the message Hello, {$userName}!, the userName variable could be a UI widget, e.g. a React component. The code integrating the localization into the React app could then call formatToParts which would yield the React component unstringified, ready to be used in render().

Another example: It is {$currentTime} where $currentTime is a Date object formatted as 18:03. If formatToParts yields it unformatted, the calling code can then call DateTimeFormat.prototype.formatToParts on it and add custom markup to achieve effects like 18:03 (hours in bold).

Describe the solution you'd like
The formatToParts iterator would yield parts after all selection logic runs (in MF terms, that's after plural and select chooses one of the supplied variants) and after the variables are resolved to their runtime values, but before variables are resolved to their values, they are formated to strings, and interpolated into the message.

Describe why your solution should shape the standard
It's an API offering a lot of flexibility to its consumers. The regular format API returning a string can be implemented on top of it, too.

Additional context or examples
Fluent is now considering formatToParts in projectfluent/fluent.js#383 and projectfluent/fluent#273. I expect it to be ready by the end of H1. We see it as a great way of allowing interesting use-cases like component interpolation mentioned above, as well as an alternative approach to handle BiDi isolation (see #28) and to support custom transformation functions for text (great for implementing pseudolocalizations).

Metadata

Metadata

Assignees

No one assigned

    Labels

    requirementsIssues related with MF requirements list

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions