简体   繁体   English

Ember.js在区域设置更改时重新呈现链接

[英]Ember.js re-render links on locale change

My application uses the ember-i18n addon. 我的应用程序使用ember-i18n插件。 For SEO purposes, I wanted to put a /[lang-code]/ in the URL. 出于SEO的目的,我想在URL中放置一个/ [lang-code] /。

When changing the language, history.pushState can change the URL and its alright, but the links on the page do not change for the new language, even though router.rootURL is changed. 更改语言时,history.pushState可以更改URL及其名称,但是即使更改router.rootURL,页面上的链接也不会更改为新语言。

So far I can just change the window.location.pathname, but thus a new network request is being made. 到目前为止,我只可以更改window.location.pathname,但是因此正在发出新的网络请求。

I tried overriding the href-to helper (from ember-href-to addon) and added this: 我尝试覆盖href-to助手(从ember-href-到插件),并添加了以下内容:

_recomputeOnLocaleChange: Ember.observer('i18n.locale', function() {
    console.log("here?");
    this.recompute();
})  

It executes on locale change, but apparently recompute does nothing. 它在语言环境更改时执行,但显然重新计算不执行任何操作。 Even with setTimeout (because perhaps rootURL didn't change yet). 即使使用setTimeout(因为rootURL可能尚未更改)。 Links are all the same. 链接都是一样的。

Can you suggest anything? 你能建议什么吗? Would be greatly appreciated! 将不胜感激! Thank you!! 谢谢!!

In the didTransition hook of your router.js file, check if the locale has changed and set a property on a service to the new locale if it has. router.js文件的didTransition挂钩中,检查语言环境是否已更改,然后将服务的属性设置为新语言环境(如果有)。 Then in your helper, you can check that property. 然后,在您的助手中,您可以检查该属性。

In router.js : router.js

oldI18nLocale: null,

didTransition() {
  this._super(...arguments);

  // ...

  let i18nLocale = this.get('locale.i18n');
  if (i18nLocale !== this.get('oldI18nLocale')) {
    this.set('someService.i18nlocale', this.get('locale.i18n');
  }
  this.set('oldI18nLocale', i18nLocale));
}

In your helper: 在您的助手中:

recomputeOnLocaleChange: Ember.observer('someService.i18nLocale', function() {
    this.recompute();
})

Hope this works. 希望这行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM