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
1 parent
c03e2d7
commit 24da924
Showing
4 changed files
with
65 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,59 @@ | ||
import React from 'react'; | ||
import Link from 'next/link'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import getConfig from 'next/config'; | ||
|
||
const { publicRuntimeConfig } = getConfig(); | ||
const permalinks: { [key: string]: { [key: string]: string } } = | ||
publicRuntimeConfig.permalinks || {}; | ||
|
||
/** | ||
* Formats permalinks | ||
{ | ||
"/": { | ||
"fr": "/accueil" | ||
} | ||
} | ||
* into | ||
{ | ||
"/fr/": "/fr/accueil", | ||
"/en/accueil": "/" | ||
} | ||
*/ | ||
export const i18nFallbackUrls: { [key: string]: string } = Object.entries( | ||
permalinks | ||
).reduce( | ||
(acc, [originalSlug, permalinks]) => ({ | ||
...acc, | ||
...Object.entries(permalinks || {}).reduce( | ||
(acc2, [locale, permalink]) => ({ | ||
...acc2, | ||
[`/${locale}${originalSlug}`]: `/${locale}${permalink}`, | ||
[`/en${permalink}`]: originalSlug, | ||
}), | ||
{} | ||
), | ||
}), | ||
{} | ||
); | ||
|
||
const I18nLink = ({ href, locale, ...props }: any) => { | ||
const router = useRouter(); | ||
const wantedLocale = locale || router.locale; | ||
let i18nProps: any = { | ||
href, | ||
locale, | ||
}; | ||
|
||
if (i18nFallbackUrls[`/${wantedLocale}${href}`]) { | ||
i18nProps = { | ||
href: i18nFallbackUrls[`/${wantedLocale}${href}`], | ||
locale: false, | ||
}; | ||
} | ||
|
||
return <Link {...i18nProps} {...props} />; | ||
}; | ||
|
||
export default I18nLink; |
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,3 +1,4 @@ | ||
export { default as useTranslation } from 'next-translate/useTranslation'; | ||
|
||
export { default as LanguageSwitcher } from './components/LanguageSwitcher'; | ||
export { default as Link } from './components/Link'; |
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