Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: CI upgrade to Node 22 + TS 5.5 #10119

Merged
merged 11 commits into from
Jun 21, 2024
Prev Previous commit
Next Next commit
fix weird i18n test
  • Loading branch information
slorber committed May 9, 2024
commit 3a4e9f5f6d44d48491f37c5c6b121201eedeb76d
53 changes: 43 additions & 10 deletions packages/docusaurus/src/server/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,36 @@ import {getLangDir} from 'rtl-detect';
import type {I18n, DocusaurusConfig, I18nLocaleConfig} from '@docusaurus/types';
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) {
const languageName = new Intl.DisplayNames(locale, {type: 'language'}).of(
locale,
)!;
const languageName = inferLanguageDisplayName(locale);
if (!languageName) {
return locale;
}
return (
languageName.charAt(0).toLocaleUpperCase(locale) + languageName.substring(1)
);
Expand Down Expand Up @@ -44,13 +70,20 @@ function getDefaultCalendar(localeStr: string) {
}

export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
return {
label: getDefaultLocaleLabel(locale),
direction: getLangDir(locale),
htmlLang: locale,
calendar: getDefaultCalendar(locale),
path: locale,
};
try {
return {
label: getDefaultLocaleLabel(locale),
direction: getLangDir(locale),
htmlLang: 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(
Expand Down
Loading