I18n: infrastruktura w panelu admina (i18next+react-i18next, detekcja jezyka PL/EN, przelacznik, testy przymuszone do PL)

This commit is contained in:
tisachi
2026-10-17 09:58:42 +02:00
parent 807d2151a2
commit 3a593feb82
19 changed files with 316 additions and 8 deletions

View File

@@ -18,7 +18,7 @@ import {
import { useAuth } from '../store/auth';
import { useIsTrainer } from '../api/queries';
import { APP_URL } from '../config';
import { Button, ThemeToggle } from './ui';
import { Button, ThemeToggle, LanguageToggle } from './ui';
import { Topbar } from '../topbar';
import { Brandmark } from './Brandmark';
import { Avatar } from './Avatar';
@@ -207,6 +207,7 @@ export default function Layout() {
</div>
<div className="flex items-center gap-2">
<ThemeToggle />
<LanguageToggle />
<Button variant="secondary" size="sm" className="flex-1" onClick={doLogout}>
<LogOut className="w-4 h-4" />
Wyloguj
@@ -220,6 +221,7 @@ export default function Layout() {
<div className="mt-auto flex flex-col items-center gap-2.5 border-t border-line px-2 pb-4 pt-3">
<Avatar name={fullName} size={32} />
<ThemeToggle />
<LanguageToggle />
<button
type="button"
onClick={doLogout}

View File

@@ -22,6 +22,7 @@ export * from './ui/typography';
export * from './ui/content';
export * from './ui/dialogs';
export * from './ui/theme-toggle';
export * from './ui/language-toggle';
/* Button */
export type ButtonVariant = 'primary' | 'secondary' | 'danger' | 'ghost';
@@ -53,7 +54,11 @@ export function Badge({
className?: string;
children: ReactNode;
}) {
return <UIBadge color={color} className={className}>{children}</UIBadge>;
return (
<UIBadge color={color} className={className}>
{children}
</UIBadge>
);
}
/* Form fields */

View File

@@ -0,0 +1,22 @@
import { Languages } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { changeLanguage } from '../../i18n';
import type { Language } from '../../i18n/language';
const OTHER: Record<Language, Language> = { pl: 'en', en: 'pl' };
export function LanguageToggle({ className = '' }: { className?: string }) {
const { t, i18n } = useTranslation();
const current: Language = i18n.language === 'en' ? 'en' : 'pl';
const next = OTHER[current];
return (
<button
onClick={() => changeLanguage(next)}
aria-label={`${t('common:toggleLanguage')} (${current.toUpperCase()} → ${next.toUpperCase()})`}
title={`${current.toUpperCase()} → ${next.toUpperCase()}`}
className={`inline-flex items-center justify-center p-2.5 rounded-[10px] bg-card border border-line text-dim shadow-card hover:text-ink hover:border-accent/45 cursor-pointer transition-all duration-150 ${className}`.trim()}
>
<Languages className="w-5 h-5" />
</button>
);
}

View File

@@ -1,13 +1,16 @@
import { Moon, Sun } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { useTheme } from '../../store/theme';
export function ThemeToggle({ className = '' }: { className?: string }) {
const { theme, toggle } = useTheme();
const { t } = useTranslation();
const label = t('common:toggleTheme');
return (
<button
onClick={toggle}
aria-label="Przełącz motyw"
title="Przełącz motyw"
aria-label={label}
title={label}
className={`inline-flex items-center justify-center p-2.5 rounded-[10px] bg-card border border-line text-dim shadow-card hover:text-ink hover:border-accent/45 cursor-pointer transition-all duration-150 ${className}`.trim()}
>
{theme === 'dark' ? <Sun className="w-5 h-5" /> : <Moon className="w-5 h-5" />}

47
admin/src/i18n/index.ts Normal file
View File

@@ -0,0 +1,47 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import {
applyDocumentLanguage,
detectInitialLanguage,
setStoredLanguage,
type Language,
} from './language';
import plCommon from './locales/pl/common.json';
import plAuth from './locales/pl/auth.json';
import plErrors from './locales/pl/errors.json';
import enCommon from './locales/en/common.json';
import enAuth from './locales/en/auth.json';
import enErrors from './locales/en/errors.json';
export const NAMESPACES = ['common', 'auth', 'errors'] as const;
export type Namespace = (typeof NAMESPACES)[number];
const resources = {
pl: { common: plCommon, auth: plAuth, errors: plErrors },
en: { common: enCommon, auth: enAuth, errors: enErrors },
} as const;
const initial = detectInitialLanguage();
applyDocumentLanguage(initial);
void i18n.use(initReactI18next).init({
lng: initial,
fallbackLng: 'pl',
supportedLngs: ['pl', 'en'],
defaultNS: 'common',
ns: [...NAMESPACES],
resources,
interpolation: { escapeValue: false },
react: { useSuspense: false },
});
i18n.on('languageChanged', (lng) => applyDocumentLanguage(lng === 'en' ? 'en' : 'pl'));
export function changeLanguage(lang: Language) {
setStoredLanguage(lang);
void i18n.changeLanguage(lang);
}
export { resolveLocaleTag } from './language';
export { i18n };
export default i18n;

View File

@@ -0,0 +1,39 @@
export type Language = 'pl' | 'en';
const STORAGE_KEY = 'fitos-lang';
export function getStoredLanguage(): Language | null {
try {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved === 'pl' || saved === 'en') return saved;
} catch {
// ignore
}
return null;
}
export function setStoredLanguage(lang: Language) {
try {
localStorage.setItem(STORAGE_KEY, lang);
} catch {
// ignore
}
}
/* localStorage → navigator.language (en* → en) → pl. */
export function detectInitialLanguage(): Language {
const stored = getStoredLanguage();
if (stored) return stored;
const nav = typeof navigator !== 'undefined' ? navigator.language || '' : '';
if (nav.toLowerCase().startsWith('en')) return 'en';
return 'pl';
}
/* i18n.language ('pl'|'en') → tag do Intl/toLocale*. */
export function resolveLocaleTag(lang: string): string {
return lang === 'en' ? 'en-US' : 'pl-PL';
}
export function applyDocumentLanguage(lang: Language) {
document.documentElement.lang = lang;
}

View File

@@ -0,0 +1,16 @@
{
"email": "Email",
"password": "Password",
"rememberMe": "Remember me",
"login": "Log in",
"loginLoading": "Logging in…",
"claimTitle": "The whole platform in one panel.",
"claimSubtitle": "For the platform administrator.",
"scopeLabel": "Panel scope",
"scopeAccountsTitle": "Accounts and access",
"scopeAccountsDesc": "Users, roles, memberships.",
"scopeGymsTitle": "Gyms",
"scopeGymsDesc": "Public and private, owners, trainers.",
"scopeCatalogTitle": "Catalogs",
"scopeCatalogDesc": "Exercises, equipment, focus areas."
}

View File

@@ -0,0 +1,10 @@
{
"appName": "FitOS Admin",
"cancel": "Cancel",
"confirm": "Confirm",
"save": "Save",
"close": "Close",
"closeNotification": "Close notification",
"toggleTheme": "Toggle theme",
"toggleLanguage": "Change language"
}

View File

@@ -0,0 +1,19 @@
{
"BAD_REQUEST": "Invalid request.",
"UNAUTHORIZED": "Session expired — log in again.",
"FORBIDDEN": "You don't have permission for this operation.",
"NOT_FOUND": "Resource not found.",
"CONFLICT": "The change conflicts with existing data.",
"VALIDATION_ERROR": "Please correct the form data.",
"INTERNAL_ERROR": "A server error occurred. Try again.",
"RATE_LIMITED": "Too many attempts. Try again later.",
"GONE": "Invitation expired.",
"UPLOAD_FAILED": "Failed to store files.",
"ACCOUNT_SUSPENDED": "Account suspended.",
"ACCOUNT_SUSPENDED_until": "Account suspended until {{date}}.",
"ACCOUNT_BANNED": "Account blocked by an administrator.",
"reasonSuffix": " Reason: {{reason}}",
"loginFailed": "Login failed",
"saveFailed": "Failed to save",
"deleteFailed": "Failed to delete"
}

View File

@@ -0,0 +1,16 @@
{
"email": "E-mail",
"password": "Hasło",
"rememberMe": "Zapamiętaj mnie",
"login": "Zaloguj",
"loginLoading": "Logowanie…",
"claimTitle": "Cała platforma w jednym panelu.",
"claimSubtitle": "Dla administratora platformy.",
"scopeLabel": "Zakres panelu",
"scopeAccountsTitle": "Konta i dostęp",
"scopeAccountsDesc": "Użytkownicy, role, członkostwa.",
"scopeGymsTitle": "Siłownie",
"scopeGymsDesc": "Publiczne i prywatne, właściciele, trenerzy.",
"scopeCatalogTitle": "Słowniki",
"scopeCatalogDesc": "Ćwiczenia, urządzenia, obszary skupienia."
}

View File

@@ -0,0 +1,10 @@
{
"appName": "FitOS Admin",
"cancel": "Anuluj",
"confirm": "Potwierdź",
"save": "Zapisz",
"close": "Zamknij",
"closeNotification": "Zamknij powiadomienie",
"toggleTheme": "Przełącz motyw",
"toggleLanguage": "Zmień język"
}

View File

@@ -0,0 +1,19 @@
{
"BAD_REQUEST": "Nieprawidłowe żądanie.",
"UNAUTHORIZED": "Sesja wygasła — zaloguj się ponownie.",
"FORBIDDEN": "Nie masz uprawnień do tej operacji.",
"NOT_FOUND": "Nie znaleziono zasobu.",
"CONFLICT": "Zmiana koliduje z istniejącymi danymi.",
"VALIDATION_ERROR": "Popraw dane w formularzu.",
"INTERNAL_ERROR": "Wystąpił błąd serwera. Spróbuj ponownie.",
"RATE_LIMITED": "Zbyt wiele prób. Spróbuj później.",
"GONE": "Zaproszenie wygasło.",
"UPLOAD_FAILED": "Nie udało się zapisać plików.",
"ACCOUNT_SUSPENDED": "Konto zawieszone.",
"ACCOUNT_SUSPENDED_until": "Konto zawieszone do {{date}}.",
"ACCOUNT_BANNED": "Konto zablokowane przez administratora.",
"reasonSuffix": " Powód: {{reason}}",
"loginFailed": "Błąd logowania",
"saveFailed": "Błąd zapisu",
"deleteFailed": "Błąd usuwania"
}

View File

@@ -11,6 +11,7 @@ import '@fontsource/space-grotesk/500.css';
import '@fontsource/space-grotesk/600.css';
import '@fontsource/space-grotesk/700.css';
import App from './App';
import './i18n';
import './styles.css';
const queryClient = new QueryClient({

View File

@@ -2,7 +2,15 @@ import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Building2, Library, Users } from 'lucide-react';
import { useAuth } from '../store/auth';
import { Button, Card, ErrorText, Field, Input, ThemeToggle } from '../components/ui';
import {
Button,
Card,
ErrorText,
Field,
Input,
ThemeToggle,
LanguageToggle,
} from '../components/ui';
import { Brandmark } from '../components/Brandmark';
const REMEMBER_KEY = 'fitos-remembered-email';
@@ -34,7 +42,10 @@ export default function Login() {
return (
<div className="relative flex min-h-screen items-center justify-center p-4">
<ThemeToggle className="fixed right-4 top-4 z-10" />
<div className="fixed right-4 top-4 z-10 flex items-center gap-2">
<ThemeToggle />
<LanguageToggle />
</div>
<div className="grid w-full max-w-3xl overflow-hidden rounded-2xl border border-line bg-card shadow-card md:grid-cols-2">
<div
className="relative flex flex-col gap-3.5 overflow-hidden border-b border-line p-7 md:border-b-0 md:border-r md:p-8"