Auto-detect LLM output language from system locale on first startup

This commit is contained in:
Alexander Farber 2025-12-13 15:19:55 +01:00
parent a92be72e88
commit 4d9f25e9fe
No known key found for this signature in database
5 changed files with 259 additions and 1 deletions

View file

@ -46,17 +46,27 @@ const getLocalePath = (
return path.join(baseDir, `${lang}.js`);
};
// Supported locale codes mapped to English language names
const LOCALE_TO_LANGUAGE_NAME: Record<string, string> = {
zh: 'Chinese',
en: 'English',
ru: 'Russian',
de: 'German',
};
// Language detection
export function detectSystemLanguage(): SupportedLanguage {
const envLang = process.env['QWEN_CODE_LANG'] || process.env['LANG'];
if (envLang?.startsWith('zh')) return 'zh';
if (envLang?.startsWith('en')) return 'en';
if (envLang?.startsWith('ru')) return 'ru';
if (envLang?.startsWith('de')) return 'de';
try {
const locale = Intl.DateTimeFormat().resolvedOptions().locale;
if (locale.startsWith('zh')) return 'zh';
if (locale.startsWith('ru')) return 'ru';
if (locale.startsWith('de')) return 'de';
} catch {
// Fallback to default
}
@ -64,6 +74,14 @@ export function detectSystemLanguage(): SupportedLanguage {
return 'en';
}
/**
* Maps a locale code to its English language name.
* Used for LLM output language instructions.
*/
export function getLanguageNameFromLocale(locale: SupportedLanguage): string {
return LOCALE_TO_LANGUAGE_NAME[locale] || 'English';
}
// Translation loading
async function loadTranslationsAsync(
lang: SupportedLanguage,