For anyone tackling this with next-18next
in 2023. I am using:
"i18next": "^22.4.9",
"next": "13.1.6",
"next-i18next": "^13.1.5",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-i18next": "^12.1.5"
Translation file
public/locales/<locale>/common.json
{
"replacedValue": "some replaced value",
"anotherReplacedValue": "another replaced value",
"sentence": "Sentence with <strong>{{replacedValue}}</strong>, and <i>{{anotherReplacedValue}}</i>."
}
Page file
pages/index.js
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { useTranslation, Trans } from "next-i18next";
export default function Home() {
const { t } = useTranslation("common");
return (
<p>
<Trans>
{t("sentence", {
replacedValue: t('replacedValue'),
anotherReplacedValue: t('anotherReplacedValue'),
})}
</Trans>
</p>
);
}
export async function getServerSideProps({ locale }) {
return {
props: {
...(await serverSideTranslations(locale, ["common"])),
},
};
}
Result
Sentence with some replaced value, and another replaced value.
Note ⚠️
Be sure to import Trans
from next-i18next
rather than react-i18next
, otherwise you'll get hydration errors since the translations happens client-side instead of server-side.