i18next无法加载额外YAML格式翻译文件的问题求助
i18next无法加载额外YAML格式翻译文件的问题求助
我尝试了各种方法给我的应用加载额外的翻译文件,但始终不见效果。我开启了调试模式,控制台显示翻译文件已经加载完成,但界面上完全看不到任何翻译生效的迹象。
以下是我的i18n初始化代码:
import { Meteor } from 'meteor/meteor'; import { Tracker } from 'meteor/tracker'; import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import Backend from 'i18next-http-backend'; import HttpApi from 'i18next-http-backend'; import LanguageDetector from 'i18next-browser-languagedetector'; import yaml from 'js-yaml'; import Hosts from '../api/hosts/host'; const defaultLang = 'en'; const allLangs = [defaultLang, 'sv', 'tr']; const options = { allowMultiLoading: true, backend: { loadPath: '/i18n/{{lng}}/{{ns}}.yml', parse: function (data) { return yaml.load(data); }, }, debug: true, defaultNS: 'common', fallbackLng: defaultLang, lng: defaultLang, load: 'languageOnly', // we only provide en, de -> no region specific locals like en-US, de-DE // have a common namespace used around the full app ns: [ 'common', 'accounts', 'members', 'hosts', 'admin', 'activities', 'processes', 'calendar', 'resources', ], preload: allLangs, saveMissing: true, supportedLngs: allLangs, useSuspense: process && !process.release, }; // for browser use http backend to load translations and browser lng detector if (process && !process.release) { i18n.use(Backend).use(initReactI18next).use(LanguageDetector).use(HttpApi); } // initialize if not already initialized if (!i18n.isInitialized) { i18n.init(options); // check & set lang for user(logged) or host prefences Tracker.autorun(() => { let preferedLang = defaultLang; if (Meteor.userId()) { const handler = Meteor.subscribe('me'); if (handler.ready()) { preferedLang = Meteor.user()?.lang; } } else { const handler = Meteor.subscribe('currentHost'); if (handler.ready()) { preferedLang = Hosts.findOne().settings.lang; } } i18n.changeLanguage(preferedLang); }); } export default i18n; export { defaultLang };
还有个很诡异的现象:当我把fallbackLng设置为allLangs时,瑞典语(sv)的翻译能正常工作,但土耳其语(tr)还是没反应;而当fallbackLng设为defaultLang时,土耳其语和瑞典语都不会出现在i18n.languages的选项列表里...
真心希望能得到大家的帮助!
备注:内容来源于stack exchange,提问作者Emo
相关产品推荐
相关产品推荐

