React I18N-从API加载多种语言
我的应用程序将语言存储在数据库中, API管理客户端应根据API请求使用的语言。
API返回具有以下结构的JSON:
{
"en":{...translations},
"cn":{...translations},
"jp":{...translations},
}
目前我以这种方式初始化I18N,
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import HttpBackend from "i18next-http-backend";
import LanguageDetector from "i18next-browser-languagedetector";
import { languageService } from "services";
const fallbackLng = ["en"];
const { getDictionary } = languageService;
i18n
.use(HttpBackend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng,
backend: {
allowMultiLoading: true,
request: (options: any, url: any, payload: any, callback: any) => {
try {
getDictionary().then(res => {
callback(null, {
data: res,
status: 200
});
});
} catch (e) {
console.error(e);
callback(null, {
status: 500
});
}
},
}
});
export const i18nextConfig = i18n;
但是由此产生的I18N存储,而不是
store: {
data: {
en:{...translation },
cn:{...translation },
}
}
它的
store: {
data: {
en:{
translation:{
"en":{...translation},
"cn":{...translation}
}
}
}
}
我一直在阅读文档,但在这种情况下似乎还不清楚。
my application stores the languages in a Database and the API manages which languages the client should use depending on the API request.
the api returns an JSON with the following structure:
{
"en":{...translations},
"cn":{...translations},
"jp":{...translations},
}
At the moment I'm initializing i18n in this way
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import HttpBackend from "i18next-http-backend";
import LanguageDetector from "i18next-browser-languagedetector";
import { languageService } from "services";
const fallbackLng = ["en"];
const { getDictionary } = languageService;
i18n
.use(HttpBackend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng,
backend: {
allowMultiLoading: true,
request: (options: any, url: any, payload: any, callback: any) => {
try {
getDictionary().then(res => {
callback(null, {
data: res,
status: 200
});
});
} catch (e) {
console.error(e);
callback(null, {
status: 500
});
}
},
}
});
export const i18nextConfig = i18n;
but the resulting i18n store instead of being
store: {
data: {
en:{...translation },
cn:{...translation },
}
}
its
store: {
data: {
en:{
translation:{
"en":{...translation},
"cn":{...translation}
}
}
}
}
I've been reading the documentation but it seems pretty unclear in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能还需要使用MultiLoad-backend-aDapter =>
You may need to also use the multiload-backend-adapter => https://github.com/i18next/i18next-multiload-backend-adapter