translation我正在從存盤在反應應用程式的公共檔案夾中的檔案中讀取值。檔案存盤在

但由于我們data可以隨時更改,所以我們將資料存盤到資料庫中。在從該語言的瀏覽器資料中檢測到語言后的加載時間將被呼叫API并直接使用。所以下面的檔案代碼運行良好。
import i18next from 'i18next';
const LanguageDetector = require("i18next-browser-languagedetector");
const initReactI18next = require("react-i18next");
import { TRANSLATIONS_EN } from "../public/locales/en/en.js";
import { TRANSLATIONS_IT } from "../public/locales/it/it.js";
i18next
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources: {
en:TRANSLATIONS_EN,
it:TRANSLATIONS_IT,
},
fallbackLng: 'fr'
});
i18next.changeLanguage(navigator.language);
export default i18next;
現在我使用下面的代碼代替這個,我在其中呼叫API來讀取data它,它將直接分配給variable. 我們不會在下面的代碼中將資料寫入檔案,而不是files我呼叫 API 的靜態檔案。如果我列印 API 或檔案的輸出,它們是完全相同的。但無法理解為什么這不起作用。可能asynchronous是什么原因?
import i18next from 'i18next';
import React from 'react';
import TransFile from './TransFile.js';
import apiDelegate from '../src/components/Utils/Common/api.js';
const LanguageDetector = require('i18next-browser-languagedetector');
const initReactI18next = require('react-i18next');
import { TRANSLATIONS_EN } from '../public/locales/en/en.js'; //just for verification
import makeGetRequest from './abc.js';
apiDelegate.getTranslations().then((result) => {
output = JSON.stringify(result);
const tr_en = 'translation: ' JSON.parse(output) ;
alert(tr_en); //this and below alert produce same result.
alert(TRANSLATIONS_EN));
i18next
.use(LanguageDetector)
.use(initReactI18next)
.init({
resources: {
en: tr_en, //if I write TRANSLATIONS_EN here it works
},
fallbackLng: 'fr',
});
i18next.changeLanguage(navigator.language);
});
export default i18next;
無法理解奇怪的問題。有沒有更好的處理方法?我找到了i18n-http-backendapi 但從這里我不知道如何呼叫restAPI。請幫忙。
uj5u.com熱心網友回復:
第一個then是處理回應標頭...您需要第二個then來處理回應正文。嘗試:
apiDelegate.getTranslations()
.then(response => response.json())
.then(result => {
// ... Your code
})
uj5u.com熱心網友回復:
這似乎是以下內容的副本:
- 如何將 API 資料合并到 i18next 而不是靜態檔案中
- 我們可以在其他反應組件中完全完成之前使用組件嗎
您可能想要使用 i18next-http-backend:如何在 i18next 而不是靜態檔案中合并 API 資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/419825.html
標籤:
