simple-i18n-example icon indicating copy to clipboard operation
simple-i18n-example copied to clipboard

Proposal: Extend the Link component

Open simonschllng opened this issue 4 years ago • 2 comments

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

simonschllng avatar Jun 26 '20 09:06 simonschllng

I don't know why I never thought of extending the default Link component. Brilliant! I'm definitely refactoring this over the weekend.

BiscuiTech avatar Jun 27 '20 01:06 BiscuiTech

Looks great to me! Do you want to make a pull request?

fwojciec avatar Jun 27 '20 05:06 fwojciec