Skip to content

Commit

Permalink
feat(theme): add support for meta og locale and alternates (facebook#…
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorinaPacurar authored and slorber committed Aug 3, 2023
1 parent 27d694f commit a6d0bdc
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ 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 bcp47ToOpenGraphLocale = (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 (
Expand All @@ -52,6 +61,20 @@ function AlternateLangHeaders(): JSX.Element {
})}
hrefLang="x-default"
/>

<meta
property="og:locale"
content={bcp47ToOpenGraphLocale(currentHtmlLang)}
/>
{Object.values(localeConfigs)
.filter((config) => currentHtmlLang !== config.htmlLang)
.map((config) => (
<meta
key={`meta-og-${config.htmlLang}`}
property="og:locale:alternate"
content={bcp47ToOpenGraphLocale(config.htmlLang)}
/>
))}
</Head>
);
}
Expand Down

0 comments on commit a6d0bdc

Please sign in to comment.