Angular 4创建通往同一组件的不同语言路径 [英] Angular 4 create different language path route to the same component

查看:98
本文介绍了Angular 4创建通往同一组件的不同语言路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为网站创建多语言支持.我目前正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆