Link interpolation i18n

余生颓废 提交于 2019-12-02 01:59:25

问题


I use ember-i18n to handle multiples langages in my project, and I need to insert a link inside a translation (with interpolation).

Any idea ?


回答1:


Response from @jamesarosen on Github :

You can't use the {{link-to}} helper inside a translation because it emits a DOM node, not a String.

But you can use ember-href-to addon to generate URLs.

In JavaScript:

// some-thing/component.js:
import { hrefTo } from 'ember-href-to/helpers/href-to';

text: Ember.computed('i18n.locale', function() {
  const i18n = this.get('i18n');
  const href = hrefTo(this, 'some.route');
  const link = Ember.String.htmlSafe(`<a href="${href}">Link Text</a>`);
  return i18n.t('some.translation', { link });
})

Or in Handlebars (you'll need an htmlSafe helper):

{{t 'some.translation'
  link=(htmlSafe (join '<a href="' (href-to 'some.route') '">Link Text</a>'))}}



回答2:


I face a similar problem recently and this is what I did:

Translation file (en.json) :

"some.message.key": "Successfully created <a href='{{userlink}}'> {{username}} </a>."

In the respective template/hbs file:

{{html-safe (t "some.message.key" userlink=link username=name)}}

html-safe is a helper provided by ember-cli-string-helpers add on.



来源:https://stackoverflow.com/questions/40265294/link-interpolation-i18n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!