reactjs - 无法在 i18next 中使用来自 S3 buck 的 JSON
问题描述
这就是我的i18n
文件的样子 -
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import HttpApi from 'i18next-http-backend';
i18n
.use(HttpApi)
.use(initReactI18next)
.init({
// resources: resourcesObj,
initImmediate: false,
backend: {
loadPath: 'https://bucketlink.amazonaws.com/locales/{{lng}}/{{ns}}.json',
crossDomain: true,
allowMultiLoading: true,
},
lng: "en",
fallbackLng: "en",
debug: false,
ns: ["translations"],
defaultNS: "translations",
keySeparator: false,
interpolation: {
escapeValue: true
}
});
export default i18n;
当我更改语言时,我看到我的网络选项卡显示 JSON 正确来自 S3。但是,在我的所有屏幕中都显示了键而不是值。有人可以告诉我为什么我的翻译值没有显示。
注意——如果我使用资源并将我的 json 对象放入其中,它会按预期工作。
解决方案
从您发布的演示中,问题是您的 json 文件(存储在 s3 中的文件)的格式不正确。
您需要按 lang & 命名空间(translations
在您的情况下)拆分 json 文件,并且 json 文件必须仅包含与lang
&相关的键namespace
。
例如,您的文件夹应如下所示:
└── locales
├── en
│ └── translations.json
└── he
└── translations.json
每个translation.json
文件应包含:
{
"Overview": "Overview",
"TaskList": "Task List"
}
PS,你的翻译文件可以是一个嵌套对象,那么你需要用一个dot notation
(类似的东西a.b.c
)指定键
推荐阅读
- python-3.x - Gurobi 使用 addVar 中的 obj 参数
- google-pay - 如何测试草稿中的 Google Pay 课程?
- php - 使用 REST API php 在 KOHA 中传递令牌
- r - 在 R 中将 2 个图合并为 1 个图
- asp.net - ASP.NET:面板未使用 visible=true 呈现
- sql - 在选择中选择?
- javascript - 问题重新索引嵌套对象数组中的项目
- perl - Mojo::UserAgent - 在解码前检查 Content-Encoding 标头
- angular - Spring Boot + Angular 应用程序作为 WAR 部署到 Heroku,抛出 404,除非我在 URL 中包含“index.html”
- vue.js - 向列表添加多个排序/过滤选项 - 最佳方法?