diff --git a/addon/services/i18n.js b/addon/services/i18n.js index b0b8d5c1..d4814534 100644 --- a/addon/services/i18n.js +++ b/addon/services/i18n.js @@ -1,4 +1,5 @@ import Ember from "ember"; +import getOwner from 'ember-getowner-polyfill'; import Locale from "../utils/locale"; import addTranslations from "../utils/add-translations"; import getLocales from "../utils/get-locales"; @@ -50,7 +51,7 @@ export default Parent.extend(Evented, { // @public addTranslations: function(locale, translations) { - addTranslations(locale, translations, this.container); + addTranslations(locale, translations, getOwner(this)); this._addLocale(locale); if (this.get('locale').indexOf(locale) === 0) { @@ -60,7 +61,7 @@ export default Parent.extend(Evented, { // @private _initDefaults: on('init', function() { - const ENV = this.container.lookupFactory('config:environment'); + const ENV = getOwner(this)._lookupFactory('config:environment'); if (this.get('locale') == null) { var defaultLocale = (ENV.i18n || {}).defaultLocale; @@ -81,7 +82,7 @@ export default Parent.extend(Evented, { _locale: computed('locale', function() { const locale = this.get('locale'); - return locale ? new Locale(this.get('locale'), this.container) : null; + return locale ? new Locale(this.get('locale'), getOwner(this)) : null; }) }); diff --git a/addon/utils/add-translations.js b/addon/utils/add-translations.js index 6117769d..26e70637 100644 --- a/addon/utils/add-translations.js +++ b/addon/utils/add-translations.js @@ -1,15 +1,12 @@ import Ember from "ember"; -export default function addTranslations(locale, newTranslations, container) { +export default function addTranslations(locale, newTranslations, owner) { const key = `locale:${locale}/translations`; - var existingTranslations = container.lookupFactory(key); + var existingTranslations = owner._lookupFactory(key); if (existingTranslations == null) { existingTranslations = {}; - // CRUFT: there's no public API for registering factories at runtime. - // See http://discuss.emberjs.com/t/whats-the-correct-way-to-register-new-factories-at-runtime/8018 - const registry = container.registry || container._registry; - registry.register(key, existingTranslations); + owner.register(key, existingTranslations); } Ember.merge(existingTranslations, newTranslations); diff --git a/addon/utils/locale.js b/addon/utils/locale.js index 343c905a..a6988da6 100644 --- a/addon/utils/locale.js +++ b/addon/utils/locale.js @@ -4,7 +4,7 @@ const { assert, merge, typeOf, warn } = Ember; // @private // // This class is the work-horse of localization look-up. -function Locale(id, container) { +function Locale(id, owner) { // On Construction: // 1. look for translations in the locale (e.g. pt-BR) and all parent // locales (e.g. pt), flatten any nested keys, and then merge them. @@ -13,23 +13,23 @@ function Locale(id, container) { // 3. Default `rtl` to `false` // 4. Ensure `pluralForm` is defined this.id = id; - this.container = container; + this.owner = owner; this.rebuild(); } Locale.prototype = { rebuild() { - this.translations = getFlattenedTranslations(this.id, this.container); + this.translations = getFlattenedTranslations(this.id, this.owner); this._setConfig(); }, _setConfig() { - walkConfigs(this.id, this.container, (config) => { + walkConfigs(this.id, this.owner, (config) => { if (this.rtl === undefined) { this.rtl = config.rtl; } if (this.pluralForm === undefined) { this.pluralForm = config.pluralForm; } }); - const defaultConfig = this.container.lookupFactory('ember-i18n@config:zh'); + const defaultConfig = this.owner._lookupFactory('ember-i18n@config:zh'); if (this.rtl === undefined) { warn(`ember-i18n: No RTL configuration found for ${this.id}.`, false, { id: 'ember-i18n.no-rtl-configuration' }); @@ -87,8 +87,8 @@ Locale.prototype = { }, _defineMissingTranslationTemplate(key) { - const i18n = this.container.lookup('service:i18n'); - const missingMessage = this.container.lookupFactory('util:i18n/missing-message'); + const i18n = this.owner.lookup('service:i18n'); + const missingMessage = this.owner._lookupFactory('util:i18n/missing-message'); const locale = this.id; function missingTranslation(data) { return missingMessage.call(i18n, locale, key, data); } @@ -99,37 +99,37 @@ Locale.prototype = { }, _compileTemplate(key, string) { - const compile = this.container.lookupFactory('util:i18n/compile-template'); + const compile = this.owner._lookupFactory('util:i18n/compile-template'); const template = compile(string, this.rtl); this.translations[key] = template; return template; } }; -function getFlattenedTranslations(id, container) { +function getFlattenedTranslations(id, owner) { const result = {}; const parentId = parentLocale(id); if (parentId) { - merge(result, getFlattenedTranslations(parentId, container)); + merge(result, getFlattenedTranslations(parentId, owner)); } - const translations = container.lookupFactory(`locale:${id}/translations`) || {}; + const translations = owner._lookupFactory(`locale:${id}/translations`) || {}; merge(result, withFlattenedKeys(translations)); return result; } // Walk up confiugration objects from most specific to least. -function walkConfigs(id, container, fn) { - const appConfig = container.lookupFactory(`locale:${id}/config`); +function walkConfigs(id, owner, fn) { + const appConfig = owner._lookupFactory(`locale:${id}/config`); if (appConfig) { fn(appConfig); } - const addonConfig = container.lookupFactory(`ember-i18n@config:${id}`); + const addonConfig = owner._lookupFactory(`ember-i18n@config:${id}`); if (addonConfig) { fn(addonConfig); } const parentId = parentLocale(id); - if (parentId) { walkConfigs(parentId, container, fn); } + if (parentId) { walkConfigs(parentId, owner, fn); } } function parentLocale(id) { diff --git a/app/instance-initializers/ember-i18n.js b/app/instance-initializers/ember-i18n.js index c0d51315..cdb6b37b 100644 --- a/app/instance-initializers/ember-i18n.js +++ b/app/instance-initializers/ember-i18n.js @@ -8,6 +8,7 @@ export default { initialize(appOrAppInstance) { if (legacyHelper != null) { + // Used for Ember < 1.13 const i18n = appOrAppInstance.container.lookup('service:i18n'); i18n.localeStream = new Stream(function() { diff --git a/bower.json b/bower.json index 6ba3a66e..97762108 100644 --- a/bower.json +++ b/bower.json @@ -5,7 +5,7 @@ "ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3", "ember-cli-test-loader": "ember-cli-test-loader#0.1.3", "ember-load-initializers": "ember-cli/ember-load-initializers#0.1.5", - "ember-qunit": "0.4.6", + "ember-qunit": "0.4.16", "ember-qunit-notifications": "0.0.7", "ember-resolver": "~0.1.18", "jquery": ">=1.11.1 <3.0.0", diff --git a/package.json b/package.json index a2c7679f..c34fa3cf 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "license": "Apache-2.0", "devDependencies": { "broccoli-asset-rev": "^2.1.2", - "ember-cli": "^1.13.8", + "ember-cli": "1.13.8", "ember-cli-app-version": "^0.5.0", "ember-cli-content-security-policy": "^0.4.0", "ember-cli-dependency-checker": "^1.0.1", @@ -42,7 +42,8 @@ "ember-addon" ], "dependencies": { - "ember-cli-babel": "^5.0.0" + "ember-cli-babel": "^5.0.0", + "ember-getowner-polyfill": "^1.0.0" }, "ember-addon": { "configPath": "tests/dummy/config"