Tar*_*kiv 6 reactjs react-intl react-redux react-router-v4
我正在创建多语言应用程序。我正在使用:React Intl; React Router(最新v4); Redux。
我的应用程序中的路径将取决于语言环境:
/ <-- default expecting this to be uk
/en
/ru
/news <-- uk
/en/news
/ru/news
Run Code Online (Sandbox Code Playgroud)
如果用户拥有locale = en-US
并进入localhost:8080
应用,则将其重定向到 localhost:8080/en
如果用户拥有locale = uk
并进入localhost:8080
应用程序,则显示其对应于地址的组件localhost:8080/
而不更改位置路径名。
/ <-- default expecting this to be uk
/en
/ru
/news <-- uk
/en/news
/ru/news
Run Code Online (Sandbox Code Playgroud)
目前,它无法正常工作。
我no match
在航线配置,如果我进入localhost:8080/
或localhost:8080/en
。
这是我的解决方法。重定向正在运行,并且前缀已添加到 props 中。
const Routes = ({ lang }) => (
<BrowserRouter>
<Route render={props => <Header lang={lang} {...props} />} />
<Switch>
<RootRouter
exact
path={'/:lang(en|ru)?'}
lang={lang}
component={Landing}
/>
<Route
exact
path={'/:lang(en|ru)?/news'}
render={props => <News {...props} lang={lang} />}
/>
<Route component={FourOhFour} />
</Switch>
</BrowserRouter>
);
const mapStateToProps = state => ({ lang: getPrefix(state.locale) });
export default connect(mapStateToProps)(Routes);
Run Code Online (Sandbox Code Playgroud)
const RootRouter = ({ component: Component, exact, path, lang }) => (
<Route
exact={exact}
path={path}
render={(props: ContextRouter) =>
props.location.pathname === '/' && lang !== '' ? (
<Redirect to={`/${lang}`} />
) : (
<Component {...props} lang={lang} />
)}
/>
);
Run Code Online (Sandbox Code Playgroud)
const changeLang = newLang => () => {
if (lang !== newLang) {
const newUrl = switchLangHepler(lang, newLang, location.pathname);
setLocaleData(newLang);
localStorage.setItem('lang', String(newLang));
history.replace(newUrl);
}
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1004 次 |
最近记录: |