React-Router翻译路线的最佳方式?

问题描述 投票:0回答:1

我正在构建一个需要以3种语言提供的反应应用程序。 现在,它只有英文版本 - 我正在使用反应路由器来管理页面。

我不需要翻译它,我已经有翻译,但我不知道如何在我的应用程序中实现这些。 将我的项目复制3次到/ en,/ it,/ fr听起来效率低下。

有什么建议?

     <Router >

 <div>
 <Switch>

  <Route exact path="/contact" component={Contact}  />
  <Route exact path="/where" component={Where}  />
  <Route exact path="/about" component={About}  />
  <Route exact path="/" component={Home}  />
  <Route component={NotFound}  />

  </Switch>

  </div>

  </Router>
reactjs react-router
1个回答
0
投票

您可以使用Router params语言

<Router >
  <Route path="/:language" component={Localise}/>
</Router>

和本地化组件

render() {
    const {language} = this.props.match.params; 
    return (
        <div>
            <Switch>
              <Route exact path=`${match.path}/contact` component={Contact}  />
              <Route exact path=`${match.path}/where` component={Where}  />
              <Route exact path=`${match.path}/about` component={About}  />
              <Route exact path={match.path} component={Home}  />
              <Route component={NotFound}  />
            </Switch>
        </div>
    )
}
© www.soinside.com 2019 - 2024. All rights reserved.