0

In which route/component should I inject the "i18n" service for it to be used in application.hbs? I'm trying to use it in other HBS files, and if I inject "i18n" into the route/component - I'm able to use it.

But it's just not working in application.hbs

crowmagnumb
  • 6,621
  • 9
  • 33
  • 42
Put Time
  • 71
  • 6
  • I don't use handlebars but you will have to give much more info for those that do. Can you please show the relevant parts of code for which you are successful and for which you are not. As is, not enough info to go on here. – crowmagnumb Nov 15 '17 at 22:24

2 Answers2

1

Generally you dont have to inject the i18n service to use the t helper, which is what you usually do from a template.

But in generally a service needs to be injected to the controller if you want to use it in the routes template. So you need to inject the service to the application controller to use it on your application template.

Lux
  • 17,835
  • 5
  • 43
  • 73
  • By default, application controller is not created - am I right? Should I create one using "ember g controller application"? Because I just see the application.hbs file. No other application.* files are available in the project. Which route actually loads/mounts the application.hbs file? – Put Time Nov 16 '17 at 16:53
  • Yes, just create the file. You always have an application controller. You have a controller for every route. If you don't explicit define it you get a default one. – Lux Nov 16 '17 at 18:59
0

You can do it by using an instance initializer and inject i18n service to your all routes and components by using this code:

/instance-initializers/component-route-i18n-injector.js

import Ember from 'ember';

export function initialize(appInstance) {

  let i18n = appInstance.lookup('service:i18n');
  Ember.Component.reopen({
    i18n: i18n
  });
  Ember.Controller.reopen({
    i18n: i18n
  });
}

export default {
  name: 'component-route-i18n-injector',
  initialize
};

You can take a look at this twiddle.

Ahmet Emre Kilinc
  • 5,489
  • 12
  • 30
  • 42