
フロントエンド
11/11/2026
6
npm install i18next react-i18nextsrc/locales/ja.json
{
"todo": {
"title": "Todo",
"description": "新しいToDoを入力",
"add": "追加",
"totalTime": "合計作業時間",
"hourUnit": "時間",
"minuteUnit": "分",
"delete": "削除"
},
"buttons": {
"switchLang": "日本語"
}
}src/locales/en.json
{
"todo": {
"title": "To-Do List",
"description": "Enter a new To-Do",
"add": "Add",
"totalTime": "Total Work Time",
"hourUnit": "h",
"minuteUnit": "m",
"delete": "Delete"
},
"buttons": {
"switchLang": "English"
}
}// src/i18n.ts
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import ja from './locales/ja.json';
import en from './locales/en.json';
i18n
.use(initReactI18next) // react-i18nextと連携
.init({
resources: {
ja: { translation: ja },
en: { translation: en },
},
lng: 'ja', // 初期言語
fallbackLng: 'ja', // 翻訳が見つからない場合の言語
interpolation: {
escapeValue: false, // ReactはデフォルトでXSS対策されているためfalse
},
});
export default i18n;"use client";
// 辞書ライブラリー
import '@/i18n';'use client';
import { useTranslation } from 'react-i18next';
const UseTranslation = () => {
const { t, i18n } = useTranslation(); //ここでuseTranslation フックを呼び出す
// 言語を切り替える関数
const toggleLanguage = () => {
const nextLang = i18n.language === 'en' ? 'ja' : 'en';
i18n.changeLanguage(nextLang);
};
return (
<>
{/* 「合計作業時間」または「Total Work Time」を表示 */}
<p>{t('todo.totalTime')}</p>
{/* 言語切り替えボタン */}
<button onClick={toggleLanguage}>
{t('buttons.switchLang')}
</button>
</>
)
}
Hika
UI/UXデザインとフロントエンドを学びながら、React / Next.js / TypeScript / Supabase を使ってWebアプリを個人開発しています。