Skip to content

Commit 5aba675

Browse files
committed
Refactor localized list collation
1 parent b13c25d commit 5aba675

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

app/web/i18n/sorting.test.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
import { getLocalizedListComparer } from "./sorting";
1+
import { getLocaleCollator, getLocalizedListComparer } from "./sorting";
22

33
describe("getLocalizedListComparer", () => {
44
it("uses pinyin collation for zh-Hans when supported", () => {
5-
const resolvedLocale = new Intl.Collator(
6-
"zh-Hans-u-co-pinyin",
7-
).resolvedOptions().locale;
5+
const collator = getLocaleCollator("zh-Hans");
86

9-
if (resolvedLocale.includes("-pinyin")) {
10-
expect(
11-
["中国", "法国", "德国", "阿富汗"].sort(
12-
getLocalizedListComparer("zh-Hans"),
13-
),
14-
).toEqual(["阿富汗", "德国", "法国", "中国"]);
7+
if (collator.resolvedOptions().locale.includes("-pinyin")) {
8+
expect(["中国", "法国", "德国", "阿富汗"].sort(collator.compare)).toEqual(
9+
["阿富汗", "德国", "法国", "中国"],
10+
);
1511
}
1612
});
1713

1814
it("uses stroke collation for zh-Hant when supported", () => {
19-
const resolvedLocale = new Intl.Collator(
20-
"zh-Hant-u-co-stroke",
21-
).resolvedOptions().locale;
15+
const collator = getLocaleCollator("zh-Hant");
2216

23-
if (resolvedLocale.includes("-stroke")) {
24-
expect(
25-
["德國", "法國", "中國", "加拿大"].sort(
26-
getLocalizedListComparer("zh-Hant"),
27-
),
28-
).toEqual(["中國", "加拿大", "法國", "德國"]);
17+
if (collator.resolvedOptions().locale.includes("-stroke")) {
18+
expect(["德國", "法國", "中國", "加拿大"].sort(collator.compare)).toEqual(
19+
["中國", "加拿大", "法國", "德國"],
20+
);
2921
}
3022
});
3123

app/web/i18n/sorting.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@ function getLocalizedListSortLocales(language: string): string[] {
2525
export function getLocalizedListComparer(
2626
language: string,
2727
): Intl.Collator["compare"] {
28-
return new Intl.Collator(getLocalizedListSortLocales(language)).compare;
28+
return getLocaleCollator(language).compare;
29+
}
30+
31+
export function getLocaleCollator(language: string): Intl.Collator {
32+
// Intl.Collator compares strings according to locale-specific sorting rules.
33+
return new Intl.Collator(getLocalizedListSortLocales(language));
2934
}

0 commit comments

Comments
 (0)