diff --git a/package-lock.json b/package-lock.json index a1edbf00..0fa0820d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,6 +54,7 @@ "decimal.js": "^10.6.0", "embla-carousel-react": "^8.3.0", "esbuild-wasm": "^0.25.8", + "i18next": "^25.5.2", "ignore": "^7.0.5", "input-otp": "^1.2.4", "isomorphic-git": "^1.32.1", @@ -67,6 +68,7 @@ "react-day-picker": "^8.10.1", "react-dom": "^18.3.1", "react-hook-form": "^7.53.0", + "react-i18next": "^15.7.3", "react-markdown": "^10.1.0", "react-resizable-panels": "^3.0.3", "react-router-dom": "^6.26.2", @@ -209,9 +211,9 @@ } }, "node_modules/@babel/runtime": { - "version": "7.27.1", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.27.1.tgz", - "integrity": "sha512-1x3D2xEk2fRo3PAhwQwu5UubzgiVWSXTBfWpVd2Mx2AzRqJuDJCsgaDVZ7HB5iGzDW1Hl1sWN2mFyKjmR9uAog==", + "version": "7.28.4", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.28.4.tgz", + "integrity": "sha512-Q/N6JNWvIvPnLDvjlE1OUBLPQHH6l3CltCEsHIujp45zQUSSh8K+gHnaEX45yAT1nyngnINhvWtzN+Nb9D8RAQ==", "license": "MIT", "engines": { "node": ">=6.9.0" @@ -6331,6 +6333,15 @@ "node": ">=18" } }, + "node_modules/html-parse-stringify": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/html-parse-stringify/-/html-parse-stringify-3.0.1.tgz", + "integrity": "sha512-KknJ50kTInJ7qIScF3jeaFRpMpE8/lfiTdzf/twXyPBLAGrLRTmkz3AdTnKeh40X8k9L2fdYwEp/42WGXIRGcg==", + "license": "MIT", + "dependencies": { + "void-elements": "3.1.0" + } + }, "node_modules/html-url-attributes": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/html-url-attributes/-/html-url-attributes-3.0.1.tgz", @@ -6379,6 +6390,37 @@ "node": ">= 14" } }, + "node_modules/i18next": { + "version": "25.5.2", + "resolved": "https://registry.npmjs.org/i18next/-/i18next-25.5.2.tgz", + "integrity": "sha512-lW8Zeh37i/o0zVr+NoCHfNnfvVw+M6FQbRp36ZZ/NyHDJ3NJVpp2HhAUyU9WafL5AssymNoOjMRB48mmx2P6Hw==", + "funding": [ + { + "type": "individual", + "url": "https://locize.com" + }, + { + "type": "individual", + "url": "https://locize.com/i18next.html" + }, + { + "type": "individual", + "url": "https://www.i18next.com/how-to/faq#i18next-is-awesome.-how-can-i-support-the-project" + } + ], + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.27.6" + }, + "peerDependencies": { + "typescript": "^5" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, "node_modules/iconv-lite": { "version": "0.6.3", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", @@ -8888,6 +8930,32 @@ "react": "^16.8.0 || ^17 || ^18 || ^19" } }, + "node_modules/react-i18next": { + "version": "15.7.3", + "resolved": "https://registry.npmjs.org/react-i18next/-/react-i18next-15.7.3.tgz", + "integrity": "sha512-AANws4tOE+QSq/IeMF/ncoHlMNZaVLxpa5uUGW1wjike68elVYr0018L9xYoqBr1OFO7G7boDPrbn0HpMCJxTw==", + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.27.6", + "html-parse-stringify": "^3.0.1" + }, + "peerDependencies": { + "i18next": ">= 25.4.1", + "react": ">= 16.8.0", + "typescript": "^5" + }, + "peerDependenciesMeta": { + "react-dom": { + "optional": true + }, + "react-native": { + "optional": true + }, + "typescript": { + "optional": true + } + } + }, "node_modules/react-is": { "version": "18.3.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", @@ -10848,6 +10916,15 @@ } } }, + "node_modules/void-elements": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/void-elements/-/void-elements-3.1.0.tgz", + "integrity": "sha512-Dhxzh5HZuiHQhbvTW9AMetFfBHDMYpo23Uo9btPXgdYP+3T5S+p+jgNy7spra+veYhBP2dCSgxR/i2Y02h5/6w==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/w3c-xmlserializer": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-5.0.0.tgz", diff --git a/package.json b/package.json index 6a554e93..d15b1325 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "decimal.js": "^10.6.0", "embla-carousel-react": "^8.3.0", "esbuild-wasm": "^0.25.8", + "i18next": "^25.5.2", "ignore": "^7.0.5", "input-otp": "^1.2.4", "isomorphic-git": "^1.32.1", @@ -69,6 +70,7 @@ "react-day-picker": "^8.10.1", "react-dom": "^18.3.1", "react-hook-form": "^7.53.0", + "react-i18next": "^15.7.3", "react-markdown": "^10.1.0", "react-resizable-panels": "^3.0.3", "react-router-dom": "^6.26.2", diff --git a/src/App.tsx b/src/App.tsx index b3944f7d..a6a0415b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -41,6 +41,7 @@ const defaultConfig: AppConfig = { theme: "light", relayUrl: "wss://relay.primal.net", deployServer: "shakespeare.wtf", + language: "en", }; const presetRelays = [ diff --git a/src/components/AppProvider.tsx b/src/components/AppProvider.tsx index 1065cc5c..750799d3 100644 --- a/src/components/AppProvider.tsx +++ b/src/components/AppProvider.tsx @@ -1,7 +1,8 @@ import { ReactNode, useEffect } from 'react'; import { z } from 'zod'; import { useLocalStorage } from '@/hooks/useLocalStorage'; -import { AppContext, type AppConfig, type AppContextType, type Theme } from '@/contexts/AppContext'; +import { AppContext, type AppConfig, type AppContextType, type Theme, type Language } from '@/contexts/AppContext'; +import i18n from '@/lib/i18n'; interface AppProviderProps { children: ReactNode; @@ -18,6 +19,7 @@ const AppConfigSchema: z.ZodType = z.object({ theme: z.enum(['dark', 'light', 'system']), relayUrl: z.string().url(), deployServer: z.string().min(1), + language: z.enum(['en', 'pt']), }); export function AppProvider(props: AppProviderProps) { @@ -55,6 +57,9 @@ export function AppProvider(props: AppProviderProps) { // Apply theme effects to document useApplyTheme(config.theme); + // Apply language effects to i18n + useApplyLanguage(config.language); + return ( {children} @@ -101,4 +106,13 @@ function useApplyTheme(theme: Theme) { mediaQuery.addEventListener('change', handleChange); return () => mediaQuery.removeEventListener('change', handleChange); }, [theme]); +} + +/** + * Hook to apply language changes to i18n + */ +function useApplyLanguage(language: Language) { + useEffect(() => { + i18n.changeLanguage(language); + }, [language]); } \ No newline at end of file diff --git a/src/components/LanguagePicker.test.tsx b/src/components/LanguagePicker.test.tsx new file mode 100644 index 00000000..3471cdb6 --- /dev/null +++ b/src/components/LanguagePicker.test.tsx @@ -0,0 +1,49 @@ +import { describe, it, expect } from 'vitest'; +import { render, screen, fireEvent } from '@testing-library/react'; +import { TestApp } from '@/test/TestApp'; +import { LanguagePicker } from './LanguagePicker'; + +describe('LanguagePicker', () => { + it('renders language picker with default English selection', () => { + render( + + + + ); + + // Should show the trigger button + expect(screen.getByRole('combobox')).toBeInTheDocument(); + }); + + it('shows language options when opened', async () => { + render( + + + + ); + + // Click to open the dropdown + const trigger = screen.getByRole('combobox'); + fireEvent.click(trigger); + + // Should show both language options (using getAllByText since there might be duplicates) + expect(screen.getAllByText('English').length).toBeGreaterThan(0); + expect(screen.getAllByText('Portuguese').length).toBeGreaterThan(0); + }); + + it('displays flag emojis for languages', async () => { + render( + + + + ); + + // Click to open the dropdown + const trigger = screen.getByRole('combobox'); + fireEvent.click(trigger); + + // Should show flag emojis (using getAllByText since there might be duplicates) + expect(screen.getAllByText('🇺🇸').length).toBeGreaterThan(0); + expect(screen.getAllByText('🇧🇷').length).toBeGreaterThan(0); + }); +}); \ No newline at end of file diff --git a/src/components/LanguagePicker.tsx b/src/components/LanguagePicker.tsx new file mode 100644 index 00000000..c41d9141 --- /dev/null +++ b/src/components/LanguagePicker.tsx @@ -0,0 +1,41 @@ +import { useTranslation } from 'react-i18next'; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; +import { useAppContext } from '@/hooks/useAppContext'; +import type { Language } from '@/contexts/AppContext'; + +interface LanguagePickerProps { + className?: string; +} + +export function LanguagePicker({ className }: LanguagePickerProps) { + const { t } = useTranslation(); + const { config, updateConfig } = useAppContext(); + + const handleLanguageChange = (language: Language) => { + updateConfig((prev) => ({ ...prev, language })); + }; + + return ( + + ); +} \ No newline at end of file diff --git a/src/components/ThemePicker.tsx b/src/components/ThemePicker.tsx index 3cc806b6..b5180aae 100644 --- a/src/components/ThemePicker.tsx +++ b/src/components/ThemePicker.tsx @@ -1,4 +1,5 @@ import { ChevronsUpDown, Monitor, Moon, Sun } from "lucide-react"; +import { useTranslation } from 'react-i18next'; import { Button } from "@/components/ui/button"; import { DropdownMenu, @@ -10,12 +11,13 @@ import { useTheme } from "@/hooks/useTheme"; import { type Theme } from "@/contexts/AppContext"; export function ThemePicker() { + const { t } = useTranslation(); const { theme, setTheme } = useTheme(); const themeOptions: { value: Theme; label: string; icon: React.ComponentType<{ className?: string }> }[] = [ - { value: "system", label: "System", icon: Monitor }, - { value: "light", label: "Light", icon: Sun }, - { value: "dark", label: "Dark", icon: Moon }, + { value: "system", label: t('system'), icon: Monitor }, + { value: "light", label: t('light'), icon: Sun }, + { value: "dark", label: t('dark'), icon: Moon }, ]; const currentTheme = themeOptions.find(option => option.value === theme); @@ -27,7 +29,7 @@ export function ThemePicker() { diff --git a/src/contexts/AppContext.ts b/src/contexts/AppContext.ts index 60a71e4e..004dcf82 100644 --- a/src/contexts/AppContext.ts +++ b/src/contexts/AppContext.ts @@ -1,6 +1,7 @@ import { createContext } from "react"; export type Theme = "dark" | "light" | "system"; +export type Language = "en" | "pt"; export interface AppConfig { /** Current theme */ @@ -9,6 +10,8 @@ export interface AppConfig { relayUrl: string; /** Deploy server domain (e.g., "shakespeare.wtf") */ deployServer: string; + /** Selected language */ + language: Language; } export interface AppContextType { diff --git a/src/hooks/useTranslation.ts b/src/hooks/useTranslation.ts new file mode 100644 index 00000000..f466023f --- /dev/null +++ b/src/hooks/useTranslation.ts @@ -0,0 +1,2 @@ +// Re-export useTranslation from react-i18next for consistency +export { useTranslation } from 'react-i18next'; \ No newline at end of file diff --git a/src/lib/i18n.ts b/src/lib/i18n.ts new file mode 100644 index 00000000..dc2aca57 --- /dev/null +++ b/src/lib/i18n.ts @@ -0,0 +1,159 @@ +import i18n from 'i18next'; +import { initReactI18next } from 'react-i18next'; + +// Translation resources +const resources = { + en: { + translation: { + // Navigation and Layout + preferences: 'Preferences', + settings: 'Settings', + backToSettings: 'Back to Settings', + + // Preferences Page + preferencesTitle: 'Preferences', + preferencesDescription: 'Manage your general application preferences and appearance settings.', + + // Appearance Section + appearance: 'Appearance', + appearanceDescription: 'Customize how the application looks and feels.', + theme: 'Theme', + themeDescription: 'Choose between light, dark, or system theme preference.', + + // Language Section + language: 'Language', + languageDescription: 'Select your preferred language for the interface.', + + // Language Options + english: 'English', + portuguese: 'Portuguese', + + // Theme Options + light: 'Light', + dark: 'Dark', + system: 'System', + + // Common UI Elements + save: 'Save', + cancel: 'Cancel', + close: 'Close', + open: 'Open', + edit: 'Edit', + delete: 'Delete', + create: 'Create', + update: 'Update', + loading: 'Loading...', + error: 'Error', + success: 'Success', + warning: 'Warning', + info: 'Information', + + // Shakespeare Main Page + buildNostrApps: 'Build Nostr apps with AI', + whatToBuild: 'What would you like to build?', + createProject: 'Create Project', + generating: 'Generating...', + creating: 'Creating...', + chooseModel: 'Choose a model...', + selectModelFirst: "Please select a model below, then describe what you'd like to build...", + examplePrompt: "e.g., Create a farming equipment marketplace for local farmers to buy and sell tractors, tools, and supplies...", + + // Settings Page + settingsTitle: 'Settings', + settingsDescription: 'Manage your application settings and preferences.', + aiSettings: 'AI Settings', + aiSettingsDescription: 'Configure AI providers and API keys', + gitSettings: 'Git Settings', + gitSettingsDescription: 'Configure Git credentials for HTTP authentication', + nostrSettings: 'Nostr Settings', + nostrSettingsDescription: 'Configure relay connections and Nostr preferences', + dataSettings: 'Data', + dataSettingsDescription: 'Export files and manage local data', + } + }, + pt: { + translation: { + // Navigation and Layout + preferences: 'Preferências', + settings: 'Configurações', + backToSettings: 'Voltar às Configurações', + + // Preferences Page + preferencesTitle: 'Preferências', + preferencesDescription: 'Gerencie suas preferências gerais da aplicação e configurações de aparência.', + + // Appearance Section + appearance: 'Aparência', + appearanceDescription: 'Personalize como a aplicação se parece e funciona.', + theme: 'Tema', + themeDescription: 'Escolha entre preferência de tema claro, escuro ou do sistema.', + + // Language Section + language: 'Idioma', + languageDescription: 'Selecione seu idioma preferido para a interface.', + + // Language Options + english: 'Inglês', + portuguese: 'Português', + + // Theme Options + light: 'Claro', + dark: 'Escuro', + system: 'Sistema', + + // Common UI Elements + save: 'Salvar', + cancel: 'Cancelar', + close: 'Fechar', + open: 'Abrir', + edit: 'Editar', + delete: 'Excluir', + create: 'Criar', + update: 'Atualizar', + loading: 'Carregando...', + error: 'Erro', + success: 'Sucesso', + warning: 'Aviso', + info: 'Informação', + + // Shakespeare Main Page + buildNostrApps: 'Construa aplicativos Nostr com IA', + whatToBuild: 'O que você gostaria de construir?', + createProject: 'Criar Projeto', + generating: 'Gerando...', + creating: 'Criando...', + chooseModel: 'Escolha um modelo...', + selectModelFirst: "Por favor, selecione um modelo abaixo e depois descreva o que gostaria de construir...", + examplePrompt: "ex., Criar um marketplace de equipamentos agrícolas para fazendeiros locais comprarem e venderem tratores, ferramentas e suprimentos...", + + // Settings Page + settingsTitle: 'Configurações', + settingsDescription: 'Gerencie as configurações e preferências da aplicação.', + aiSettings: 'Configurações de IA', + aiSettingsDescription: 'Configure provedores de IA e chaves de API', + gitSettings: 'Configurações do Git', + gitSettingsDescription: 'Configure credenciais do Git para autenticação HTTP', + nostrSettings: 'Configurações do Nostr', + nostrSettingsDescription: 'Configure conexões de relay e preferências do Nostr', + dataSettings: 'Dados', + dataSettingsDescription: 'Exporte arquivos e gerencie dados locais', + } + } +}; + +// Initialize i18next +i18n + .use(initReactI18next) + .init({ + resources, + lng: 'en', // Default language + fallbackLng: 'en', + interpolation: { + escapeValue: false, // React already escapes values + }, + react: { + useSuspense: false, // Disable suspense for better compatibility + } + }); + +export default i18n; \ No newline at end of file diff --git a/src/main.tsx b/src/main.tsx index 5dfd85d9..65eae552 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -3,6 +3,9 @@ import { createRoot } from 'react-dom/client'; // Import polyfills first import './lib/polyfills.ts'; +// Import i18n configuration +import './lib/i18n'; + import App from './App.tsx'; import './index.css'; diff --git a/src/pages/Preferences.test.tsx b/src/pages/Preferences.test.tsx new file mode 100644 index 00000000..a169202d --- /dev/null +++ b/src/pages/Preferences.test.tsx @@ -0,0 +1,40 @@ +import { describe, it, expect } from 'vitest'; +import { render, screen } from '@testing-library/react'; +import { TestApp } from '@/test/TestApp'; +import { Preferences } from './Preferences'; + +describe('Preferences', () => { + it('renders preferences page with language and theme sections', () => { + render( + + + + ); + + // Should show the preferences title + expect(screen.getByText('Preferences')).toBeInTheDocument(); + + // Should show appearance section + expect(screen.getByText('Appearance')).toBeInTheDocument(); + + // Should show theme and language labels + expect(screen.getByText('Theme')).toBeInTheDocument(); + expect(screen.getByText('Language')).toBeInTheDocument(); + + // Should show language picker + expect(screen.getByRole('combobox')).toBeInTheDocument(); + }); + + it('displays translated content correctly', () => { + render( + + + + ); + + // Check for key translated strings + expect(screen.getByText('Manage your general application preferences and appearance settings.')).toBeInTheDocument(); + expect(screen.getByText('Customize how the application looks and feels.')).toBeInTheDocument(); + expect(screen.getByText('Select your preferred language for the interface.')).toBeInTheDocument(); + }); +}); \ No newline at end of file diff --git a/src/pages/Preferences.tsx b/src/pages/Preferences.tsx index a99d4e45..9f7dc90d 100644 --- a/src/pages/Preferences.tsx +++ b/src/pages/Preferences.tsx @@ -1,12 +1,15 @@ import { Settings2, ArrowLeft } from "lucide-react"; +import { useTranslation } from 'react-i18next'; import { Button } from "@/components/ui/button"; import { ThemePicker } from "@/components/ThemePicker"; +import { LanguagePicker } from "@/components/LanguagePicker"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { useIsMobile } from "@/hooks/useIsMobile"; import { useNavigate } from "react-router-dom"; export function Preferences() { + const { t } = useTranslation(); const isMobile = useIsMobile(); const navigate = useNavigate(); @@ -21,15 +24,15 @@ export function Preferences() { className="h-8 w-auto px-2 -ml-2" > - Back to Settings + {t('backToSettings')}

- Preferences + {t('preferencesTitle')}

- Manage your general application preferences and appearance settings. + {t('preferencesDescription')}

@@ -39,29 +42,39 @@ export function Preferences() {

- Preferences + {t('preferencesTitle')}

- Manage your general application preferences and appearance settings. + {t('preferencesDescription')}

)} - Appearance + {t('appearance')} - Customize how the application looks and feels. + {t('appearanceDescription')} - +
- +

- Choose between light, dark, or system theme preference. + {t('themeDescription')} +

+
+ +
+ +
+ +
+

+ {t('languageDescription')}

diff --git a/src/pages/Settings.tsx b/src/pages/Settings.tsx index 5e9f3431..1239ce7b 100644 --- a/src/pages/Settings.tsx +++ b/src/pages/Settings.tsx @@ -1,4 +1,5 @@ import { useNavigate } from 'react-router-dom'; +import { useTranslation } from 'react-i18next'; import { Bot, GitBranch, Database, Wifi, Settings2 } from 'lucide-react'; import { useIsMobile } from '@/hooks/useIsMobile'; @@ -10,55 +11,59 @@ interface SettingsItem { href: string; } -const settingsItems: SettingsItem[] = [ +// Define settings items as a function to get fresh translations +const getSettingsItems = (t: (key: string) => string): SettingsItem[] => [ { id: 'preferences', - title: 'Preferences', - description: 'General application preferences and appearance', + title: t('preferences'), + description: t('preferencesDescription'), icon: Settings2, href: '/settings/preferences', }, { id: 'ai', - title: 'AI Settings', - description: 'Configure AI providers and API keys', + title: t('aiSettings'), + description: t('aiSettingsDescription'), icon: Bot, href: '/settings/ai', }, { id: 'git', - title: 'Git Settings', - description: 'Configure Git credentials for HTTP authentication', + title: t('gitSettings'), + description: t('gitSettingsDescription'), icon: GitBranch, href: '/settings/git', }, { id: 'nostr', - title: 'Nostr Settings', - description: 'Configure relay connections and Nostr preferences', + title: t('nostrSettings'), + description: t('nostrSettingsDescription'), icon: Wifi, href: '/settings/nostr', }, { id: 'data', - title: 'Data', - description: 'Export files and manage local data', + title: t('dataSettings'), + description: t('dataSettingsDescription'), icon: Database, href: '/settings/data', }, ]; export function Settings() { + const { t } = useTranslation(); const navigate = useNavigate(); const isMobile = useIsMobile(); + const settingsItems = getSettingsItems(t); + if (isMobile) { return (
-

Settings

+

{t('settingsTitle')}

- Manage your application settings and preferences. + {t('settingsDescription')}

diff --git a/src/test/TestApp.tsx b/src/test/TestApp.tsx index 1dd162a3..1bdc7924 100644 --- a/src/test/TestApp.tsx +++ b/src/test/TestApp.tsx @@ -11,6 +11,7 @@ import { LightningFSAdapter } from '@/lib/LightningFSAdapter'; import { AISettingsProvider } from '@/components/AISettingsProvider'; import { GitSettingsProvider } from '@/components/GitSettingsProvider'; import { SessionManagerProvider } from '@/components/SessionManagerProvider'; +import '@/lib/i18n'; interface TestAppProps { children: React.ReactNode; @@ -30,6 +31,7 @@ export function TestApp({ children }: TestAppProps) { theme: 'light', relayUrl: 'wss://relay.nostr.band', deployServer: 'shakespeare.wtf', + language: 'en', }; // Initialize LightningFS for testing