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

Commit

Permalink
ember 1.13 and 2.0 support
Browse files Browse the repository at this point in the history
 1. Starting work on support for Ember 1.13. Glimmer breaks the old
    helper and adds a new `Ember.Helper` with more of the power we
    need.
 2. v1.13 introduces a new `Ember.Service` that should
    be the parent of the `service:i18n`.
 3. Ember 2.0 removes `Ember.EnumerableUtils`, instead assuming
    all supported browsers supply the ES5 enumerable methods.
  • Loading branch information
jamesarosen committed Jun 15, 2015
1 parent a42a845 commit 7bf2973
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 32 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ env:
matrix:
fast_finish: true
allow_failures:
- env: EMBER_TRY_SCENARIO=ember-beta
- env: EMBER_TRY_SCENARIO=ember-canary

before_install:
Expand Down
42 changes: 16 additions & 26 deletions addon/helper.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import Ember from "ember";
import Stream from "./stream";
import { readHash } from "./stream";

// @public
export default function t(params, hash, options, env) {
const i18n = env.data.view.container.lookup('service:i18n');
const i18nKey = params[0];
var Helper = null;

var out = new Stream(function() {
const value = i18nKey.isStream ? i18nKey.value() : i18nKey;
return value === undefined ? '' : i18n.t(value, readHash(hash));
});

// observe any hash arguments that are streams:
Ember.keys(hash).forEach(function(key) {
const value = hash[key];

if (value && value.isStream) {
value.subscribe(out.notify, out);
}
});
if (Ember.Helper) {
Helper = Ember.Helper.extend({
i18n: Ember.inject.service(),

// observe the locale:
i18n.localeStream.subscribe(out.notify, out);
_locale: Ember.computed.readOnly('i18n.locale'),

// if the i18n key itself is dynamic, observe it:
if (i18nKey.isStream) {
i18nKey.subscribe(out.notify, out);
}
compute: function(params, interpolations) {
const key = params[0];
const i18n = this.get('i18n');
return i18n.t(key, interpolations);
},

return out;
_recomputeOnLocaleChange: Ember.observer('_locale', function() {
this.recompute();
})
});
}

export default Helper;
39 changes: 39 additions & 0 deletions addon/legacy-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Ember from "ember";
import Stream from "./stream";
import { readHash } from "./stream";

var helper = null;

if (Ember.Helper == null) {
// @public
helper = function tHelper(params, hash, options, env) {
const i18n = env.data.view.container.lookup('service:i18n');
const i18nKey = params[0];

var out = new Stream(function() {
const value = i18nKey.isStream ? i18nKey.value() : i18nKey;
return value === undefined ? '' : i18n.t(value, readHash(hash));
});

// observe any hash arguments that are streams:
Ember.keys(hash).forEach(function(key) {
const value = hash[key];

if (value && value.isStream) {
value.subscribe(out.notify, out);
}
});

// observe the locale:
i18n.localeStream.subscribe(out.notify, out);

// if the i18n key itself is dynamic, observe it:
if (i18nKey.isStream) {
i18nKey.subscribe(out.notify, out);
}

return out;
};
}

export default helper;
3 changes: 1 addition & 2 deletions addon/macro.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Ember from "ember";

const map = Ember.EnumerableUtils.map;
const keys = Ember.keys;
const get = Ember.get;

Expand All @@ -14,7 +13,7 @@ export default function createTranslatedComputedProperty(key, interpolations = {
}

function values(object) {
return map(keys(object), (key) => object[key]);
return keys(object).map((key) => object[key]);
}

function mapPropertiesByHash(object, hash) {
Expand Down
3 changes: 2 additions & 1 deletion addon/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import Locale from "./locale";
import addTranslations from "./add-translations";

const get = Ember.get;
const Parent = Ember.Service || Ember.Object;

// @public
export default Ember.Object.extend(Ember.Evented, {
export default Parent.extend(Ember.Evented, {

// @public
// The user's locale.
Expand Down
12 changes: 10 additions & 2 deletions app/instance-initializers/ember-i18n.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ember from "ember";
import helper from "ember-i18n/helper";
import legacyHelper from "ember-i18n/legacy-helper";
import Helper from "ember-i18n/helper";
import ENV from '../config/environment';

export default {
Expand All @@ -13,7 +14,14 @@ export default {
}
instance.container.lookup('service:i18n').set('locale', defaultLocale);

Ember.HTMLBars._registerHelper('t', helper);
if (legacyHelper != null) {
Ember.HTMLBars._registerHelper('t', legacyHelper);
}

if (Helper != null) {
instance.registry.register('helper:t', Helper);
}

instance.registry.injection('component', 'i18n', 'service:i18n');
instance.registry.injection('controller', 'i18n', 'service:i18n');
instance.registry.injection('route', 'i18n', 'service:i18n');
Expand Down

0 comments on commit 7bf2973

Please sign in to comment.