Skip to content

Instantly share code, notes, and snippets.

@ivandoric
Last active February 28, 2023 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ivandoric/ae1ca794b2bbb3e68bc7952018aab34e to your computer and use it in GitHub Desktop.
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)
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