我的 React 应用程序通过在 package.json 中设置变量从相对路径加载homepage。然而 i18next-http-backend 仍然尝试从 加载翻译http://localhost:3000/locales/en/translation.json而不是http://localhost:3000/myapp/locales/en/translation.json. 我什至尝试设置 loadPath 选项(虽然不确定语法),但这不起作用。请看下面:
i18n
.use(Backend)
.use(initReactI18next)
.init({
fallbackLng: 'en',
debug: true,
loadPath: `${process.env.PUBLIC_URL}/locales/{{lng}}/{{ns}}.json`,
interpolation: {
escapeValue: false // not needed for react as it escapes by default
}
})
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我在这里关注了这篇文章:https://dev.to/adrai/how-to-properly-internationalize-a-react-application-using-i18next-3hdb。
现在我想知道是否有办法将参数传递到从 .json 文件中提取的字符串中。
{
"GREETING": "Hello ${name}, nice to see you."
}
Run Code Online (Sandbox Code Playgroud)
import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-http-backend';
i18n
// i18next-http-backend
// loads translations from your server
// https://github.com/i18next/i18next-http-backend
.use(Backend)
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
debug: false,
fallbackLng: 'en',
interpolation: {
escapeValue: false …Run Code Online (Sandbox Code Playgroud)