Skip to content

Commit

Permalink
preserve language tags
Browse files Browse the repository at this point in the history
  • Loading branch information
devsnek committed Dec 30, 2022
1 parent acc4952 commit 046998a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/impl/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,26 @@ function parseLocaleString(localeStr) {
return [localeStr];
} else {
let options;
let selectedStr;
const smaller = localeStr.substring(0, uIndex);
try {
options = getCachedDTF(localeStr).resolvedOptions();
selectedStr = localeStr;
} catch (e) {
options = getCachedDTF(smaller).resolvedOptions();
selectedStr = smaller;
}

const { numberingSystem, calendar } = options;
// return the smaller one so that we can append the calendar and numbering overrides to it
return [smaller, numberingSystem, calendar];
return [selectedStr, numberingSystem, calendar];
}
}

function intlConfigString(localeStr, numberingSystem, outputCalendar) {
if (outputCalendar || numberingSystem) {
localeStr += "-u";
if (!localeStr.includes("-u-")) {
localeStr += "-u";
}

if (outputCalendar) {
localeStr += `-ca-${outputCalendar}`;
Expand Down
6 changes: 3 additions & 3 deletions test/datetime/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ test("DateTime.fromObject accepts a locale", () => {

test("DateTime.fromObject accepts a locale with calendar and numbering identifiers", () => {
const res = DateTime.fromObject({}, { locale: "be-u-ca-coptic-nu-mong" });
expect(res.locale).toBe("be");
expect(res.locale).toBe("be-u-ca-coptic-nu-mong");
expect(res.outputCalendar).toBe("coptic");
expect(res.numberingSystem).toBe("mong");
});
Expand All @@ -677,7 +677,7 @@ test("DateTime.fromObject accepts a locale string with weird junk in it", () =>
}
);

expect(res.locale).toBe("be");
expect(res.locale).toBe("be-u-ca-coptic-ca-islamic");

// "coptic" is right, but some versions of Node 10 give "gregory"
expect(res.outputCalendar === "gregory" || res.outputCalendar === "coptic").toBe(true);
Expand All @@ -695,7 +695,7 @@ test("DateTime.fromObject overrides the locale string with explicit settings", (
}
);

expect(res.locale).toBe("be");
expect(res.locale).toBe("be-u-ca-coptic-nu-mong");
expect(res.outputCalendar).toBe("islamic");
expect(res.numberingSystem).toBe("thai");
});
Expand Down
6 changes: 6 additions & 0 deletions test/datetime/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ test("DateTime#toLocaleString uses locale-appropriate time formats", () => {
expect(dt.reconfigure({ locale: "es" }).toLocaleString(DateTime.TIME_24_SIMPLE)).toBe("9:23");
});

test("DateTime#toLocaleString() respects language tags", () => {
expect(dt.reconfigure({ locale: "en-US-u-hc-h23" }).toLocaleString(DateTime.TIME_SIMPLE)).toBe(
"09:23"
);
});

test("DateTime#toLocaleString() accepts a zone even when the zone is set", () => {
expect(
dt.toLocaleString({
Expand Down

0 comments on commit 046998a

Please sign in to comment.