Skip to content

Add support for Unicode MessageFormat 1 #33

Description

@jsahleen

Update the msg library so that it support both MessageFormat 1 and MessageFormat 2.

User Stories

  1. As an msg library user, I want to be able to configure the message formatting version for a project, resource or message, so that I can specify whether a string is MF1, MF2 or NONE.

    • The name of the new property should be called format.
    • Valid options are MF1, MF2 and NONE.
    • The property should default to MF2, for backwards compatibility
    • Resources should inherit from their project, unless a different value is specified.
    • Messages should inherit from their resource, unless a different value is specified.
    • When serializing a project, the property should be omitted if it is the same as its project
    • When serializing a message, the property should be omitted if it is the same as its resource
    • Use TypeScript Union types instead of an enum.
  2. As an msg library user, I want to be able to format MF1 and MF2 strings, so that I can use both in my application.

    • The format method on a message should look at the format property in the attributes of the message.
    • If the format property is MF2, it should format as MF2.
    • If the format property is MF1, it should format using the mf1ToMessage helper from @messageformat/icu-messageformat-1.
    • If the format property is NONE, it should just return the string.

Implementation notes (MF1)

MF1 strings use ICU MessageFormat 1 syntax, which the MF2 MessageFormat constructor cannot parse directly. The @messageformat/icu-messageformat-1 package compiles an MF1 source into an MF2 MessageFormat instance:

import { mf1ToMessage } from '@messageformat/icu-messageformat-1';

// mf1ToMessage(locales, source, options?) => MessageFormat
const mf = mf1ToMessage('en', 'The total is {V, number, ::currency/EUR}.');
mf.format({ V: 4.2 }); // 'The total is €4.20.'

So when format is MF1, MsgMessage.format / formatToParts should build the internal formatter via mf1ToMessage(lang, value, options) instead of new MessageFormat(lang, value, options). (This replaces the earlier, inaccurate icuMessageFormat1.formatters phrasing.)

Note: @messageformat/icu-messageformat-1 depends on messageformat@^4.0.0; the project's messageformat dependency will be aligned to the 4.0.0 stable release so both packages share a single messageformat instance.

Metadata

Metadata

Assignees

Labels

Fields

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions