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(theme): add support for meta og locale and alternates #9152

Merged
merged 3 commits into from
Jul 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,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 @@ -51,6 +60,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
Loading