-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18n.js
More file actions
109 lines (99 loc) · 3.27 KB
/
Copy pathi18n.js
File metadata and controls
109 lines (99 loc) · 3.27 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Tiny i18n layer shared by popup + content scripts.
*
* Source of truth is the existing _locales/<locale>/messages.json files
* (also used by manifest __MSG__). At runtime we fetch the active locale's
* JSON from chrome.runtime.getURL and cache it.
*
* Active locale resolution:
* 1. user-selected override in chrome.storage.sync ('cgpt-sidebar-locale')
* 2. browser UI language (chrome.i18n.getUILanguage), mapped to a
* supported locale
* 3. fall back to 'en'
*
* Exposes window.__veilbar = { init, t, locale, setLocale, onChange, SUPPORTED }.
*/
(() => {
const SUPPORTED = ['en', 'zh_CN', 'ja', 'hi'];
const STORAGE_KEY = 'cgpt-sidebar-locale';
let activeLocale = 'en';
const cache = {};
const listeners = [];
function detectFromUI() {
const ui = (chrome.i18n && chrome.i18n.getUILanguage
? chrome.i18n.getUILanguage()
: 'en'
).toLowerCase();
if (ui.startsWith('zh')) return 'zh_CN';
if (ui.startsWith('ja')) return 'ja';
if (ui.startsWith('hi')) return 'hi';
return 'en';
}
async function loadMessages(locale) {
if (cache[locale]) return cache[locale];
try {
const url = chrome.runtime.getURL(`_locales/${locale}/messages.json`);
const r = await fetch(url);
if (!r.ok) return null;
const data = await r.json();
cache[locale] = data;
return data;
} catch {
return null;
}
}
async function init() {
try {
const r = await chrome.storage.sync.get(STORAGE_KEY);
const stored = r[STORAGE_KEY];
if (stored && stored !== 'auto' && SUPPORTED.includes(stored)) {
activeLocale = stored;
} else {
activeLocale = detectFromUI();
}
} catch {
activeLocale = detectFromUI();
}
await loadMessages(activeLocale);
if (activeLocale !== 'en') await loadMessages('en');
return activeLocale;
}
function t(key, substitution) {
const dict = cache[activeLocale];
let entry = dict && dict[key];
if (!entry) entry = (cache.en || {})[key];
if (!entry) return key;
let msg = entry.message;
if (substitution != null) {
msg = msg.replace(/\$[A-Za-z]+\$/g, () => substitution);
}
return msg;
}
function locale() { return activeLocale; }
async function setLocale(newLocale) {
if (newLocale === 'auto') {
activeLocale = detectFromUI();
try { await chrome.storage.sync.remove(STORAGE_KEY); } catch {}
} else if (SUPPORTED.includes(newLocale)) {
activeLocale = newLocale;
try { await chrome.storage.sync.set({ [STORAGE_KEY]: newLocale }); } catch {}
} else {
return;
}
await loadMessages(activeLocale);
listeners.forEach(cb => { try { cb(activeLocale); } catch {} });
}
function onChange(cb) { listeners.push(cb); }
chrome.storage.onChanged.addListener(async (changes, area) => {
if (area !== 'sync' || !changes[STORAGE_KEY]) return;
const newVal = changes[STORAGE_KEY].newValue;
if (newVal == null || newVal === 'auto') {
activeLocale = detectFromUI();
} else if (SUPPORTED.includes(newVal)) {
activeLocale = newVal;
}
await loadMessages(activeLocale);
listeners.forEach(cb => { try { cb(activeLocale); } catch {} });
});
window.__veilbar = { init, t, locale, setLocale, onChange, SUPPORTED };
})();