Skip to content

Commit 77b6369

Browse files
feat(mf2)!: Remove style=percent from :number and :integer (unicode-org/message-format-wg#988)
1 parent b4ae017 commit 77b6369

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

mf2/fluent/src/fluent-to-message.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ function asExpression(exp: Fluent.Expression): Expression {
149149
});
150150
}
151151
}
152+
if (annotation.name === 'number') {
153+
const style = annotation.options?.get('style');
154+
if (style?.type === 'literal') {
155+
switch (style.value) {
156+
case 'decimal':
157+
break;
158+
case 'currency':
159+
case 'unit':
160+
annotation.name = style.value;
161+
break;
162+
default:
163+
throw new Error(
164+
`Unsupported NUMBER(..., style=${JSON.stringify(style.value)})`
165+
);
166+
}
167+
annotation.options!.delete('style');
168+
}
169+
}
152170
return args.length > 0
153171
? { type: 'expression', arg: args[0], functionRef: annotation }
154172
: { type: 'expression', functionRef: annotation };

mf2/fluent/src/fluent.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ const testCases: Record<string, TestCase> = {
8181
num-bare = { NUMBER($arg) }
8282
num-fraction-valid = { NUMBER($arg, minimumFractionDigits: 1) }
8383
num-fraction-bad = { NUMBER($arg, minimumFractionDigits: "oops") }
84-
num-style = { NUMBER($arg, style: "percent") }
8584
num-currency = { NUMBER($arg, style: "currency", currency: "EUR") }
85+
# TODO: num-percent = { NUMBER($arg, style: "percent") }
86+
# TODO: num-unit = { NUMBER($arg, style: "unit", unit: "meter") }
8687
num-unknown = { NUMBER($arg, unknown: "unknown") }
8788
`,
8889
tests: [
@@ -94,8 +95,9 @@ const testCases: Record<string, TestCase> = {
9495
exp: '1,234',
9596
errors: ['bad-option']
9697
},
97-
{ msg: 'num-style', scope: { arg: 1234 }, exp: '123,400%' },
9898
{ msg: 'num-currency', scope: { arg: 1234 }, exp: '€1,234.00' },
99+
// TODO: { msg: 'num-percent', scope: { arg: 1234 }, exp: '123,400%' },
100+
// TODO: { msg: 'num-unit', scope: { arg: 1234 }, exp: '1,234 m' },
99101
{ msg: 'num-unknown', scope: { arg: 1234 }, exp: '1,234' }
100102
]
101103
},

mf2/fluent/src/message-to-fluent.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ function functionRefToFluent(
200200
return new Fluent.FunctionReference(new Fluent.Identifier(id), args);
201201
}
202202

203+
if (
204+
(name === 'currency' || name === 'unit') &&
205+
typeof ctx.functionMap.number === 'string'
206+
) {
207+
args.named.unshift(
208+
new Fluent.NamedArgument(
209+
new Fluent.Identifier('style'),
210+
new Fluent.StringLiteral(name)
211+
)
212+
);
213+
return new Fluent.FunctionReference(
214+
new Fluent.Identifier(ctx.functionMap.number),
215+
args
216+
);
217+
}
218+
203219
if (id === FluentMessageRef) {
204220
const lit = args.positional[0];
205221
if (!(lit instanceof Fluent.BaseLiteral)) {

mf2/messageformat/src/functions/number.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export function number(
9393
try {
9494
switch (name) {
9595
case 'locale':
96+
case 'style': // https://github.com/unicode-org/message-format-wg/pull/988
9697
case 'type': // used internally by Intl.PluralRules, but called 'select' here
9798
break;
9899
case 'minimumIntegerDigits':

0 commit comments

Comments
 (0)