简体   繁体   English

标签的Ember i18n动态文本(使用占位符)

[英]Ember i18n Dynamic Text for a Tag (Using Placeholder)

I'm currently translating an application using ember's i18n library. 我目前正在使用ember的i18n库翻译应用程序。

With the use of a placeholder, I'm able to translate text for a tag, nevertheless, I'm are only able to pass the tag to the placeholder if the text given is a string. 通过使用占位符,我可以翻译标签的文本,但是,如果给定的文本是字符串,则只能将标签传递给占位符。

The tags we are using are normal i18n tags: 我们使用的标签是普通的i18n标签:

example_tag : 'ejemplo'

The method we have for the placeholder we have right now looks like this: 我们现在拥有的占位符的方法如下所示:

TranslationsPlaceHolder: (->{ 
    return Ember.I18n.t('example_tag');
})

and the way we call it is like this: 我们称之为的方式是这样的:

{{input value=view.example placeholder=view.TranslationsPlaceholder}}

I'm currently looking for a way we can assign different tags with the use of one placeholder with dynamic tags, so we could pass the tag we want to translate as a parameter and only use one placeholder with different tags. 我目前正在寻找一种方法,可以使用一个带有动态标签的占位符来分配不同的标签,因此我们可以将要转换的标签作为参数传递,而只能使用一个带有不同标签的占位符。

We are using reference from this question: Inserting a translation into a placeholder with Emblem.js 我们正在使用来自以下问题的参考: 使用Emblem.js将翻译插入占位符

Thanks a lot! 非常感谢!

Any chance you could refactor the code to use the native <input> instead with one way binding? 您是否有可能重构代码以使用本机<input>而不是通过一种方法绑定? Here is a twiddle that shows you what I mean. 这是一个旋转手势 ,向您展示我的意思。

<input value={{inputValue}} oninput={{action (mut inputValue) value="target.value"}} placeholder={{translate "placeholder"}} />

where translate is a helper: 在哪里translate是一个助手:

const translations = {
  placeholder: "Type here"
};
export function translate(params/*, hash*/) {
  return translations[params] || `${params}_NOT_FOUND`;
}

export default Ember.Helper.helper(translate);

You are safe to switch to the native <input> if you are >= Ember 2.3.1. 如果您是Ember 2.3.1,则可以安全地切换到本机<input>

If you are less than that (which is the only time I'd recommend doing what I'm suggesting here instead of one way binding), you can provide your own tagless component wrapper around {{input}} that takes the key for the placeholder and does the translation via computed properties. 如果您还不够用(这是我唯一建议您执行的操作,而不是单方法绑定),则可以在{{input}}周围提供自己的无标签组件包装,该包装使用占位符,并通过计算的属性进行翻译。 See this twiddle for an example. 参见这个旋转例子。

Your input becomes: 您的输入将变为:

{{translated-placeholder-input value=value placeholderKey="placeholder"}}

where translated-placeholder is a tagless component: 其中translated-placeholder是无标签组件:

import Ember from 'ember';
const translations = {
  placeholder: "Type here..."
};
export default Ember.Component.extend({
  //creates the component without an enclosing div
  tagName: "",
  translatedPlaceholder: Ember.computed('placeholderKey', function(){
    let key = this.get('placeholderKey');
    if(!key){
      return ""; 
    }
    return translations[key] || `${key}_NOT_FOUND`;
  })
});

that's simply a passthrough to input 这只是input传递

{{input value=value placeholder=translatedPlaceholder}}

The drawback to this approach (besides all that comes with 2 way binding), is having to pass explicitly any attribute bound to the component to the {{input}} 这种方法的缺点(除了2种方式附带的所有方法外)是必须将绑定到组件的任何属性显式传递给{{input}}

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

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