mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
Default to browser language
This commit is contained in:
6 files changed
+19
-12
No files matched your search
@@ -41,7 +41,6 @@ const defaultConfig: AppConfig = {
|
|||||||
theme: "light",
|
theme: "light",
|
||||||
relayUrl: "wss://relay.primal.net",
|
relayUrl: "wss://relay.primal.net",
|
||||||
deployServer: "shakespeare.wtf",
|
deployServer: "shakespeare.wtf",
|
||||||
language: "en",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const presetRelays = [
|
const presetRelays = [
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { ReactNode, useEffect } from 'react';
|
import { ReactNode, useEffect } from 'react';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||||
import { AppContext, type AppConfig, type AppContextType, type Theme, type Language } from '@/contexts/AppContext';
|
import { AppContext, type AppConfig, type AppContextType, type Theme } from '@/contexts/AppContext';
|
||||||
import i18n from '@/lib/i18n';
|
import i18n from '@/lib/i18n';
|
||||||
|
|
||||||
interface AppProviderProps {
|
interface AppProviderProps {
|
||||||
@@ -19,7 +19,7 @@ const AppConfigSchema: z.ZodType<AppConfig, z.ZodTypeDef, unknown> = z.object({
|
|||||||
theme: z.enum(['dark', 'light', 'system']),
|
theme: z.enum(['dark', 'light', 'system']),
|
||||||
relayUrl: z.string().url(),
|
relayUrl: z.string().url(),
|
||||||
deployServer: z.string().min(1),
|
deployServer: z.string().min(1),
|
||||||
language: z.enum(['en', 'pt']),
|
language: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export function AppProvider(props: AppProviderProps) {
|
export function AppProvider(props: AppProviderProps) {
|
||||||
@@ -111,8 +111,8 @@ function useApplyTheme(theme: Theme) {
|
|||||||
/**
|
/**
|
||||||
* Hook to apply language changes to i18n
|
* Hook to apply language changes to i18n
|
||||||
*/
|
*/
|
||||||
function useApplyLanguage(language: Language) {
|
function useApplyLanguage(language?: string): void {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
i18n.changeLanguage(language);
|
i18n.changeLanguage(language ?? navigator.language);
|
||||||
}, [language]);
|
}, [language]);
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ describe('LanguagePicker', () => {
|
|||||||
fireEvent.click(trigger);
|
fireEvent.click(trigger);
|
||||||
|
|
||||||
// Should show all language options (using getAllByText since there might be duplicates)
|
// Should show all language options (using getAllByText since there might be duplicates)
|
||||||
|
expect(screen.getAllByText('System').length).toBeGreaterThan(0);
|
||||||
expect(screen.getAllByText('English').length).toBeGreaterThan(0);
|
expect(screen.getAllByText('English').length).toBeGreaterThan(0);
|
||||||
expect(screen.getAllByText('Portuguese').length).toBeGreaterThan(0);
|
expect(screen.getAllByText('Portuguese').length).toBeGreaterThan(0);
|
||||||
expect(screen.getAllByText('Chinese').length).toBeGreaterThan(0);
|
expect(screen.getAllByText('Chinese').length).toBeGreaterThan(0);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Languages } from 'lucide-react';
|
||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
|
||||||
import { useAppContext } from '@/hooks/useAppContext';
|
import { useAppContext } from '@/hooks/useAppContext';
|
||||||
import type { Language } from '@/contexts/AppContext';
|
|
||||||
|
|
||||||
interface LanguagePickerProps {
|
interface LanguagePickerProps {
|
||||||
className?: string;
|
className?: string;
|
||||||
@@ -11,18 +11,24 @@ export function LanguagePicker({ className }: LanguagePickerProps) {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { config, updateConfig } = useAppContext();
|
const { config, updateConfig } = useAppContext();
|
||||||
|
|
||||||
const handleLanguageChange = (language: Language) => {
|
const handleLanguageChange = (language: string) => {
|
||||||
updateConfig((prev) => ({ ...prev, language }));
|
updateConfig((prev) => ({ ...prev, language: language === 'system' ? undefined : language }));
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Select value={config.language} onValueChange={handleLanguageChange}>
|
<Select value={config.language ?? 'system'} onValueChange={handleLanguageChange}>
|
||||||
<SelectTrigger className={className}>
|
<SelectTrigger className={className}>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<SelectValue />
|
<SelectValue />
|
||||||
</div>
|
</div>
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<SelectContent>
|
||||||
|
<SelectItem value="system">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Languages className="h-4 w-4" />
|
||||||
|
{t('systemLanguage')}
|
||||||
|
</div>
|
||||||
|
</SelectItem>
|
||||||
<SelectItem value="en">
|
<SelectItem value="en">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<span className="text-lg">🇺🇸</span>
|
<span className="text-lg">🇺🇸</span>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { createContext } from "react";
|
import { createContext } from "react";
|
||||||
|
|
||||||
export type Theme = "dark" | "light" | "system";
|
export type Theme = "dark" | "light" | "system";
|
||||||
export type Language = "en" | "pt" | "zh";
|
|
||||||
|
|
||||||
export interface AppConfig {
|
export interface AppConfig {
|
||||||
/** Current theme */
|
/** Current theme */
|
||||||
@@ -11,7 +10,7 @@ export interface AppConfig {
|
|||||||
/** Deploy server domain (e.g., "shakespeare.wtf") */
|
/** Deploy server domain (e.g., "shakespeare.wtf") */
|
||||||
deployServer: string;
|
deployServer: string;
|
||||||
/** Selected language */
|
/** Selected language */
|
||||||
language: Language;
|
language?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AppContextType {
|
export interface AppContextType {
|
||||||
|
|||||||
+3
-1
@@ -33,6 +33,7 @@ const resources = {
|
|||||||
english: 'English',
|
english: 'English',
|
||||||
portuguese: 'Portuguese',
|
portuguese: 'Portuguese',
|
||||||
chinese: 'Chinese',
|
chinese: 'Chinese',
|
||||||
|
systemLanguage: 'System',
|
||||||
|
|
||||||
// Theme Options
|
// Theme Options
|
||||||
light: 'Light',
|
light: 'Light',
|
||||||
@@ -283,6 +284,7 @@ const resources = {
|
|||||||
english: 'Inglês',
|
english: 'Inglês',
|
||||||
portuguese: 'Português',
|
portuguese: 'Português',
|
||||||
chinese: 'Chinês',
|
chinese: 'Chinês',
|
||||||
|
systemLanguage: 'Sistema',
|
||||||
|
|
||||||
// Theme Options
|
// Theme Options
|
||||||
light: 'Claro',
|
light: 'Claro',
|
||||||
@@ -533,6 +535,7 @@ const resources = {
|
|||||||
english: 'English',
|
english: 'English',
|
||||||
portuguese: 'Português',
|
portuguese: 'Português',
|
||||||
chinese: '中文',
|
chinese: '中文',
|
||||||
|
systemLanguage: '系统',
|
||||||
|
|
||||||
// Theme Options
|
// Theme Options
|
||||||
light: '浅色',
|
light: '浅色',
|
||||||
@@ -760,7 +763,6 @@ i18n
|
|||||||
.use(initReactI18next)
|
.use(initReactI18next)
|
||||||
.init({
|
.init({
|
||||||
resources,
|
resources,
|
||||||
lng: 'en', // Default language
|
|
||||||
fallbackLng: 'en',
|
fallbackLng: 'en',
|
||||||
interpolation: {
|
interpolation: {
|
||||||
escapeValue: false, // React already escapes values
|
escapeValue: false, // React already escapes values
|
||||||
|
|||||||
Reference in new issue
Block a user