mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-24 23:41:10 +02:00
Translations: - Add complete Latvian translations (lv.json) - Remove Chinese translations (zh.json) - Update i18n routing to support en/lv locales - Update LanguageSwitcher component for Latvian - Update LocaleContext to use Latvian messages PR Review Improvements: - Create shared handleSessionExpired() utility in auth-utils.ts - Refactor use-user.ts, use-chats.ts, use-search-space.ts to use shared utility - Add session_expired error message to auth-errors.ts - Consistent redirect to /login?error=session_expired
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
"use client";
|
|
|
|
import type React from "react";
|
|
import { createContext, useContext, useEffect, useState } from "react";
|
|
import enMessages from "../messages/en.json";
|
|
import lvMessages from "../messages/lv.json";
|
|
|
|
type Locale = "en" | "lv";
|
|
|
|
interface LocaleContextType {
|
|
locale: Locale;
|
|
messages: typeof enMessages;
|
|
setLocale: (locale: Locale) => void;
|
|
}
|
|
|
|
const LocaleContext = createContext<LocaleContextType | undefined>(undefined);
|
|
|
|
const LOCALE_STORAGE_KEY = "surfsense-locale";
|
|
|
|
export function LocaleProvider({ children }: { children: React.ReactNode }) {
|
|
// Always start with 'en' to avoid hydration mismatch
|
|
// Then sync with localStorage after mount
|
|
const [locale, setLocaleState] = useState<Locale>("en");
|
|
const [mounted, setMounted] = useState(false);
|
|
|
|
// Get messages based on current locale
|
|
const messages = locale === "lv" ? lvMessages : enMessages;
|
|
|
|
// Load locale from localStorage after component mounts (client-side only)
|
|
useEffect(() => {
|
|
setMounted(true);
|
|
if (typeof window !== "undefined") {
|
|
const stored = localStorage.getItem(LOCALE_STORAGE_KEY);
|
|
if (stored === "lv") {
|
|
setLocaleState("lv");
|
|
}
|
|
}
|
|
}, []);
|
|
|
|
// Update locale and persist to localStorage
|
|
const setLocale = (newLocale: Locale) => {
|
|
setLocaleState(newLocale);
|
|
if (typeof window !== "undefined") {
|
|
localStorage.setItem(LOCALE_STORAGE_KEY, newLocale);
|
|
// Update HTML lang attribute
|
|
document.documentElement.lang = newLocale;
|
|
}
|
|
};
|
|
|
|
// Set HTML lang attribute when locale changes
|
|
useEffect(() => {
|
|
if (typeof window !== "undefined" && mounted) {
|
|
document.documentElement.lang = locale;
|
|
}
|
|
}, [locale, mounted]);
|
|
|
|
return (
|
|
<LocaleContext.Provider value={{ locale, messages, setLocale }}>
|
|
{children}
|
|
</LocaleContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useLocaleContext() {
|
|
const context = useContext(LocaleContext);
|
|
if (context === undefined) {
|
|
throw new Error("useLocaleContext must be used within a LocaleProvider");
|
|
}
|
|
return context;
|
|
}
|