2017-02-27 51 views
2

我在應用程序中創建一些路線/ router.js如何在ember-cli中翻譯路線?

Router.map(function() { 
    let i18n = this.service('i18n'); 
    this.route("lang", { path: '/:lang' }, function() { 
     this.route('home', { path: '/', template: 'home' }); 
     this.route('about', { path: '/' + i18n.t('router.about'), template: 'about' }); 
     this.route('locales', { path: '/' + i18n.t('router.locations'), template: 'locales' }); 
    }); 
}); 

但國際化翻譯僅在第一次。

如何通過更改語言來翻譯這些路線?

我使用:

燼-CLI:2.11.1

節點:7.4.0

ember-i18n:5.0.0

+1

請閱讀此頁:http://xyproblem.info,然後小心地描述你願意來實現。你所做的事情是非常錯誤的,所以我想聽聽你最初的目標,而不是你想達到的目標。 –

+0

你想翻譯URL中的路由名稱嗎?如果是的話,不要!這是一個非常糟糕的主意。 – Lux

+0

@Lux,我試圖翻譯路線路徑。 –

回答

0

的路由創建一次,你不能更改路由名稱。

對於你的目標,你可以這樣做:

//router.js 

this.route("lang", {path: '/:lang'}, function() { 
    this.route('register', {path: ':path'}); 
    }); 

而且在模板:

//index.hbs 
{{#link-to "lang.register" locale path}}{{t "routes.register"}}{{/link-to}} 

,並在相應的控制器:

//controllers/index.js 
i18n: Ember.inject.service(), 

    locale: Ember.computed.readOnly('i18n.locale'), 

    path: Ember.computed('locale', function() { 
    return { 
     path: this.get('i18n').t('routes.register') 
    }; 
    }), 

現在你的路由的網址發生變化,按i18n語言環境更改。

請看看this twiddle

+0

但是,如何在同一時間更改當前URI? –

+0

@BrunoSalgado我更新了我的旋轉。請看'changeLang'行動 –

+0

Ebrahim,這工作正常。但是,當我使用多條路線時,Ember始終會呼叫定義的第一條路線。這是'因爲路徑是動態的(路徑:'/:路徑')。你能幫我嗎? [twiddle](https://ember-twiddle.com/307a4a1e767d6feee843a10c87c443d8?openFiles=controllers.application.js%2C&route=%2Fen%2Fhome) –