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

feat(core, theme-classic): allow overriding htmlLang #6371

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/docusaurus-module-type-aliases/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -64,7 +64,14 @@ declare module '@generated/i18n' {
defaultLocale: string;
locales: [string, ...string[]];
currentLocale: string;
localeConfigs: Record<string, {label: string; direction: string}>;
localeConfigs: Record<
string,
{
label: string;
direction: string;
htmlLang: string;
}
>;
};
export = i18n;
}
Original file line number Diff line number Diff line change
@@ -25,23 +25,23 @@ import {useLocation} from '@docusaurus/router';
// See https://github.com/facebook/docusaurus/issues/3317
function AlternateLangHeaders(): JSX.Element {
const {
i18n: {defaultLocale, locales},
i18n: {defaultLocale, localeConfigs},
} = useDocusaurusContext();
const alternatePageUtils = useAlternatePageUtils();

// Note: it is fine to use both "x-default" and "en" to target the same url
// See https://www.searchviu.com/en/multiple-hreflang-tags-one-url/
return (
<Head>
{locales.map((locale) => (
{Object.entries(localeConfigs).map(([locale, {htmlLang}]) => (
<link
key={locale}
rel="alternate"
href={alternatePageUtils.createUrl({
locale,
fullyQualified: true,
})}
hrefLang={locale}
hrefLang={htmlLang}
/>
))}
<link
@@ -91,11 +91,7 @@ export default function LayoutHead(props: Props): JSX.Element {
const {title, description, image, keywords, searchMetadata} = props;
const faviconUrl = useBaseUrl(favicon);
const pageTitle = useTitleFormatter(title);

// See https://github.com/facebook/docusaurus/issues/3317#issuecomment-754661855
// const htmlLang = currentLocale.split('-')[0];
const htmlLang = currentLocale; // should we allow the user to override htmlLang with localeConfig?
const htmlDir = localeConfigs[currentLocale].direction;
const {htmlLang, direction: htmlDir} = localeConfigs[currentLocale];

return (
<>
1 change: 1 addition & 0 deletions packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -120,6 +120,7 @@ export type TranslationFiles = TranslationFile[];

export type I18nLocaleConfig = {
label: string;
htmlLang: string;
direction: string;
};

11 changes: 10 additions & 1 deletion packages/docusaurus/src/server/__tests__/i18n.test.ts
Original file line number Diff line number Diff line change
@@ -40,38 +40,47 @@ describe('defaultLocaleConfig', () => {
expect(getDefaultLocaleConfig('fr')).toEqual({
label: canComputeLabel ? 'Français' : 'fr',
direction: 'ltr',
htmlLang: 'fr',
});
expect(getDefaultLocaleConfig('fr-FR')).toEqual({
label: canComputeLabel ? 'Français (France)' : 'fr-FR',
direction: 'ltr',
htmlLang: 'fr-FR',
});
expect(getDefaultLocaleConfig('en')).toEqual({
label: canComputeLabel ? 'English' : 'en',
direction: 'ltr',
htmlLang: 'en',
});
expect(getDefaultLocaleConfig('en-US')).toEqual({
label: canComputeLabel ? 'American English' : 'en-US',
direction: 'ltr',
htmlLang: 'en-US',
});
expect(getDefaultLocaleConfig('zh')).toEqual({
label: canComputeLabel ? '中文' : 'zh',
direction: 'ltr',
htmlLang: 'zh',
});
expect(getDefaultLocaleConfig('zh-CN')).toEqual({
label: canComputeLabel ? '中文(中国)' : 'zh-CN',
direction: 'ltr',
htmlLang: 'zh-CN',
});
expect(getDefaultLocaleConfig('en-US')).toEqual({
label: canComputeLabel ? 'American English' : 'en-US',
direction: 'ltr',
htmlLang: 'en-US',
});
expect(getDefaultLocaleConfig('fa')).toEqual({
label: canComputeLabel ? 'فارسی' : 'fa',
direction: 'rtl',
htmlLang: 'fa',
});
expect(getDefaultLocaleConfig('fa-IR')).toEqual({
label: canComputeLabel ? 'فارسی (ایران)' : 'fa-IR',
direction: 'rtl',
htmlLang: 'fa-IR',
});
});
});
@@ -156,7 +165,7 @@ describe('loadI18n', () => {
locales: ['en', 'fr', 'de'],
currentLocale: 'de',
localeConfigs: {
fr: {label: 'Français', direction: 'ltr'},
fr: {label: 'Français', direction: 'ltr', htmlLang: 'fr'},
en: getDefaultLocaleConfig('en'),
de: getDefaultLocaleConfig('de'),
},
1 change: 1 addition & 0 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
@@ -98,6 +98,7 @@ const PresetSchema = Joi.alternatives().try(

const LocaleConfigSchema = Joi.object({
label: Joi.string(),
htmlLang: Joi.string(),
direction: Joi.string().equal('ltr', 'rtl').default('ltr'),
});

1 change: 1 addition & 0 deletions packages/docusaurus/src/server/i18n.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export function getDefaultLocaleConfig(locale: string): I18nLocaleConfig {
return {
label: getDefaultLocaleLabel(locale),
direction: getLangDir(locale),
htmlLang: locale,
};
}