2017-07-14 78 views
1

Angular2 - 打字稿 - NG-翻譯翻譯我的網址角2 - 打字稿

你好的人,

我需要改變取決於語言我的網址的選擇了休息「恩/大豆」,「FR /大豆」。在我app.routes我得到這個路徑:

{ path: ':lang/soybean', component: SoybeanComponent } 

我的大豆成分從郎PARAM與翻譯NG-翻譯

this.route.params.subscribe(params => { 
    translate.use(params['lang']); 
}) 

如何顯示URL作爲FR /大豆,但仍使用路徑fr /大豆!?

我試過

*我創建了一條新路:{ path: ':lang/soya', component: SoybeanComponent }, 我以爲我可以將用戶重定向到好的路徑:

if(params['lang'] === 'en'){ 
     this.router.navigateByUrl('lang/soybean'); 
    } 
    if(params['lang'] === 'fr'){ 
     this.router.navigateByUrl('lang/soya'); 
    } 

,但它會導致一個無限加載。
後來我想重定向等之後,我可以硬編碼郎PARAM:

if(params['lang'] === 'en'){ 
     this.router.navigateByUrl('en/soybean'); 
    } 
    if(params['lang'] === 'fr'){ 
     this.router.navigateByUrl('fr/soya'); 
    } 

但在這種情況下,我會捕捉異常*

+0

看起來你把'lang'當作路由參數和常量路徑路由? – DeborahK

+0

'lang'確定我將使用的巫婆語言,但它並未改變路徑。如果'lang'是fr,那麼soybean.html和soybean.component將被翻譯成法文。我想要的是如何顯示URL爲fr /大豆,但仍然使用路徑fr /大豆! 對不起,我將編輯我的第一篇文章! – SilverShroud

回答

0

我發現我自己用location.replace(NEWURL)解決方案

我創建了一條新路:{ path: ':lang/soya', component: SoybeanComponent }

,在我的部分我用location.replace (url)

this.route.params.subscribe(params => { 
      translate.use(params['lang']); 
      switch (params['lang']) { 
       case 'en': 
        location.replace(`index.html#/${params['lang']}/soybean`) 
         break; 
       case 'fr': 
        location.replace(`index.html#/${params['lang']}/soya`); 
         break; 
      } 

     }) 

這對我有用!如果你知道更好的解決方案,我會很高興聽到它!謝謝 !

1

你是它過於複雜:-)。 與反引號只是用模板字符串

url = `${lang}/${i18name}` 
+0

這是角2嗎?你能否確定你在哪裏以及如何使用它!因爲我在app.routes.ts中定義了我的url,如{path:':lang/soybean',component:SoybeanComponent}?謝謝 ! – SilverShroud

+0

它被烘焙成Typescript,這是Angular 2&4基於的基礎。 https://basarat.gitbooks.io/typescript/content/docs/template-strings.html – JGFMK