简体   繁体   English

用react-router本地化的子路径?

[英]Localised sub-paths with react-router?

So I am converting a large existing application to React. 所以我正在将一个大型的现有应用程序转换为React。 Our localised content is like this: 我们的本地化内容如下:

www.website.com/about      // english
www.website.com/es/about   // spanish
www.website.com/de/about   // german

So I need to declare routes for each locale, I assume like this: 所以我需要为每个语言环境声明路由,我假设是这样的:

<Route path='/' component={App} />
<Route path='/es' component={App} />
<Route path='/de' component={App} />

I am wondering if there is a more terse way of doing this, though? 我想知道是否还有更简洁的方法?

Also, I would like all links to respect the base path, eg: 另外,我希望所有链接都遵守基本路径,例如:

<Link to='/inbox' />

Would automatically go to /es/inbox if that user's locale is Spanish. 如果该用户的语言环境为西班牙语,则将自动转到/es/inbox There must be a pre-existing or common way of doing this? 必须有预先存在的或通用的方法来执行此操作吗?

You can use params with react router. 您可以将参数与react路由器一起使用。 So something like 所以像

<Route path='/:language' component={App} />

So in the components you can access this.props.params.language will have whatever which is navigated. 因此,在组件中您可以访问this.props.params.language将具有导航的内容。 So in the component you can handle those logic 因此,您可以在组件中处理这些逻辑

One approach 一种方法

You can keep locale or something else as static route for language params. 您可以将locale或其他内容保留为language参数的静态route

As an example 举个例子

Your links will be like 您的链接就像

www.website.com/locale/en/about    // english
www.website.com/locale/es/about   // spanish
www.website.com/locale/de/about   // german

And routes will be something like this. 路线将是这样。

<Route path='/locale'/>
  <Route path='en' component={App} />
  <Route path='es' component={App} />
  <Route path='de' component={App} />
</Route>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM