Search code examples
ember.jsember-i18n

Using i18n in Mixin?


Is there a way to use i18n in Mixin. I'd like to create Mixins as configurations for my fields, and I need to translate labels, but as far as I've read https://github.com/jamesarosen/ember-i18n/wiki/Doc:-i18n-Service I don't see an opportunity to implement that.


Solution

  • As Kitler noted, you can inject the i18n service to your mixins, so you can use them in your components.

    export default Ember.Mixin.create({
      i18n: Ember.inject.service(),
    
      translate: function(key, options) {
        return this.get('i18n').t(key, options);
      }
    });
    

    What do you expect more? Do you want to modify all of the labels as Decorator? If you want to do decorate label of a component by a mixin than you should do:

    export default Ember.Mixin.create({
      i18n: Ember.inject.service(),
    
      translate: function(key, options) {
        return this.get('i18n').t(key, options);
      },
    
      init(){ //init or didReceiveAttrs 
        this._super(...arguments);
        this.set('label', this.translate(this.get('label')));
      }
    });