Skip to content

Commit 1211464

Browse files
refactor(mf2): Drop locale from MessageValue as unnecessary
1 parent 9a0f3de commit 1211464

File tree

8 files changed

+14
-31
lines changed

8 files changed

+14
-31
lines changed

mf2/fluent/src/functions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export function getFluentFunctions(res: FluentMessageResource) {
3838
type: 'fluent-message' as const,
3939
source,
4040
dir,
41-
locale,
4241
selectKey(keys) {
4342
str ??= mf.format(options, onError);
4443
return keys.has(str) ? str : null;

mf2/icu-messageformat-1/src/functions.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function time(
6060
* trailing `.sss` part for non-integer input
6161
*/
6262
function duration(
63-
{ locales, source }: MessageFunctionContext,
63+
{ source }: MessageFunctionContext,
6464
_options: unknown,
6565
input?: unknown
6666
) {
@@ -96,7 +96,6 @@ function duration(
9696

9797
return {
9898
type: 'mf1-duration' as const,
99-
locale: locales[0],
10099
source,
101100
toParts() {
102101
const res = { type: 'mf1-duration' as const, source, value: str };
@@ -148,12 +147,12 @@ function number(
148147
type: 'number',
149148
source,
150149
get dir() {
151-
dir ??= getLocaleDir(this.locale);
150+
if (dir == null) {
151+
locale ??= Intl.NumberFormat.supportedLocalesOf(locales, opt)[0];
152+
dir = getLocaleDir(locale);
153+
}
152154
return dir;
153155
},
154-
get locale() {
155-
return (locale ??= Intl.NumberFormat.supportedLocalesOf(locales, opt)[0]);
156-
},
157156
get options() {
158157
return { ...opt };
159158
},

mf2/messageformat/src/functions/datetime.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export interface MessageDateTime extends MessageValue {
1010
readonly type: 'datetime';
1111
readonly source: string;
1212
readonly dir: 'ltr' | 'rtl' | 'auto';
13-
readonly locale: string;
1413
readonly options: Readonly<Intl.DateTimeFormatOptions>;
1514
toParts(): [MessageDateTimePart];
1615
toString(): string;
@@ -163,15 +162,12 @@ function dateTimeImplementation(
163162
type: 'datetime',
164163
source,
165164
get dir() {
166-
dir ??= getLocaleDir(this.locale);
165+
if (dir == null) {
166+
locale ??= Intl.DateTimeFormat.supportedLocalesOf(locales, opt)[0];
167+
dir = getLocaleDir(locale);
168+
}
167169
return dir;
168170
},
169-
get locale() {
170-
return (locale ??= Intl.DateTimeFormat.supportedLocalesOf(
171-
locales,
172-
opt
173-
)[0]);
174-
},
175171
get options() {
176172
return { ...opt };
177173
},

mf2/messageformat/src/functions/fallback.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { MessageValue } from '../message-value.js';
99
export interface MessageFallback extends MessageValue {
1010
readonly type: 'fallback';
1111
readonly source: string;
12-
readonly locale: 'und';
1312
toParts(): [MessageFallbackPart];
1413
toString(): string;
1514
}
@@ -22,7 +21,6 @@ export interface MessageFallbackPart extends MessageExpressionPart {
2221

2322
export const fallback = (source: string = '�'): MessageFallback => ({
2423
type: 'fallback',
25-
locale: 'und',
2624
source,
2725
toParts: () => [{ type: 'fallback', source }],
2826
toString: () => `{${source}}`

mf2/messageformat/src/functions/number.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export interface MessageNumber extends MessageValue {
1010
readonly type: 'number';
1111
readonly source: string;
1212
readonly dir: 'ltr' | 'rtl' | 'auto';
13-
readonly locale: string;
1413
readonly options: Readonly<
1514
Intl.NumberFormatOptions & Intl.PluralRulesOptions
1615
>;
@@ -135,15 +134,12 @@ export function number(
135134
type: 'number',
136135
source,
137136
get dir() {
138-
dir ??= getLocaleDir(this.locale);
137+
if (dir == null) {
138+
locale ??= Intl.NumberFormat.supportedLocalesOf(locales, options)[0];
139+
dir = getLocaleDir(locale);
140+
}
139141
return dir;
140142
},
141-
get locale() {
142-
return (locale ??= Intl.NumberFormat.supportedLocalesOf(
143-
locales,
144-
options
145-
)[0]);
146-
},
147143
get options() {
148144
return { ...options };
149145
},

mf2/messageformat/src/functions/string.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export interface MessageString extends MessageValue {
77
readonly type: 'string';
88
readonly source: string;
99
readonly dir: 'ltr' | 'rtl' | 'auto';
10-
readonly locale: string;
1110
selectKey(keys: Set<string>): string | null;
1211
toParts(): [MessageStringPart];
1312
toString(): string;
@@ -35,15 +34,14 @@ export function string(
3534
): MessageString {
3635
const str = input === undefined ? '' : String(input);
3736
const selStr = str.normalize();
38-
const locale = ctx.locales[0];
3937
return {
4038
type: 'string',
4139
source: ctx.source,
4240
dir: ctx.dir ?? 'auto',
43-
locale,
4441
selectKey: keys => (keys.has(selStr) ? selStr : null),
4542
toParts() {
4643
const { dir, source } = ctx;
44+
const locale = ctx.locales[0];
4745
return dir === 'ltr' || dir === 'rtl'
4846
? [{ type: 'string', source, dir, locale, value: str }]
4947
: [{ type: 'string', source, locale, value: str }];

mf2/messageformat/src/functions/unknown.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export interface MessageUnknownValue extends MessageValue {
66
readonly type: 'unknown';
77
readonly source: string;
88
readonly dir: 'auto';
9-
readonly locale: 'und';
109
toParts(): [MessageUnknownPart];
1110
toString(): string;
1211
valueOf(): unknown;
@@ -27,7 +26,6 @@ export const unknown = (
2726
type: 'unknown',
2827
source,
2928
dir: 'auto',
30-
locale: 'und',
3129
toParts: () => [{ type: 'unknown', source, value: input }],
3230
toString: () => String(input),
3331
valueOf: () => input

mf2/messageformat/src/message-value.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const BIDI_ISOLATE = Symbol('bidi-isolate');
55
export interface MessageValue {
66
readonly type: string;
77
readonly source: string;
8-
readonly locale: string;
98
readonly dir?: 'ltr' | 'rtl' | 'auto';
109
readonly options?: Readonly<object>;
1110
selectKey?: (keys: Set<string>) => string | null;

0 commit comments

Comments
 (0)