如何使用 React js 和 i18next 将语言环境添加到路径?
我正在使用 React Js 和 i18next 尝试构建一个具有多种语言的应用程序
我想在我的 URL 中生成本地,问题是前缀将在 URL 中添加多次!
这是我的配置:
i18n.js
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: true,
supportedLngs: ['de','en','fr'],
fallbackLng: 'de',
whitelist: ['de','en','fr'],
detection: {
order: ['path','cookie', 'htmlTag', 'localStorage', 'sessionStorage','subdomain'],
caches: ['cookie'],
lookupFromPathIndex: 0,
checkWhitelist: true
},
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
// format: (value, format, lng) => { // legacy usage
// if (value instanceof Date) {
// return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime[format])
// }
// return value;
// }
},
backend: {
loadPath: '/locales/{{lng}}/translation.json',
}
});
路由:
function App() {
const baseRouteUrl = "/:locale(en|de|fr)?";
return (
//@todo add baseurl
<I18nextProvider i18n={i18next}>
<BrowserRouter >
<Suspense fallback={<p>...Loading</p>}>
<Switch>
<Route exact path={baseRouteUrl + "/"} component={Home} />
<Route path={baseRouteUrl + "/about"} component={About} />
</Switch>
</Suspense>
</BrowserRouter>
</I18nextProvider>
);
}
导航:
const Navigation = () => {
const baseUrl = cookies.get('i18next');
return (
<Link to={baseUrl + "/"}> Home </Link>
<br/>
<Link to={baseUrl + "/about/"}>About </Link>
);
};
URL 为:
http://localhost:3000/en /about/
http://localhost:3000/en/de/about/
感谢您的帮助!
I am using React Js and i18next to try to build an app with multiple Language
I want to generate the Local inside my URL, the Problem that the Prefix would be added multiple times in the URL!
Here is my configuration:
i18n.js
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: true,
supportedLngs: ['de','en','fr'],
fallbackLng: 'de',
whitelist: ['de','en','fr'],
detection: {
order: ['path','cookie', 'htmlTag', 'localStorage', 'sessionStorage','subdomain'],
caches: ['cookie'],
lookupFromPathIndex: 0,
checkWhitelist: true
},
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
// format: (value, format, lng) => { // legacy usage
// if (value instanceof Date) {
// return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime[format])
// }
// return value;
// }
},
backend: {
loadPath: '/locales/{{lng}}/translation.json',
}
});
Routing:
function App() {
const baseRouteUrl = "/:locale(en|de|fr)?";
return (
//@todo add baseurl
<I18nextProvider i18n={i18next}>
<BrowserRouter >
<Suspense fallback={<p>...Loading</p>}>
<Switch>
<Route exact path={baseRouteUrl + "/"} component={Home} />
<Route path={baseRouteUrl + "/about"} component={About} />
</Switch>
</Suspense>
</BrowserRouter>
</I18nextProvider>
);
}
Navigation:
const Navigation = () => {
const baseUrl = cookies.get('i18next');
return (
<Link to={baseUrl + "/"}> Home </Link>
<br/>
<Link to={baseUrl + "/about/"}>About </Link>
);
};
the URL would be:
http://localhost:3000/en/about/
http://localhost:3000/en/de/about/
Thanks for any Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cookie 的价值是什么?
如果您改用 resolvedLanguage 值会有帮助吗?
顺便说一句:您可以删除白名单选项,白名单已重命名为supportedLngs
What is the value of the cookie?
Does it help if you use the resolvedLanguage value instead?
btw: you can remove the whitelist option, whitelist was renamed to supportedLngs