Skip to content

Commit 70a20dd

Browse files
feat(mf2): Support u:locale option (unicode-org/message-format-wg#846)
1 parent 93ab099 commit 70a20dd

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

mf2/messageformat/src/data-model/format-markup.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { MessageResolutionError } from '../errors.js';
12
import type { Context } from '../format-context.js';
23
import type { MessageMarkupPart } from '../formatted-parts.js';
3-
import { resolveValue } from './resolve-value.js';
4+
import { getValueSource, resolveValue } from './resolve-value.js';
45
import type { Markup } from './types.js';
56

67
export function formatMarkup(
@@ -13,12 +14,17 @@ export function formatMarkup(
1314
if (options?.size) {
1415
part.options = {};
1516
for (const [name, value] of options) {
16-
let rv = resolveValue(ctx, value);
17-
if (typeof rv === 'object' && typeof rv?.valueOf === 'function') {
18-
const vv = rv.valueOf();
19-
if (vv !== rv) rv = vv;
17+
if (name === 'u:locale') {
18+
const msg = `The option ${name} is not valid for markup`;
19+
const optSource = getValueSource(value);
20+
ctx.onError(new MessageResolutionError('bad-option', msg, optSource));
21+
} else {
22+
let rv = resolveValue(ctx, value);
23+
if (typeof rv === 'object' && typeof rv?.valueOf === 'function') {
24+
rv = rv.valueOf();
25+
}
26+
part.options[name] = rv;
2027
}
21-
part.options[name] = rv;
2228
}
2329
}
2430
return part;

mf2/messageformat/src/data-model/function-context.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
import type { Context } from '../format-context.js';
2+
import { resolveValue } from './resolve-value.js';
3+
import { Options } from './types.js';
24

35
export class MessageFunctionContext {
46
#ctx: Context;
7+
#locales: string[];
58
readonly source: string;
6-
constructor(ctx: Context, source: string) {
9+
constructor(ctx: Context, source: string, options: Options | undefined) {
710
this.#ctx = ctx;
11+
const lc = options?.get('u:locale');
12+
if (lc) {
13+
let rl = resolveValue(ctx, lc);
14+
if (typeof rl === 'object' && typeof rl?.valueOf === 'function') {
15+
rl = rl.valueOf();
16+
}
17+
this.#locales = Array.isArray(rl) ? rl.map(String) : [String(rl)];
18+
} else {
19+
this.#locales = ctx.locales;
20+
}
821
this.source = source;
922
}
1023
get localeMatcher() {
1124
return this.#ctx.localeMatcher;
1225
}
1326
get locales() {
14-
return this.#ctx.locales.slice();
27+
return this.#locales.slice();
1528
}
1629
get onError() {
1730
return this.#ctx.onError;

mf2/messageformat/src/data-model/resolve-function-ref.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function resolveFunctionRef(
2525
if (!rf) {
2626
throw new MessageError('unknown-function', `Unknown function :${name}`);
2727
}
28-
const msgCtx = new MessageFunctionContext(ctx, source);
28+
const msgCtx = new MessageFunctionContext(ctx, source, options);
2929
const opt = resolveOptions(ctx, options);
3030
const res = rf(msgCtx, opt, ...fnInput);
3131
if (
@@ -51,7 +51,9 @@ function resolveOptions(ctx: Context, options: Options | undefined) {
5151
const opt: Record<string, unknown> = Object.create(null);
5252
if (options) {
5353
for (const [name, value] of options) {
54-
opt[name] = resolveValue(ctx, value);
54+
if (name !== 'u:locale') {
55+
opt[name] = resolveValue(ctx, value);
56+
}
5557
}
5658
}
5759
return opt;

mf2/messageformat/src/data-model/resolve-value.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export function resolveValue(
1919
}
2020

2121
/** @internal */
22+
export function getValueSource(value: Literal | VariableRef): string;
23+
export function getValueSource(
24+
value: Literal | VariableRef | undefined
25+
): string | undefined;
2226
export function getValueSource(value: Literal | VariableRef | undefined) {
2327
switch (value?.type) {
2428
case 'literal':

0 commit comments

Comments
 (0)