2017-02-27 52 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) –