- Updated `generateUUID` function for improved readability and performance. - Deleted `logger.ts`, `other.ts`, `request.ts`, `storage.ts`, `tansParams.ts`, and `validate.ts` as they were no longer needed. - Simplified TypeScript configuration by removing unnecessary paths and aliases. - Enhanced Vite configuration for better project structure and maintainability.
25 lines
874 B
TypeScript
25 lines
874 B
TypeScript
import { SUPPORTED_LANGUAGE_CODES } from './constants';
|
|
import type { LanguageCode } from '../types/runtime';
|
|
|
|
const SUPPORTED_LANGUAGE_SET = new Set<string>(SUPPORTED_LANGUAGE_CODES);
|
|
|
|
export function normalizeLocale(locale: string | null | undefined): string {
|
|
return locale?.trim().toLowerCase().split('_').join('-') ?? '';
|
|
}
|
|
|
|
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_SET.has(baseLanguage) ? (baseLanguage as LanguageCode) : fallback;
|
|
}
|
|
|
|
export function detectSystemLanguage(): LanguageCode {
|
|
if (typeof navigator === 'undefined') return 'zh';
|
|
return resolveSupportedLanguage(navigator.language);
|
|
}
|