Interpolation of i18n links

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

Any idea?

+2
source share
2 answers

Reply from @jamesarosen on Github:

You cannot use the helper {{link-to}}inside a translation because it emits a DOM node, not a string.

But you can use ember-href-to addon to create 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 the htmlSafe helper):

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

I am facing a similar problem recently and this is what I did:

Translation File (en.json):

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

template/hbs:

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

html-safe - ember-cli-string-helpers .

0

Source: https://habr.com/ru/post/1545334/


All Articles