forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Site link auto switch by locale (ant-design#42381)
* Revert "Revert "docs: auto trans link" (ant-design#42373)" This reverts commit fa90b25. * docs: site update link auto switch logic
- Loading branch information
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Link } from 'dumi'; | ||
import * as React from 'react'; | ||
import useLocale from '../../../hooks/useLocale'; | ||
|
||
type LinkProps = Parameters<typeof Link>[0]; | ||
|
||
export interface LocaleLinkProps extends LinkProps { | ||
sourceType: 'a' | 'Link'; | ||
children?: React.ReactNode; | ||
} | ||
|
||
export default function LocaleLink({ sourceType, to, ...props }: LocaleLinkProps) { | ||
const Component = sourceType === 'a' ? 'a' : Link; | ||
|
||
const [, localeType] = useLocale(); | ||
|
||
const localeTo = React.useMemo(() => { | ||
if (!to || typeof to !== 'string') { | ||
return to; | ||
} | ||
|
||
// Auto locale switch | ||
const cells = to.match(/(\/[^#]*)(#.*)?/); | ||
if (cells) { | ||
let path = cells[1].replace(/\/$/, ''); | ||
const hash = cells[2] || ''; | ||
|
||
if (localeType === 'cn' && !path.endsWith('-cn')) { | ||
path = `${path}-cn`; | ||
} else if (localeType === 'en' && path.endsWith('-cn')) { | ||
path = path.replace(/-cn$/, ''); | ||
} | ||
|
||
return `${path}${hash}`; | ||
} | ||
|
||
return to; | ||
}, [to]); | ||
|
||
const linkProps: LocaleLinkProps = { | ||
...props, | ||
} as LocaleLinkProps; | ||
|
||
if (to) { | ||
linkProps.to = localeTo; | ||
} | ||
|
||
return <Component {...linkProps} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters