How to set the i18n.locale from within an initializer

半世苍凉 提交于 2019-12-23 02:51:03

问题


I would like to set 'i18n.locale' from within an initializer in EmberJS.

I am using 1.12 via Ember-CLI with ember-i18n.

I tried this

import Ember from 'ember';

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

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

but am receiving Assertion Failed: Path 'i18n.locale' must be global if no obj is given.


回答1:


You could get i18n as service:i18n and use instance-initializer to set 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 guides:

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

As of Ember 1.13.8 you now have to access the container as a function

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



来源:https://stackoverflow.com/questions/30775225/how-to-set-the-i18n-locale-from-within-an-initializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!