Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #336 from rwjblue/avoid-deprecations-for-container
Browse files Browse the repository at this point in the history
Use public API for container / registry functions.
  • Loading branch information
jamesarosen committed Nov 17, 2015
2 parents 571f13c + 983e395 commit 2233786
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
7 changes: 4 additions & 3 deletions addon/services/i18n.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
})

});
9 changes: 3 additions & 6 deletions addon/utils/add-translations.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
30 changes: 15 additions & 15 deletions addon/utils/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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' });
Expand Down Expand Up @@ -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); }
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions app/instance-initializers/ember-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down

0 comments on commit 2233786

Please sign in to comment.