コンテンツにスキップ

react-i18next

使う

Introduction | react-i18next documentation

1
$ npm i react-i18next i18next
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import jaJp from "./locales/ja-JP.json";

i18n.use(initReactI18next).init({
  resources: {
    "ja-JP": {
      translation: jaJp,
    },
  },
  lng: "ja-JP",
  fallbackLng: "ja-JP",
  interpolation: {
    escapeValue: false,
  },
});
src/locales/ja-JP.json
1
2
3
{
  "version": "バージョン: {{version}}"
}
1
2
3
4
5
6
7
import { useTranslation } from "react-i18next";

export function App() {
  const { t } = useTranslation();

  return <div>{t("version", { version: import.meta.env.VERSION })}</div>;
}