Files
Momento/memento-note/lib/i18n/detect-user-language.ts
Sepehr Ramezani dbd49d6fcb
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m25s
feat: 8 AI providers, rich text editor, agent notifications, UI contrast & font settings
- Add DeepSeek, OpenRouter, Mistral, Z.AI, LM Studio as AI providers
  with editable model names via Combobox in admin settings
- Fix OpenRouter broken by normalizeProvider bug in config.ts
- Convert agent-created notes from Markdown to HTML (TipTap rich text)
- Add Notification model + in-app notifications for agent results
- Agent notification click opens the created note directly
- Add note count display on notebook and inbox headers
- Fix checklist toggle in card view (persist state via localCheckItems)
- Add checklist creation option in tabs/list view (dropdown on + button)
- Fix image description ENOENT error with HTTP fallback
- Improve UI contrast across all themes (input, border, checkbox visibility)
- Add font family setting (Inter vs System Default) in Appearance settings
- Fix CSS font-sans variable conflict (removed dead Geist references)
- Update README with new features and 8 providers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-01 16:14:07 +02:00

102 lines
2.9 KiB
TypeScript

/**
* Detect user's preferred language.
* Priority:
* 1. Most common language among user's notes (DB GROUP BY)
* 2. Browser language hint (passed from server component via Accept-Language)
* 3. Default: 'en'
*/
import { auth } from '@/auth'
import { prisma } from '@/lib/prisma'
import { unstable_cache } from 'next/cache'
import { SupportedLanguage } from './load-translations'
const SUPPORTED_LANGUAGES = new Set(['en', 'fr', 'es', 'de', 'fa', 'it', 'pt', 'ru', 'zh', 'ja', 'ko', 'ar', 'hi', 'nl', 'pl'])
/**
* Parse an Accept-Language header string and find the best matching supported language
*/
export function parseAcceptLanguage(acceptLanguage: string | null): SupportedLanguage | null {
if (!acceptLanguage) return null
// Parse Accept-Language: "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7"
const languages = acceptLanguage
.split(',')
.map(lang => {
const [code, q] = lang.trim().split(';q=')
return { code: code.trim().toLowerCase(), quality: q ? parseFloat(q) : 1.0 }
})
.sort((a, b) => b.quality - a.quality)
for (const { code } of languages) {
if (SUPPORTED_LANGUAGES.has(code)) {
return code as SupportedLanguage
}
const base = code.split('-')[0]
if (SUPPORTED_LANGUAGES.has(base)) {
return base as SupportedLanguage
}
}
return null
}
const getCachedUserLanguage = unstable_cache(
async (userId: string): Promise<SupportedLanguage | null> => {
try {
const result = await prisma.note.groupBy({
by: ['language'],
where: {
userId,
language: { not: null }
},
_sum: { languageConfidence: true },
_count: true,
orderBy: { _sum: { languageConfidence: 'desc' } },
take: 1,
})
if (result.length > 0 && result[0].language) {
const topLanguage = result[0].language as SupportedLanguage
if (SUPPORTED_LANGUAGES.has(topLanguage)) {
return topLanguage
}
}
return null
} catch (error) {
console.error('Error detecting user language from notes:', error)
return null
}
},
['user-language'],
{ tags: ['user-language'] }
)
/**
* Detect user language.
* @param browserLanguageHint - Optional browser language parsed from Accept-Language header.
* Should be passed from server components that have access to headers().
*/
export async function detectUserLanguage(browserLanguageHint?: SupportedLanguage | null): Promise<SupportedLanguage> {
const session = await auth()
if (!session?.user?.id) {
return browserLanguageHint || 'en'
}
// 1. Try to detect from user's notes
const noteLanguage = await getCachedUserLanguage(session.user.id)
if (noteLanguage) {
return noteLanguage
}
// 2. Fall back to browser language hint
if (browserLanguageHint) {
return browserLanguageHint
}
// 3. Default
return 'en'
}