mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-02 02:42:41 +02:00
fix(v2): fix/enhance minor i18n issues reported (#4092)
* fix comment * allow to pass custom classname in navbar items * Add IconLanguage comp to dropdown * do not trim htmlLang * Add initial hreflang SEO support * doc hreflang
This commit is contained in:
parent
8a934ac9b7
commit
869ebe7b53
12 changed files with 158 additions and 32 deletions
|
@ -16,6 +16,8 @@ export {
|
|||
FooterLinkItem,
|
||||
} from './utils/useThemeConfig';
|
||||
|
||||
export {useAlternatePageUtils} from './utils/useAlternatePageUtils';
|
||||
|
||||
export {docVersionSearchTag, DEFAULT_SEARCH_TAG} from './utils/searchUtils';
|
||||
|
||||
export {isDocsPluginEnabled} from './utils/docsUtils';
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
import {useLocation} from '@docusaurus/router';
|
||||
|
||||
// Permits to obtain the url of the current page in another locale
|
||||
// Useful to generate hreflang meta headers etc...
|
||||
// See https://developers.google.com/search/docs/advanced/crawling/localized-versions
|
||||
export function useAlternatePageUtils() {
|
||||
const {
|
||||
siteConfig: {baseUrl, url},
|
||||
i18n: {defaultLocale, currentLocale},
|
||||
} = useDocusaurusContext();
|
||||
const {pathname} = useLocation();
|
||||
|
||||
const baseUrlUnlocalized =
|
||||
currentLocale === defaultLocale
|
||||
? baseUrl
|
||||
: baseUrl.replace(`/${currentLocale}/`, '/');
|
||||
|
||||
const pathnameSuffix = pathname.replace(baseUrl, '');
|
||||
|
||||
function getLocalizedBaseUrl(locale: string) {
|
||||
return locale === defaultLocale
|
||||
? `${baseUrlUnlocalized}`
|
||||
: `${baseUrlUnlocalized}${locale}/`;
|
||||
}
|
||||
|
||||
// TODO support correct alternate url when localized site is deployed on another domain
|
||||
function createUrl({
|
||||
locale,
|
||||
fullyQualified,
|
||||
}: {
|
||||
locale: string;
|
||||
// For hreflang SEO headers, we need it to be fully qualified (full protocol/domain/path...)
|
||||
// For locale dropdown, using a path is good enough
|
||||
fullyQualified: boolean;
|
||||
}) {
|
||||
return `${fullyQualified ? url : ''}${getLocalizedBaseUrl(
|
||||
locale,
|
||||
)}${pathnameSuffix}`;
|
||||
}
|
||||
|
||||
return {createUrl};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue