Skip to content

Commit 4d53b7b

Browse files
authored
fix: format should follow locale order (#1619)
1 parent 9989963 commit 4d53b7b

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

packages/core/src/formats.test.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,53 @@ import { date, number } from "./formats"
33
describe("@lingui/core/formats", () => {
44
describe("date", () => {
55
it("should support Date as input", () => {
6-
expect(date(["en"], new Date(2023, 2, 5))).toMatchInlineSnapshot(
7-
`"3/5/2023"`
8-
)
6+
expect(date(["en"], new Date(2023, 2, 5))).toBe("3/5/2023")
97
})
108
it("should support iso string as input", () => {
11-
expect(
12-
date(["en"], new Date(2023, 2, 5).toISOString())
13-
).toMatchInlineSnapshot(`"3/5/2023"`)
9+
expect(date(["en"], new Date(2023, 2, 5).toISOString())).toBe("3/5/2023")
1410
})
1511

1612
it("should pass format options", () => {
1713
expect(
1814
date(["en"], new Date(2023, 2, 5).toISOString(), { dateStyle: "full" })
19-
).toMatchInlineSnapshot(`"Sunday, March 5, 2023"`)
15+
).toBe("Sunday, March 5, 2023")
2016

2117
expect(
2218
date(["en"], new Date(2023, 2, 5).toISOString(), {
2319
dateStyle: "medium",
2420
})
25-
).toMatchInlineSnapshot(`"Mar 5, 2023"`)
21+
).toBe("Mar 5, 2023")
2622
})
2723

2824
it("should respect passed locale", () => {
2925
expect(
3026
date(["pl"], new Date(2023, 2, 5).toISOString(), { dateStyle: "full" })
31-
).toMatchInlineSnapshot(`"niedziela, 5 marca 2023"`)
27+
).toBe("niedziela, 5 marca 2023")
3228
})
3329
})
3430

3531
describe("number", () => {
3632
it("should pass format options", () => {
37-
expect(
38-
number(["en"], 1000, { style: "currency", currency: "EUR" })
39-
).toMatchInlineSnapshot(`"€1,000.00"`)
33+
expect(number(["en"], 1000, { style: "currency", currency: "EUR" })).toBe(
34+
"€1,000.00"
35+
)
4036

41-
expect(
42-
number(["en"], 1000, { maximumSignificantDigits: 3 })
43-
).toMatchInlineSnapshot(`"1,000"`)
37+
expect(number(["en"], 1000, { maximumSignificantDigits: 3 })).toBe(
38+
"1,000"
39+
)
4440
})
4541

46-
it("should respect passed locale", () => {
42+
it("should respect passed locale(s)", () => {
43+
expect(number(["pl"], 1000, { style: "currency", currency: "EUR" })).toBe(
44+
"1000,00 €"
45+
)
46+
47+
expect(
48+
number(["pl", "en-US"], 1000, { style: "currency", currency: "EUR" })
49+
).toBe("1000,00 €")
4750
expect(
48-
number(["pl"], 1000, { style: "currency", currency: "EUR" })
49-
).toMatchInlineSnapshot(`"1000,00 €"`)
51+
number(["en-US", "pl"], 1000, { style: "currency", currency: "EUR" })
52+
).toBe("€1,000.00")
5053
})
5154
})
5255
})

packages/core/src/formats.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function normalizeLocales(locales: Locales): string[] {
1212
export function date(
1313
locales: Locales,
1414
value: string | Date,
15-
format: Intl.DateTimeFormatOptions = {}
15+
format?: Intl.DateTimeFormatOptions
1616
): string {
1717
const _locales = normalizeLocales(locales)
1818

@@ -27,7 +27,7 @@ export function date(
2727
export function number(
2828
locales: Locales,
2929
value: number,
30-
format: Intl.NumberFormatOptions = {}
30+
format?: Intl.NumberFormatOptions
3131
): string {
3232
const _locales = normalizeLocales(locales)
3333

@@ -49,11 +49,11 @@ export function plural(
4949

5050
const plurals = ordinal
5151
? getMemoized(
52-
() => cacheKey("plural-ordinal", _locales, {}),
52+
() => cacheKey("plural-ordinal", _locales),
5353
() => new Intl.PluralRules(_locales, { type: "ordinal" })
5454
)
5555
: getMemoized(
56-
() => cacheKey("plural-cardinal", _locales, {}),
56+
() => cacheKey("plural-cardinal", _locales),
5757
() => new Intl.PluralRules(_locales, { type: "cardinal" })
5858
)
5959

@@ -75,9 +75,9 @@ function getMemoized<T>(getKey: () => string, construct: () => T) {
7575

7676
function cacheKey(
7777
type: string,
78-
locales?: readonly string[],
79-
options: unknown = {}
78+
locales: readonly string[],
79+
options?: Intl.DateTimeFormatOptions | Intl.NumberFormatOptions
8080
) {
81-
const localeKey = [...locales].sort().join("-")
81+
const localeKey = locales.join("-")
8282
return `${type}-${localeKey}-${JSON.stringify(options)}`
8383
}

0 commit comments

Comments
 (0)