0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Next.js App Router で react-i18next の書き方

Posted at

react-i18next の書き方メモ

クライアントコンポーネント

"use client";

import { useTranslation } from "react-i18next";

interface Hoge {
  title: string;
  text: string;
}

export default function Page() {
  const { t } = useTranslation();
  const hoge = t("hoge", {
    returnObjects: true,
  }) as Hoge;

  return (
    <>
      <h1>{hoge.title}</h1>
      <p>{hoge.text}</p>
    </>
  );
}    

サーバーコンポーネント

import initTranslations from "@/app/i18n";

interface Props {
  locale: string;
}

interface Hoge {
  title: string;
  text: string;
}

const i18nNamespaces = ["hoge"];

export default async function Page({ locale }: Props) {
  const { t } = await initTranslations(locale, i18nNamespaces);
  const hoge = t("hoge", {
    returnObjects: true,
  }) as Hoge;

  return (
    <>
      <h1>{hoge.title}</h1>
      <p>{hoge.text}</p>
    </>
  );
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?