i18n - How to use it in application.hbs?

206 Views Asked by At

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

2

There are 2 best solutions below

0
Ahmet Emre Kilinc On

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.

2
Lux On

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.