-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
239 additions
and
104 deletions.
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { | ||
matchAllFilepathPartsRegex, | ||
optionalMatchAllFilepathPartsRegex, | ||
optionalMatchAllFilepathPartRegex, | ||
dynamicFilepathPartsRegex, | ||
} from '../shared/regex' | ||
|
||
/** Transform Next file-system synthax to path-to-regexp synthax */ | ||
/** Transform Next file-system syntax to path-to-regexp syntax */ | ||
export const fileNameToPath = (fileName: string) => | ||
fileName | ||
.replace(optionalMatchAllFilepathPartsRegex, ':$1*') // [[...param]] | ||
.replace(optionalMatchAllFilepathPartRegex, ':$1*') // [[...param]] | ||
.replace(matchAllFilepathPartsRegex, ':$1+') // [...param] | ||
.replace(dynamicFilepathPartsRegex, ':$1') // [param] |
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
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 |
---|---|---|
@@ -1,60 +1,17 @@ | ||
import NextLink, { LinkProps } from 'next/link' | ||
import { useRouter as useNextRouter } from 'next/router' | ||
import React from 'react' | ||
import type { UrlObject } from 'url' | ||
|
||
import { fileUrlToUrl } from './fileUrlToUrl' | ||
import { getLocale } from './getLocale' | ||
import { getNtrData } from './ntrData' | ||
import { removeLangPrefix } from './removeLangPrefix' | ||
import { urlToFileUrl } from './urlToFileUrl' | ||
import { translatePushReplaceArgs } from './translatePushReplaceArgs' | ||
|
||
/** | ||
* Link component that handle route translations | ||
*/ | ||
export const Link: React.FC<React.PropsWithChildren<LinkProps>> = ({ href, as, locale: propLocale, ...props }) => { | ||
export const Link: React.FC<React.PropsWithChildren<LinkProps>> = ({ href, as, locale, ...props }) => { | ||
const router = useNextRouter() | ||
let locale = getLocale(router, propLocale) | ||
const locales = router.locales || getNtrData().locales | ||
const unPrefixedHref = typeof href === 'string' ? removeLangPrefix(href) : href | ||
const translatedArgs = translatePushReplaceArgs({ router, url: href, as, locale }) | ||
|
||
if (!propLocale && typeof href === 'string' && unPrefixedHref !== href) { | ||
const hrefLocale = href.split('/')[1] | ||
if (hrefLocale && locales.includes(hrefLocale)) { | ||
locale = hrefLocale | ||
} | ||
} | ||
|
||
let translatedUrl: string | undefined | ||
let parsedUrl: UrlObject | URL | string | undefined | ||
|
||
/** | ||
* Href can be: | ||
* - an external url | ||
* - a correct file url | ||
* - a wrong file url (not matching any page) | ||
* - a correct translated url | ||
* - a wrong translated url | ||
*/ | ||
try { | ||
translatedUrl = fileUrlToUrl(unPrefixedHref, locale) | ||
// Href is a correct file url | ||
parsedUrl = unPrefixedHref | ||
} catch { | ||
parsedUrl = urlToFileUrl(unPrefixedHref, locale) | ||
if (parsedUrl) { | ||
try { | ||
translatedUrl = fileUrlToUrl(parsedUrl, locale) | ||
// Href is a correct translated url | ||
} catch { | ||
// Href is a wrong file url or an external url | ||
} | ||
} else { | ||
// Href is a wrong url or an external url | ||
} | ||
} | ||
|
||
return <NextLink href={parsedUrl || href} as={as || translatedUrl} locale={locale} {...props} /> | ||
return <NextLink href={translatedArgs.url} as={translatedArgs.as} locale={translatedArgs.locale} {...props} /> | ||
} | ||
|
||
export default Link |
Oops, something went wrong.