mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-01 19:27:48 +02:00
refactor: fix i18n bug in node v22 (#10129)
This commit is contained in:
parent
e48b7818af
commit
02e38d8ccf
1 changed files with 43 additions and 10 deletions
|
@ -10,10 +10,36 @@ import {getLangDir} from 'rtl-detect';
|
||||||
import type {I18n, DocusaurusConfig, I18nLocaleConfig} from '@docusaurus/types';
|
import type {I18n, DocusaurusConfig, I18nLocaleConfig} from '@docusaurus/types';
|
||||||
import type {LoadContextParams} from './site';
|
import type {LoadContextParams} from './site';
|
||||||
|
|
||||||
|
function inferLanguageDisplayName(locale: string) {
|
||||||
|
const tryLocale = (l: string) => {
|
||||||
|
try {
|
||||||
|
return new Intl.DisplayNames(l, {
|
||||||
|
type: 'language',
|
||||||
|
fallback: 'code',
|
||||||
|
}).of(l)!;
|
||||||
|
} catch (e) {
|
||||||
|
// This is to compensate "of()" that is a bit strict
|
||||||
|
// Looks like starting Node 22, this locale throws: "en-US-u-ca-buddhist"
|
||||||
|
// RangeError: invalid_argument
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const parts = locale.split('-');
|
||||||
|
|
||||||
|
// This is a best effort, we try various locale forms that could give a result
|
||||||
|
return (
|
||||||
|
tryLocale(locale) ??
|
||||||
|
tryLocale(`${parts[0]}-${parts[1]}`) ??
|
||||||
|
tryLocale(parts[0]!)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function getDefaultLocaleLabel(locale: string) {
|
function getDefaultLocaleLabel(locale: string) {
|
||||||
const languageName = new Intl.DisplayNames(locale, {type: 'language'}).of(
|
const languageName = inferLanguageDisplayName(locale);
|
||||||
locale,
|
if (!languageName) {
|
||||||
)!;
|
return locale;
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
languageName.charAt(0).toLocaleUpperCase(locale) + languageName.substring(1)
|
languageName.charAt(0).toLocaleUpperCase(locale) + languageName.substring(1)
|
||||||
);
|
);
|
||||||
|
@ -44,13 +70,20 @@ function getDefaultCalendar(localeStr: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
|
export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
|
||||||
return {
|
try {
|
||||||
label: getDefaultLocaleLabel(locale),
|
return {
|
||||||
direction: getLangDir(locale),
|
label: getDefaultLocaleLabel(locale),
|
||||||
htmlLang: locale,
|
direction: getLangDir(locale),
|
||||||
calendar: getDefaultCalendar(locale),
|
htmlLang: locale,
|
||||||
path: locale,
|
calendar: getDefaultCalendar(locale),
|
||||||
};
|
path: locale,
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(
|
||||||
|
`Docusaurus couldn't get default locale config for ${locale}`,
|
||||||
|
{cause: e},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadI18n(
|
export async function loadI18n(
|
||||||
|
|
Loading…
Add table
Reference in a new issue