- Reorganize project structure with new electron and shared directories - Add comprehensive i18n support with Chinese, English, and Japanese locales - Update build configurations and TypeScript paths for new structure - Add various UI components including chat interface and task management - Include Windows release binaries and localization files - Update dependencies and fix import paths throughout the codebase
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { SUPPORTED_LANGUAGE_CODES, type LanguageCode } from './constants';
|
||
|
||
// 创建语言代码集合用于快速查找
|
||
const SUPPORTED_LANGUAGE_CODE_SET = new Set<string>(SUPPORTED_LANGUAGE_CODES);
|
||
|
||
/**
|
||
* 标准化语言代码(处理 zh-CN、zh_TW 等变体)
|
||
*/
|
||
export function normalizeLocale(locale: string | null | undefined): string {
|
||
return locale?.trim().toLowerCase().replaceAll('_', '-') ?? '';
|
||
}
|
||
|
||
/**
|
||
* 解析支持的语言代码
|
||
* @param locale 原始语言代码(如 'zh-CN', 'en-US')
|
||
* @param fallback 回退语言代码,默认为 'zh'
|
||
* @returns 支持的语言代码
|
||
*/
|
||
export function resolveSupportedLanguage(
|
||
locale: string | null | undefined,
|
||
fallback: LanguageCode = 'zh',
|
||
): LanguageCode {
|
||
const normalizedLocale = normalizeLocale(locale);
|
||
if (!normalizedLocale) return fallback;
|
||
|
||
const [baseLanguage] = normalizedLocale.split('-');
|
||
return SUPPORTED_LANGUAGE_CODE_SET.has(baseLanguage)
|
||
? (baseLanguage as LanguageCode)
|
||
: fallback;
|
||
}
|
||
|
||
/**
|
||
* 检测系统语言
|
||
*/
|
||
export function detectSystemLanguage(): LanguageCode {
|
||
if (typeof navigator === 'undefined') return 'zh';
|
||
return resolveSupportedLanguage(navigator.language);
|
||
} |