-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathi18n.js
More file actions
63 lines (54 loc) · 1.73 KB
/
i18n.js
File metadata and controls
63 lines (54 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { execSync } from 'child_process';
import zh from './locales/zh.js';
import en from './locales/en.js';
const locales = { en, zh };
// 只区分 zh / en:默认英文,检测到中文则用中文
export const detectLocale = () => {
// 1. 显式环境变量强制覆盖:DUAL_SUBTITLE_LANG=zh/en
const override = (process.env.DUAL_SUBTITLE_LANG || '').toLowerCase();
if (override === 'zh' || override === 'en') {
return override;
}
// 2. 终端相关环境变量
const env =
process.env.LC_ALL ||
process.env.LC_MESSAGES ||
process.env.LANG ||
'';
const envLower = String(env).toLowerCase();
if (envLower.includes('zh')) return 'zh';
// 3. Node Intl 默认 locale
const intl = Intl.DateTimeFormat().resolvedOptions().locale || '';
const intlLower = String(intl).toLowerCase();
if (intlLower.startsWith('zh')) return 'zh';
// 4. macOS 上读取系统首选语言(AppleLanguages)
if (process.platform === 'darwin') {
try {
const out = execSync('defaults read -g AppleLanguages', { encoding: 'utf8' });
if (String(out).toLowerCase().includes('zh')) {
return 'zh';
}
} catch (e) {
// 忽略读取失败,继续走默认逻辑
}
}
// 5. 默认英文
return 'en';
};
const activeLocale = detectLocale();
const dict = locales[activeLocale] || locales.en;
/**
* t('key', params) -> string
* key 对应 locales 里的字段;字段可为字符串或 (params) => string
*/
export const t = (key, params = {}) => {
const value = dict[key];
if (typeof value === 'function') {
return value(params);
}
if (typeof value === 'string') {
return value;
}
// 未配置的 key,回退为 key 本身,方便在开发时发现问题
return key;
};