fix: i18n system overhaul and sidebar UI bugs
- Fix LanguageProvider: add RTL support (ar/fa), translation caching, prevent blank flash during load, browser language detection - Fix detect-user-language: extend whitelist from 5 to all 15 languages - Remove hardcoded initialLanguage="fr" from auth layout - Complete fr.json translation (all sections translated from English) - Add missing admin.ai keys to all 13 non-English locales - Translate ai.autoLabels, ai.batchOrganization, memoryEcho sections for all locales - Remove duplicate top-level autoLabels/batchOrganization from en.json - Fix notebook creation: replace window.location.reload() with createNotebookOptimistic + router.refresh() - Fix notebook name truncation in sidebar with min-w-0 - Remove redundant router.refresh() after note creation in page.tsx Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client'
|
||||
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
import { createContext, useContext, useEffect, useState, useCallback, useRef } from 'react'
|
||||
import type { ReactNode } from 'react'
|
||||
import { SupportedLanguage, loadTranslations, getTranslationValue, Translations } from './load-translations'
|
||||
|
||||
@@ -13,18 +13,35 @@ type LanguageContextType = {
|
||||
|
||||
const LanguageContext = createContext<LanguageContextType | undefined>(undefined)
|
||||
|
||||
const RTL_LANGUAGES: SupportedLanguage[] = ['ar', 'fa']
|
||||
|
||||
function updateDocumentDirection(lang: SupportedLanguage) {
|
||||
document.documentElement.lang = lang
|
||||
document.documentElement.dir = RTL_LANGUAGES.includes(lang) ? 'rtl' : 'ltr'
|
||||
}
|
||||
|
||||
export function LanguageProvider({ children, initialLanguage = 'en' }: {
|
||||
children: ReactNode
|
||||
initialLanguage?: SupportedLanguage
|
||||
}) {
|
||||
const [language, setLanguageState] = useState<SupportedLanguage>(initialLanguage)
|
||||
const [translations, setTranslations] = useState<Translations | null>(null)
|
||||
const cacheRef = useRef<Map<SupportedLanguage, Translations>>(new Map())
|
||||
|
||||
// Load translations when language changes
|
||||
// Load translations when language changes (with caching)
|
||||
useEffect(() => {
|
||||
const cached = cacheRef.current.get(language)
|
||||
if (cached) {
|
||||
setTranslations(cached)
|
||||
updateDocumentDirection(language)
|
||||
return
|
||||
}
|
||||
|
||||
const loadLang = async () => {
|
||||
const loaded = await loadTranslations(language)
|
||||
cacheRef.current.set(language, loaded)
|
||||
setTranslations(loaded)
|
||||
updateDocumentDirection(language)
|
||||
}
|
||||
loadLang()
|
||||
}, [language])
|
||||
@@ -34,7 +51,6 @@ export function LanguageProvider({ children, initialLanguage = 'en' }: {
|
||||
const saved = localStorage.getItem('user-language') as SupportedLanguage
|
||||
if (saved) {
|
||||
setLanguageState(saved)
|
||||
document.documentElement.lang = saved
|
||||
} else {
|
||||
// Auto-detect from browser language
|
||||
const browserLang = navigator.language.split('-')[0] as SupportedLanguage
|
||||
@@ -43,35 +59,44 @@ export function LanguageProvider({ children, initialLanguage = 'en' }: {
|
||||
if (supportedLangs.includes(browserLang)) {
|
||||
setLanguageState(browserLang)
|
||||
localStorage.setItem('user-language', browserLang)
|
||||
document.documentElement.lang = browserLang
|
||||
}
|
||||
}
|
||||
}, [])
|
||||
|
||||
const setLanguage = (lang: SupportedLanguage) => {
|
||||
const setLanguage = useCallback((lang: SupportedLanguage) => {
|
||||
setLanguageState(lang)
|
||||
localStorage.setItem('user-language', lang)
|
||||
// Update HTML lang attribute for font styling
|
||||
document.documentElement.lang = lang
|
||||
}
|
||||
updateDocumentDirection(lang)
|
||||
}, [])
|
||||
|
||||
const t = (key: string, params?: Record<string, string | number>) => {
|
||||
const t = useCallback((key: string, params?: Record<string, string | number>) => {
|
||||
if (!translations) return key
|
||||
|
||||
let value: any = getTranslationValue(translations, key)
|
||||
|
||||
// Replace parameters like {count}, {percentage}, etc.
|
||||
if (params) {
|
||||
if (params && typeof value === 'string') {
|
||||
Object.entries(params).forEach(([param, paramValue]) => {
|
||||
value = value.replace(`{${param}}`, String(paramValue))
|
||||
})
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
return typeof value === 'string' ? value : key
|
||||
}, [translations])
|
||||
|
||||
// During initial load, show children with the initial language as fallback
|
||||
// to prevent blank flash
|
||||
if (!translations) {
|
||||
return null // Show loading state if needed
|
||||
return (
|
||||
<LanguageContext.Provider value={{
|
||||
language: initialLanguage,
|
||||
setLanguage,
|
||||
t: (key: string) => key,
|
||||
translations: {} as Translations
|
||||
}}>
|
||||
{children}
|
||||
</LanguageContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -49,7 +49,7 @@ export async function detectUserLanguage(): Promise<SupportedLanguage> {
|
||||
if (sortedLanguages.length > 0) {
|
||||
const topLanguage = sortedLanguages[0][0] as SupportedLanguage
|
||||
// Verify it's a supported language
|
||||
if (['fr', 'en', 'es', 'de', 'fa'].includes(topLanguage)) {
|
||||
if (['en', 'fr', 'es', 'de', 'fa', 'it', 'pt', 'ru', 'zh', 'ja', 'ko', 'ar', 'hi', 'nl', 'pl'].includes(topLanguage)) {
|
||||
return topLanguage
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user