Add i18n system with 4-language support (en/zh/ja/ko)

- Add I18nContext provider with useI18n hook and localStorage persistence
- Add translations.ts with complete strings for English, Chinese, Japanese, Korean
- Wrap App in I18nProvider, replace hardcoded toast messages with t() calls
- Add language selector to SettingsModal with Globe icon
- Add localization credits in About section
- Default language: English
- Add I18N_USAGE.md documentation
This commit is contained in:
fspecii
2026-02-08 18:19:08 +02:00
parent af5343f9db
commit c219edb037
5 changed files with 2591 additions and 60 deletions
+40
View File
@@ -0,0 +1,40 @@
import React, { createContext, useContext, useState, ReactNode } from 'react';
import { translations, Language, TranslationKey } from '../i18n/translations';
interface I18nContextType {
language: Language;
setLanguage: (lang: Language) => void;
t: (key: TranslationKey) => string;
}
const I18nContext = createContext<I18nContextType | undefined>(undefined);
export const I18nProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
const [language, setLanguage] = useState<Language>(() => {
const stored = localStorage.getItem('language') as Language;
return stored === 'zh' || stored === 'en' || stored === 'ja' || stored === 'ko' ? stored : 'en';
});
const handleSetLanguage = (lang: Language) => {
setLanguage(lang);
localStorage.setItem('language', lang);
};
const t = (key: TranslationKey): string => {
return translations[language][key] || key;
};
return (
<I18nContext.Provider value={{ language, setLanguage: handleSetLanguage, t }}>
{children}
</I18nContext.Provider>
);
};
export const useI18n = () => {
const context = useContext(I18nContext);
if (!context) {
throw new Error('useI18n must be used within I18nProvider');
}
return context;
};