diff --git a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx index 9a30c775fff2..713067a17120 100644 --- a/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/SiteMetadata/index.tsx @@ -24,10 +24,18 @@ import SearchMetadata from '@theme/SearchMetadata'; // See https://github.com/facebook/docusaurus/issues/3317 function AlternateLangHeaders(): JSX.Element { const { - i18n: {defaultLocale, localeConfigs}, + i18n: {currentLocale, defaultLocale, localeConfigs}, } = useDocusaurusContext(); const alternatePageUtils = useAlternatePageUtils(); + const currentHtmlLang = localeConfigs[currentLocale]?.htmlLang; + + // HTML lang is a BCP 47 tag, but the Open Graph protocol requires + // using underscores instead of dashes. + // See https://ogp.me/#optional + // See https://en.wikipedia.org/wiki/IETF_language_tag) + const bcp47ToLocale = (code: string): string => code.replace('-', '_'); + // 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 ( @@ -51,6 +59,19 @@ function AlternateLangHeaders(): JSX.Element { })} hrefLang="x-default" /> + + {currentHtmlLang && ( + + )} + {Object.values(localeConfigs) + .filter((config) => currentHtmlLang !== config.htmlLang) + .map((config) => ( + + ))} ); } diff --git a/packages/docusaurus/src/client/SiteMetadataDefaults.tsx b/packages/docusaurus/src/client/SiteMetadataDefaults.tsx index 56a412ed181a..1f49959d3536 100644 --- a/packages/docusaurus/src/client/SiteMetadataDefaults.tsx +++ b/packages/docusaurus/src/client/SiteMetadataDefaults.tsx @@ -28,18 +28,6 @@ export default function SiteMetadataDefaults(): JSX.Element { */} {title} - - {Object.values(localeConfigs).map((config) => - htmlLang !== config.htmlLang ? ( - - ) : ( - '' - ), - )} {noIndex && }