Skip to content

Instantly share code, notes, and snippets.

@ivandoric

ivandoric/[id].jsx

Last active Jun 6, 2021
Embed
What would you like to do?
Code used in video - [Next + Strapi - Internationalization](https://www.youtube.com/watch?v=iqi3ZSp1cpE)
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;
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