简体   繁体   English

i18n-如何在application.hbs中使用它?

[英]i18n - How to use it in application.hbs?

In which route/component should I inject the "i18n" service for it to be used in application.hbs? 我应该在哪个路由/组件中注入“ i18n”服务,以便在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. 我正在尝试在其他HBS文件中使用它,并且如果将“ i18n”注入到路由/组件中,则可以使用它。

But it's just not working in application.hbs 但这在application.hbs中不起作用

Generally you dont have to inject the i18n service to use the t helper, which is what you usually do from a template. 通常,您不必注入i18n服务即可使用t helper,这通常是从模板执行的操作。

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. 因此,您需要将服务注入到application控制器中才能在application模板上使用它。

You can do it by using an instance initializer and inject i18n service to your all routes and components by using this code: 您可以使用实例初始化程序来完成此操作,并使用以下代码将i18n服务注入所有路由和组件:

/instance-initializers/component-route-i18n-injector.js /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 . 您可以看一下这个玩笑

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

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