This is an automated email from the ASF dual-hosted git repository. shuai pushed a commit to branch locales-nav in repository https://gitbox.apache.org/repos/asf/answer-website.git
commit bd6ee2535628fa6213d6c6812c9070fac3bb68a1 Author: shuai <[email protected]> AuthorDate: Mon Jan 6 14:08:18 2025 +0800 fix: navbar support locales nav --- docusaurus.config.js | 6 ++++++ src/theme/NavbarItem/NavbarNavLink.tsx | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docusaurus.config.js b/docusaurus.config.js index 7e4709111..9dec0d5a0 100644 --- a/docusaurus.config.js +++ b/docusaurus.config.js @@ -109,6 +109,9 @@ const config = { { label: 'Plugins', to: '/plugins', + tos: { + 'zh-CN': 'https://test.com' + } }, { label: 'Blog', @@ -130,6 +133,9 @@ const config = { { label: 'Q&A Community', to: 'https://meta.answer.dev', + tos: { + 'zh-CN': 'https://baidu.com', + } }, { label: 'Team', diff --git a/src/theme/NavbarItem/NavbarNavLink.tsx b/src/theme/NavbarItem/NavbarNavLink.tsx index 8e22a938f..20b08c1be 100644 --- a/src/theme/NavbarItem/NavbarNavLink.tsx +++ b/src/theme/NavbarItem/NavbarNavLink.tsx @@ -12,10 +12,16 @@ import isInternalUrl from '@docusaurus/isInternalUrl'; import {isRegexpStringMatch} from '@docusaurus/theme-common'; import IconExternalLink from '@theme/Icon/ExternalLink'; import type {Props} from '@theme/NavbarItem/NavbarNavLink'; +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; + +interface SProps extends Props { + tos: Record<string, any> +} export default function NavbarNavLink({ activeBasePath, activeBaseRegex, + tos, to, href, label, @@ -23,10 +29,13 @@ export default function NavbarNavLink({ isDropdownLink, prependBaseUrlToHref, ...props -}: Props): JSX.Element { +}: SProps): JSX.Element { // TODO all this seems hacky // {to: 'version'} should probably be forbidden, in favor of {to: '/version'} - const toUrl = useBaseUrl(to); + const { i18n: { currentLocale } } = useDocusaurusContext(); + // If to is a string, we assume it's a path that needs localization + const aliasTo = tos?.[currentLocale] || to; + const toUrl = useBaseUrl(aliasTo); const activeBaseUrl = useBaseUrl(activeBasePath); const normalizedHref = useBaseUrl(href, {forcePrependBaseUrl: true}); const isExternalLink = label && href && !isInternalUrl(href);
