我尝试从React Native中的API加载翻译JSON文件并对其进行解析,但是由于parse方法从不调用,因此加载似乎不起作用。
function initI18Next(onInit) {
return (
i18n
.use(initReactI18next)
.use(XHR)
.init({
...initOptions,
backend : backendOptions
}).then(onInit())
)}
initOptions:
export const initOptions = {
fallbackLng : 'es',
defaultNS : 'translation',
lng : 'es',
debug: true,
keySeparator : false,
initImmediate: false,
preload : ['en', 'es'],
react: {
wait: true,
useSuspense: false
},
interpolation: {
escapeValue: true // not needed for react as it escapes by default
},
resources : i18nResources
}
后端选项:
export const backendOptions = {
loadPath: 'http://www.mocky.io/v2/5ecd0fe73200006400236655.json',
parse(data) {
console.log('data', data);
return data;
}
};
i18next-xhr-backend已过时,请改用i18next-http-backend ...然后尝试将i18next调试选项设置为true,并查看开发者控制台日志。
Btw:加载路径对我来说很奇怪。应该更像这样:https://github.com/i18next/i18next-http-backend#backend-options