React I18N-从API加载多种语言

发布于 2025-02-10 19:21:07 字数 1555 浏览 1 评论 0原文

我的应用程序将语言存储在数据库中, 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}
      }
    }
  }
}

i18n商店结构

我一直在阅读文档,但在这种情况下似乎还不清楚。

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}
      }
    }
  }
}

i18n store structure

I've been reading the documentation but it seems pretty unclear in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2025-02-17 19:21:07

您可能还需要使用MultiLoad-backend-aDapter =>

You may need to also use the multiload-backend-adapter => https://github.com/i18next/i18next-multiload-backend-adapter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文