- 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>
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
/**
|
|
* Detect user's preferred language from their existing notes
|
|
* Analyzes language distribution across all user's notes
|
|
*/
|
|
|
|
import { auth } from '@/auth'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { SupportedLanguage } from './load-translations'
|
|
|
|
export async function detectUserLanguage(): Promise<SupportedLanguage> {
|
|
const session = await auth()
|
|
|
|
// Default to English for non-logged-in users
|
|
if (!session?.user?.id) {
|
|
return 'en'
|
|
}
|
|
|
|
try {
|
|
// Get all user's notes with detected languages
|
|
const notes = await prisma.note.findMany({
|
|
where: {
|
|
userId: session.user.id,
|
|
language: { not: null }
|
|
},
|
|
select: {
|
|
language: true,
|
|
languageConfidence: true
|
|
}
|
|
})
|
|
|
|
if (notes.length === 0) {
|
|
return 'en' // Default for new users
|
|
}
|
|
|
|
// Count language occurrences weighted by confidence
|
|
const languageScores: Record<string, number> = {}
|
|
|
|
for (const note of notes) {
|
|
if (note.language) {
|
|
const confidence = note.languageConfidence || 0.8
|
|
languageScores[note.language] = (languageScores[note.language] || 0) + confidence
|
|
}
|
|
}
|
|
|
|
// Find language with highest score
|
|
const sortedLanguages = Object.entries(languageScores)
|
|
.sort(([, a], [, b]) => b - a)
|
|
|
|
if (sortedLanguages.length > 0) {
|
|
const topLanguage = sortedLanguages[0][0] as SupportedLanguage
|
|
// Verify it's a supported language
|
|
if (['en', 'fr', 'es', 'de', 'fa', 'it', 'pt', 'ru', 'zh', 'ja', 'ko', 'ar', 'hi', 'nl', 'pl'].includes(topLanguage)) {
|
|
return topLanguage
|
|
}
|
|
}
|
|
|
|
return 'en'
|
|
} catch (error) {
|
|
console.error('Error detecting user language:', error)
|
|
return 'en'
|
|
}
|
|
}
|