Angular 4 create different language path route to the same component

╄→гoц情女王★ 提交于 2020-01-06 07:15:30

问题


I want to create the multi-language support for the website. I'm currently using ngx-translate to translate all the text . Let's way we have two urls, mypage/en/home and mypage/es/home. How can I create those language paths and route them to home component?


回答1:


Updated based on comment:

In the routes for the router you can do something like this:

export const routes: Routes =[
  {
    path: 'mypage/:language/home', component: HomeComponent
  }
]

This way you actually only need one route and can have as many languages as you want.

Then in your component you can do:

public constructor (
  route: ActivatedRoute
){
  this.language = this.route.snapshot.params['language'];  
}

If you really want multiple routes, you can do something like this:

export const routes: Routes =[
  {
    path: 'mypage/en/home', component: HomeComponent
  }
  {
    path: 'mypage/es/home', component: HomeComponent
  }
]


来源:https://stackoverflow.com/questions/50297558/angular-4-create-different-language-path-route-to-the-same-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!