Files
store_ai_extension/src/popup/hook/use-i18n.ts
2026-05-12 17:58:27 +08:00

113 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {computed, onMounted, ref} from 'vue';
/**
* Popup 多语言(仅影响 Popup 文案,不改平台配置/爬取数据)。
* 中文备注:用户要求把切换入口放在 Popup 底部版本号附近,因此这里提供一个轻量的本地 i18n。
*/
/** chrome.storage.local 中保存语言的 key */
const POPUP_UI_LANG_KEY = 'popupUiLang';
/** 目前仅提供中英两种,后续可在这里继续扩展 */
export type PopupUiLang = 'zh-CN' | 'en';
/** 文案字典key 统一用英文标识,方便维护 */
const DICT: Record<PopupUiLang, Record<string, string>> = {
'zh-CN': {
please_login: '请先登录后再开始爬取',
sign_in: '登录',
sign_out: '退出登录',
platform_select: '平台选择',
scan_now: '立即扫描',
opening: '正在打开…',
scanning: '扫描中',
paused: '已暂停',
done: '已完成',
failed: '失败',
show_tab: '显示页面',
continue_now: '继续',
cancel: '取消',
step_done: '已完成',
step_failed: '失败',
step_running: '进行中',
step_pending: '等待中',
language: '语言',
lang_zh: '中文',
lang_en: 'English',
},
en: {
please_login: 'Please sign in before scanning.',
sign_in: 'Sign in',
sign_out: 'Sign out',
platform_select: 'Platform',
scan_now: 'Scan now',
opening: 'Opening…',
scanning: 'Scanning',
paused: 'Paused',
done: 'Done',
failed: 'Failed',
show_tab: 'Show tab',
continue_now: 'Continue now',
cancel: 'Cancel',
step_done: 'Completed',
step_failed: 'Failed',
step_running: 'Running',
step_pending: 'Pending',
language: 'Language',
lang_zh: '中文',
lang_en: 'English',
},
};
/**
* Popup 内使用的 i18n composable
*/
export function useI18n() {
const uiLang = ref<PopupUiLang>('zh-CN');
// 中文备注:从本地存储恢复用户上次选择
onMounted(async () => {
try {
if (typeof chrome === 'undefined' || !chrome.storage?.local) return;
const res = await chrome.storage.local.get(POPUP_UI_LANG_KEY);
const stored = res?.[POPUP_UI_LANG_KEY];
if (stored === 'zh-CN' || stored === 'en') {
uiLang.value = stored;
}
} catch {
// ignore
}
});
const t = computed(() => {
return (key: string) => {
return DICT[uiLang.value]?.[key] ?? DICT.en[key] ?? key;
};
});
/**
* 设置语言并持久化
*/
async function setUiLang(lang: PopupUiLang) {
uiLang.value = lang;
try {
if (typeof chrome === 'undefined' || !chrome.storage?.local) return;
await chrome.storage.local.set({[POPUP_UI_LANG_KEY]: lang});
} catch {
// ignore
}
}
return {
uiLang,
t: t.value,
setUiLang,
// 中文备注:给 template 直接使用的语言选项
langOptions: [
{value: 'zh-CN' as const, labelKey: 'lang_zh'},
{value: 'en' as const, labelKey: 'lang_en'},
],
};
}