mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-08 22:03:01 +02:00
fix(core): fix default i18n calendar used, infer it from locale if possible (#9878)
This commit is contained in:
parent
85d74245f8
commit
6bf21d215c
2 changed files with 27 additions and 4 deletions
|
@ -81,7 +81,7 @@ describe('defaultLocaleConfig', () => {
|
||||||
label: 'فارسی',
|
label: 'فارسی',
|
||||||
direction: 'rtl',
|
direction: 'rtl',
|
||||||
htmlLang: 'fa',
|
htmlLang: 'fa',
|
||||||
calendar: 'gregory',
|
calendar: 'persian',
|
||||||
path: 'fa',
|
path: 'fa',
|
||||||
});
|
});
|
||||||
expect(getDefaultLocaleConfig('fa-IR')).toEqual({
|
expect(getDefaultLocaleConfig('fa-IR')).toEqual({
|
||||||
|
@ -89,7 +89,7 @@ describe('defaultLocaleConfig', () => {
|
||||||
label: 'فارسی (ایران)',
|
label: 'فارسی (ایران)',
|
||||||
direction: 'rtl',
|
direction: 'rtl',
|
||||||
htmlLang: 'fa-IR',
|
htmlLang: 'fa-IR',
|
||||||
calendar: 'gregory',
|
calendar: 'persian',
|
||||||
path: 'fa-IR',
|
path: 'fa-IR',
|
||||||
});
|
});
|
||||||
expect(getDefaultLocaleConfig('en-US-u-ca-buddhist')).toEqual({
|
expect(getDefaultLocaleConfig('en-US-u-ca-buddhist')).toEqual({
|
||||||
|
|
|
@ -19,13 +19,36 @@ function getDefaultLocaleLabel(locale: string) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getDefaultCalendar(localeStr: string) {
|
||||||
|
const locale = new Intl.Locale(localeStr);
|
||||||
|
|
||||||
|
// If the locale name includes -u-ca-xxx the calendar will be defined
|
||||||
|
if (locale.calendar) {
|
||||||
|
return locale.calendar;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not well-supported but server code can infer a calendar from the locale
|
||||||
|
// See https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getCalendars
|
||||||
|
// See https://caniuse.com/mdn-javascript_builtins_intl_locale_getcalendars
|
||||||
|
const calendars =
|
||||||
|
// @ts-expect-error: new std method (Bun/JSC/WebKit)
|
||||||
|
locale.getCalendars?.() ??
|
||||||
|
// @ts-expect-error: non-std attribute (V8/Chromium/Node)
|
||||||
|
locale.calendars;
|
||||||
|
|
||||||
|
if (calendars instanceof Array && calendars[0]) {
|
||||||
|
return calendars[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'gregory';
|
||||||
|
}
|
||||||
|
|
||||||
export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
|
export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
|
||||||
return {
|
return {
|
||||||
label: getDefaultLocaleLabel(locale),
|
label: getDefaultLocaleLabel(locale),
|
||||||
direction: getLangDir(locale),
|
direction: getLangDir(locale),
|
||||||
htmlLang: locale,
|
htmlLang: locale,
|
||||||
// If the locale name includes -u-ca-xxx the calendar will be defined
|
calendar: getDefaultCalendar(locale),
|
||||||
calendar: new Intl.Locale(locale).calendar ?? 'gregory',
|
|
||||||
path: locale,
|
path: locale,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue