87 lines
3.6 KiB
JavaScript
87 lines
3.6 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import i18n from 'i18next';
|
|
import { initReactI18next } from 'react-i18next';
|
|
import { normalizeLanguage } from './language';
|
|
import { getLanguageEntry } from './catalog';
|
|
|
|
// Load Chinese synchronously; other languages are loaded on demand.
|
|
import zhTranslation from './locales/zh.json';
|
|
|
|
const loadedLanguages = new Set(['zh']);
|
|
|
|
// 可 await:启动流程(i18n/bootstrap.js)要等语言包落库后再渲染,避免首屏先闪回退语言
|
|
export const loadLanguage = async (lng) => {
|
|
if (loadedLanguages.has(lng)) return;
|
|
const entry = getLanguageEntry(lng);
|
|
if (!entry?.load) return;
|
|
|
|
try {
|
|
const translation = await entry.load();
|
|
const data = translation.default || translation;
|
|
const translationData = data.translation || data;
|
|
i18n.addResourceBundle(lng, 'translation', translationData, true, true);
|
|
loadedLanguages.add(lng);
|
|
} catch (error) {
|
|
console.error(`Failed to load language: ${lng}`, error);
|
|
}
|
|
};
|
|
|
|
i18n.use(initReactI18next).init({
|
|
// 初始语言由 i18n/bootstrap.js 在渲染前判定(URL 参数 / 用户选择 / IP 国家 /
|
|
// 浏览器语言 / 默认语言),不再使用浏览器语言检测器;此处只是渲染前的占位。
|
|
lng: 'zh',
|
|
// 不能用 'languageOnly':它会把 zh-TW 截成 zh,繁体包永远不会命中。
|
|
load: 'all',
|
|
resources: { zh: zhTranslation },
|
|
fallbackLng: 'zh',
|
|
// Natural-language keys may contain dots and colons, so keep all keys flat.
|
|
keySeparator: false,
|
|
nsSeparator: false,
|
|
// The locale files carry hundreds of ""-valued keys written by
|
|
// i18next-cli sync as translation TODOs. i18next treats "" as a valid
|
|
// translation by default, which renders those strings as BLANK; treating
|
|
// them as missing falls back to the key itself (the readable Chinese
|
|
// source text) instead.
|
|
// 注:locales/{fr,ru}.json 未登记进 catalog.js(译文覆盖率不足),当前不会被加载。
|
|
returnEmptyString: false,
|
|
interpolation: { escapeValue: false },
|
|
// 语种按需加载:bundle 由 addResourceBundle 异步落库,只有订阅 store 的
|
|
// 'added' 才会在落库后补渲染;仅默认的 bindI18n:'languageChanged' 是同步
|
|
// 触发的,那一刻资源还没到,界面会停在回退语言直到用户再点一次。
|
|
react: { useSuspense: false, bindI18nStore: 'added' },
|
|
});
|
|
|
|
// <html lang> 必须跟着切:泰文词间无空格,浏览器要靠 lang="th" 才会启用 ICU
|
|
// 词典分词,否则整串泰文是不可断原子、窄列必然溢出;lang 还决定字体回退(CJK
|
|
// 与谚文/泰文字形的消歧,日文/繁体汉字字形)和读屏语言。index.html 里的
|
|
// lang="zh" 只是首帧默认值。
|
|
const syncHtmlLang = (lng) => {
|
|
if (typeof document !== 'undefined') document.documentElement.lang = lng;
|
|
};
|
|
|
|
i18n.on('languageChanged', (lng) => {
|
|
const code = normalizeLanguage(lng);
|
|
syncHtmlLang(code);
|
|
loadLanguage(code);
|
|
});
|
|
|
|
export default i18n;
|