feat(i18n): add fallback toast and cached bundle fallback logic

1. add global i18n fallback toast on lang:fallback event, only show once per process
2. add getCachedBundleAny to retrieve expired cache for boot fallback
3. add cache fallback logic when remote i18n bundle load fails
This commit is contained in:
2026-07-02 16:33:22 +08:00
parent c86e688e47
commit e1ce2ae268
3 changed files with 102 additions and 8 deletions

22
App.vue
View File

@@ -115,7 +115,27 @@
uni.$on && uni.$on('uniacid:changed', () => {
try { ThemeOverride.onUniacidChange(); } catch (e) {}
});
// lang:fallback / lang:remote-status / theme:fallback / theme:remote-status 不在 App.vue 监听,留给业务按需订阅
// 监听国际化远端拉取失败: 给用户英文 toast 提示
// 触发时机: lang.js setRemoteStatus('fail', ...) 时
// 设计: 用 setRemoteStatus 内部归一化后的 reason 决定文案
// - 'timeout' / 'network' / 'http' / 'parse' / 'exception' 都弹同一句英文说明
// - 'unknown' 不弹(可能是业务主动设置,不是真失败)
// 防刷: 同一进程内只弹一次(避免重试时反复刷屏)
if (!globalThis.__i18nFallbackToastShown) {
uni.$on && uni.$on('lang:fallback', (detail) => {
const r = detail && detail.reason;
if (r === 'unknown' || r === undefined || r === null) return;
if (globalThis.__i18nFallbackToastShown) return;
globalThis.__i18nFallbackToastShown = true;
try {
uni.showToast({
title: 'Failed to load i18n. Please check your network and try again.',
icon: 'none',
duration: 4000,
});
} catch (e) {}
});
}
uni.hideTabBar();
// #ifdef MP