Last active
February 28, 2023 12:42
-
-
Save ivandoric/ae1ca794b2bbb3e68bc7952018aab34e to your computer and use it in GitHub Desktop.
Code used in video - [Next + Strapi - Internationalization](https://www.youtube.com/watch?v=iqi3ZSp1cpE)
This file contains hidden or 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
import Link from "next/link"; | |
import { useRouter } from "next/router"; | |
function Page({ content }) { | |
const router = useRouter(); | |
console.log(router); | |
return ( | |
<div className="container"> | |
<h2>{content.title}</h2> | |
<div className="body">{content.body}</div> | |
<br /> | |
<br /> | |
<Link | |
href={router.asPath} | |
locale={router.locale === "en-US" ? "hr" : "en-US"} | |
> | |
<a> | |
{router.locale === "en-US" | |
? "Prikaži hrvatski prijevod" | |
: "Show english translation"} | |
</a> | |
</Link> | |
</div> | |
); | |
} | |
export const getServerSideProps = async (context) => { | |
const { id } = context.params; | |
const { locale } = context; | |
let translation = undefined; | |
const initialRes = await fetch(`http://localhost:1337/pages/${id}`); | |
const initial = await initialRes.json(); | |
if (locale === "hr") { | |
const translationRes = await fetch( | |
`http://localhost:1337/pages/${initial.localizations[0].id}` | |
); | |
translation = await translationRes.json(); | |
} | |
return { | |
props: { | |
content: translation ? translation : initial, | |
}, | |
}; | |
}; | |
export default Page; |
This file contains hidden or 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
module.exports = { | |
i18n: { | |
locales: ["en-US", "hr"], | |
defaultLocale: "en-US", | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment