简体   繁体   English

Ember i18n动态链接翻译

[英]Ember i18n dynamic link inside of translation

I was wondering how I could pass a link into to {{t}} helper. 我想知道如何将链接传递到{{t}}助手中。 I am using v3.0.1 of Ember i18n at the moment. 我目前正在使用Ember i18n的 v3.0.1。

Obviously you cannot pass a link helper into at helper (something like 显然,您无法将链接助手传递给at helper(类似

{{ t "some-translation-string" link={{#link-to 'page' model}}page{{/link-to}} }}

won't work of course). 当然不会起作用)。

So I was thinking, maybe I can create a custom prop that returns a whole link. 所以我在想,也许我可以创建一个返回整个链接的自定义道具。 But then again, how do I create that link?. 但是话又说回来,我该如何创建该链接? Does anyone know of a method that has the same arguments as the link-to helper, but returns just the link (In my case 'page' and model )? 有谁知道一种方法,该方法的参数与link-to帮助器的参数相同,但仅返回链接(在我的情况下为'page'model )?

You might be able to achieve that with a basic link, but I doubt that you'll be able to toss a live link-to component inside a translation. 您也许可以使用基本链接来实现这一点,但是我怀疑您是否可以在翻译中使用实时链接到组件。

So instead split your translation string into pieces: 因此,将您的翻译字符串分成几部分:

{{t 'goToSettingPage-before'}}
{{link-to (t 'goToSettingPage-link') 'route.name'}}
{{t 'goToSettingPage-after'}}
'goToSettingPage-before': 'Go to'
'goToSettingPage-link':   'settings'
'goToSettingPage-after':  'page.'

You can create a helper to do exactly what you want using ember-href-to . 您可以使用ember-href-to创建一个帮助程序来完成您想要的工作。

Helper: 帮手:

compute (params) {
  const i18n = this.get('i18n');
  const target = hrefTo(this, params[1]);
  // const targetParam = params[2]; //dynamic segment
  const text = i18n.t(params[0]);

  return Ember.String.htmlSafe('<a href='+ target +'>' + text +'</a>');
}

Template usage: 模板用法:

{{t "linkExample-learnMore" link=(helper-name 'linkExample-here' 'some.route')}}

Translations: 翻译:

"linkExample-learnMore": "Click {{{link}}} to do something",
"linkExample-here":"here"

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

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