Skip to content

Commit 659d8a7

Browse files
committed
Date region fallback support
1 parent 96fdf72 commit 659d8a7

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

news/changelog-1.3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Improve the performance of extremely large documents with margin elements by improving the efficiency of positioning the elements.
66

7+
## Dates
8+
9+
- Properly fall back to language only locale when a supported language-region locale isn't available. (#3059)
10+
711
## About Pages
812

913
- Add support for `image-alt` which provides alternate text for the about page image. (#3010)

src/core/date.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,37 @@ export function initDayJsPlugins() {
135135
dayjs.extend(advancedPlugin);
136136
}
137137

138-
export async function setDateLocale(locale: string) {
139-
locale = locale.toLowerCase();
140-
if (locale !== dayjs.locale()) {
141-
const localePath = resourcePath(
142-
`library/dayjs/locale/${locale}.js`,
143-
);
144-
if (existsSync(localePath)) {
145-
const localeUrl = toFileUrl(localePath).href;
138+
export async function setDateLocale(localeStr: string) {
139+
localeStr = localeStr.toLowerCase();
140+
if (localeStr !== dayjs.locale()) {
141+
// Try to find the language + region (e.g. fr-CA) first
142+
// but fall back to just the language (e.g. fr)
143+
const findLocale = () => {
144+
const locales = [localeStr];
145+
if (localeStr.includes("-")) {
146+
locales.push(localeStr.split("-")[0]);
147+
}
148+
149+
for (const locale of locales) {
150+
const path = resourcePath(
151+
`library/dayjs/locale/${locale}.js`,
152+
);
153+
if (existsSync(path)) {
154+
return {
155+
locale,
156+
path,
157+
};
158+
}
159+
}
160+
return undefined;
161+
};
162+
163+
const locale = findLocale();
164+
if (locale) {
165+
const localeUrl = toFileUrl(locale.path).href;
146166
const localeModule = await import(localeUrl);
147167
dayjs.locale(localeModule.default, null, true);
148-
dayjs.locale(locale);
168+
dayjs.locale(locale.locale);
149169
}
150170
}
151171
}

tests/docs/date/fr-CA.qmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: This is a Test
3+
date: 2022/10/1
4+
lang: fr-CA
5+
---
6+
7+
## Hello Date!

tests/docs/date/fr-FR.qmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: This is a Test
3+
date: 2022/10/1
4+
lang: fr-FR
5+
---
6+
7+
## Hello Date!

tests/docs/date/fr.qmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: This is a Test
3+
date: 2022/10/1
4+
lang: fr
5+
---
6+
7+
## Hello Date!

tests/smoke/render/render-date.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ import { testRender } from "./render.ts";
1111
const tests = [
1212
{ input: docs("date/today.qmd"), noMatch: />today</ },
1313
{ input: docs("date/lastmodified.qmd"), noMatch: />last-modified</ },
14+
{ input: docs("date/fr.qmd"), match: /octobre/, noMatch: /October/ },
15+
{ input: docs("date/fr-FR.qmd"), match: /octobre/, noMatch: /October/ },
16+
{ input: docs("date/fr-CA.qmd"), match: /octobre/, noMatch: /October/ },
1417
];
1518

1619
tests.forEach((test) => {
1720
const to = "html";
1821
const output = outputForInput(test.input, "html");
22+
23+
const noMatch = test.noMatch ? [test.noMatch] : [];
24+
const match = test.match ? [test.match] : [];
25+
1926
testRender(test.input, to, false, [
20-
ensureFileRegexMatches(output.outputPath, [], [
21-
test.noMatch,
22-
]),
27+
ensureFileRegexMatches(output.outputPath, match, noMatch),
2328
]);
2429
});

0 commit comments

Comments
 (0)