您好,我正在使用 i18next 进行翻译,但也收到以下错误:
i18next::backendConnector: loading namespace translation for language ja failed TypeError: Failed to parse URL from /locales/ja/translation.json
at Object.fetch (node:internal/deps/undici/undici:11576:11) {
[cause]: TypeError [ERR_INVALID_URL]: Invalid URL
我的文件位于此路径
/public/locales/ja/translation.json
这就是我的配置:
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';
if (!i18n.isInitialized) {
i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
supportedLngs: ['en', 'ja'],
fallbackLng: 'ja',
debug: true,
});
}
export default i18n;
网络选项卡中没有失败的请求,json 文件有效,所以我觉得是某种配置错误之类的。如果您有任何建议,我将非常感激!预先感谢
看起来您正在使用 Node.js(在后端)运行此库。 因此,您会收到错误,因为服务器端代码不理解浏览器绝对路径。它只能使用完整的 url(包含 http 和域名)来获取。
因此,您可以通过指定 json 文件的完整 url 来解决此错误。
如果翻译文件位于服务器本地,最好使用 i18next-fs-backend 从磁盘读取它。