如何在初始化程序中设置i18n.locale

时间:2015-06-11 08:14:57

标签: ember.js ember-cli ember-i18n

我想设置' i18n.locale'来自EmberJS的初始化者。

我通过Ember-CLI使用1.12和ember-i18n。

我试过这个

import Ember from 'ember';

export function initialize(container, application) {
  Ember.set('i18n.locale', 'de');
}

export default {
  name: 'user-locale',
  initialize: initialize
}

但正在接​​收Assertion Failed: Path 'i18n.locale' must be global if no obj is given.

1 个答案:

答案 0 :(得分:2)

您可以i18n作为service:i18n并使用instance-initializer设置i18n.locale

//app/instance-initializers/user-locale.js
export function initialize(instance) {
  var i18n = instance.container.lookup('service:i18n');
  i18n.set('locale', 'de');
}

export default {
  name: 'user-locale',
  after: "ember-i18n",
  initialize: initialize
}

InstanceInitializers指南:

http://emberjs.com/deprecations/v1.x/#toc_access-to-instances-in-initializers

http://emberjs.com/blog/2015/05/13/ember-1-12-released.html#toc_instance-initializers

从Ember 1.13.8开始,您现在必须将容器作为函数访问

const i18n = instance.container().lookup('service:i18n');