Angular 4创建通往同一组件的不同语言路径 [英] Angular 4 create different language path route to the same component
问题描述
我想为网站创建多语言支持.我目前正在使用ngx-translate翻译所有文本.我们有两个网址,mypage/en/home和mypage/es/home.如何创建这些语言路径并将其路由到home组件?
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?
推荐答案
根据评论进行了更新:
在路由器的路由中,您可以执行以下操作:
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
}
]
这篇关于Angular 4创建通往同一组件的不同语言路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!