简体   繁体   English

灰烬:将本地化密钥传递给组件模板

[英]Ember: Passing localisation key to component template

I have a modal-dialog component template, which contains the following 我有一个模态对话框组件模板,其中包含以下内容

  <div class="header">
    {{t title}}
  </div>

So, I am using the ember-i18n [1] library to add localisation to ember. 因此,我正在使用ember-i18n [1]库将本地化添加到ember。 Now I am calling that component template from the actual modal dialog template: 现在,我从实际的模式对话框模板中调用该组件模板:

{{#modal-dialog title="dialog.title"}}
  <h3 class="flush--top">I am a modal dialog</h5>
  <button {{action "close"}}>Done</button>
{{/modal-dialog}}

What I am trying to do here is, defining a key that is used for localisation in the dialog template, which is passed to the component template and translated there. 我在这里想要做的是在对话框模板中定义用于本地化的键,该键将传递到组件模板并在那里进行翻译。 However, this leads to the following error: Missing translation: title . 但是,这导致以下错误: Missing translation: title So the variable title just seems to actually be treated as a string and not as a variable. 因此,变量标题似乎实际上只是被视为字符串而不是变量。

Alternatively I could translate the title in the dialog template and pass it to the component template: 或者,我可以翻译对话框模板中的标题并将其传递给组件模板:

dialog: 对话:

{{#modal-dialog title={{t "dialog.title"}} action="close"}}

That leads to a compiler error: 这导致编译器错误:

Error: Parse error on line 1:
...#modal-dialog title={{t "dialog.title"}}
-----------------------^
Expecting 'STRING', 'INTEGER', 'BOOLEAN', 'OPEN_SEXPR', 'ID', 'DATA', got 'OPEN'

Is there any way to make this happen? 有什么办法可以做到这一点?

[1] https://github.com/jamesarosen/ember-i18n [1] https://github.com/jamesarosen/ember-i18n

mitchlloyd presented a great solution here: http://discuss.emberjs.com/t/need-to-pass-a-value-from-component-template-to-component/5792/6 mitchlloyd在这里提出了一个很好的解决方案: http ://discuss.emberjs.com/t/need-to-pass-a-value-from-component-template-to-component/5792/6

Use the Translation suffix in the template (I was just passing a title property): 在模板中使用Translation后缀(我只是传递title属性):

{{#modal-dialog action="close" titleTranslation="modal.title"}}
  <h3 class="flush--top">Name Change Modal</h5>
{{/modal-dialog}}

The component uses the Mixin from ember-i18n: 该组件使用ember-i18n中的Mixin:

export default Ember.Component.extend(Em.I18n.TranslateableProperties, {
}); 

And finally in the template, just reference the translated title property: 最后在模板中,只需引用翻译后的title属性:

<div class="title"> {{title}}</div>

The explanations above are not working with the latest version of ember-i18n where TranslateableProperties has been removed : 以上说明不适用于已删除 TranslateableProperties的最新版本的ember-i18n:

Here is how I made it work with ember-i18n 4.x and ember-cli 1.13.1: 这是我如何使用ember-i18n 4.x和ember-cli 1.13.1进行工作:

1 - Make sure that the service i18n is injected in your component: 1-确保将服务i18n注入您的组件中:

//initializers/i18n.js
export default {
  name: 'i18n',

  after: 'ember-i18n',

  initialize: function(_, app) {
      app.inject('controller', 'i18n', 'service:i18n');
      app.inject('component', 'i18n', 'service:i18n');
  }
};

2 - The component's template references a {{title}} property: 2-组件的模板引用了{{title}}属性:

//templates/components/pick-card.hbs
!-- Selection of the card -->
<div class="row" style="margin-top: 40px;">
    <div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h1 class="panel-title">{{title}}</h1>
            </div>
...

3 - The template where I call my component: 3-我在其中调用组件的模板:

//templates/security/forgot-username.hbs
{{#pick-card title-i18n="page.forgot_username"}}
{{/pick-card}}

4 - The component's controller where the magic is: 4-组件的控制器的魅力在于:

    import Ember from 'ember';
    import {translationMacro as t} from "ember-i18n";

    export default Ember.Component.extend({
      title: Ember.computed('i18n.locale', function() {
          return this.get('i18n').t(this.get('title-i18n'));
      })
    });

As described in ember-i18n doc the use of i18n.locale is crucial to make sure that the property {{title}} is translated automatically as soon as the user changes the locale. ember-i18n文档中所述,使用i18n.locale对于确保用户更改语言环境后立即自动转换属性{{title}}至关重要。

Have fun :) 玩得开心 :)

Better way is to use the Handlebars Subexpressions: 更好的方法是使用Handlebars子表达式:

You would use some this like this: 您可以这样使用:

{{#modal-dialog title=(tv "dialog.title") action="close"}}

Here tv is the helper i have used: 电视是我使用过的助手:

And You need to register a helper for this: 并且您需要为此注册一个助手:

Ember.Handlebars.registerHelper('tv', function (key) {
  return Em.I18n.t(key);
});

Hope this helps 希望这可以帮助

You can compute that title property within the controller or model. 您可以在控制器或模型中计算标题属性。 So: 所以:

{{#modal-dialog title={{t "dialog.title"}} action="close"}}

Would be: 将会:

{{#modal-dialog title="dialog.translateTitle" action="close"}}

And then you could have a computed property like so: 然后,您可以具有如下所示的计算属性:

translateTitle: function () {
  return this.translateLibraryMethodYouUse(this.get('title'));
}.property('title') 

That is obviously psuedo code, but it should work if you can get your localization into the translateTitle property. 显然,这是伪代码,但是如果您可以将本地化转换为translateTitle属性,则它应该可以工作。 you can put that in your dialog model, or you can put it into your item controller or whatever you are using for that context. 您可以将其放在对话框模型中,也可以将其放入项目控制器或用于该上下文的任何对象。

UPDATE:: 更新::

Okay, so took a look at that library. 好吧,所以看看那个图书馆。 You can also try updating your controller or model with the Em.I18n.TranslateableProperties mixin to allow you to bind a translated property: 您还可以尝试使用Em.I18n.TranslateableProperties mixin更新控制器或模型,以使您可以绑定翻译后的属性:

App.DialogController = Ember.ObjectController.extend(Em.I18n.TranslateableProperties, {
  titleTranslation: 'dialog.title'
});

Good luck, and hope that helps! 祝您好运,希望对您有所帮助!

Just to update the responses here to be a little more up to date! 只是为了更新此处的回复,使其更新一些! As mentioned by @mohamedjahabersadiq, you can use Subexpressions. 如@mohamedjahabersadiq所述,您可以使用Subexpressions。 These also work fine in HTMLBars (>= Ember 1.10.0). 这些在HTMLBars(> = Ember 1.10.0)中也可以正常工作。 However, you don't have to register a new helper, you can use the existing t helper from ember-i18n. 但是,您不必注册新的帮助程序,可以使用ember-i18n中现有的t帮助程序。

{{#modal-dialog title=(t "dialog.title")}}
    Hello
{{/modal-dialog}}

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

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