simple-i18n-example
simple-i18n-example copied to clipboard
Proposal: Extend the Link component
Proposing to add the locale at one place to the Link
instead of each place. Use LocaleLink
instead of Link
where needed.
import Link, { LinkProps } from 'next/link'
import React from 'react'
import useTranslation from '../lib/useTranslation'
const LocaleLink: React.FC<LinkProps> = (props) => {
const { locale } = useTranslation()
const localized = Object.assign({}, props, {
href: `/[lang]${props.href}`,
as: props.as ? `/${locale}${props.as}` : `/${locale}${props.href}`
})
return <Link {...localized} />
}
export default LocaleLink
I don't know why I never thought of extending the default Link component. Brilliant! I'm definitely refactoring this over the weekend.
Looks great to me! Do you want to make a pull request?