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

Ember 1.13 and 2.0 support #227

Merged
merged 2 commits into from
Jun 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite this, I get a "helper t not found" error message whenever I try to use it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. Robert is going to land emberjs/rfcs#58 into 1.13 which will give you {{t without any explicit registration step.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I was basing this off of https://github.com/emberjs/ember.js/pull/11278/files#diff-5d4704d516d9bedb9605e499dc0a88d8R122

I guess once dashless-autolookup lands I'll want a single app/helpers/t.js that exports either legacyHelper or Helper, depending on the presence of Ember.Helper. That'll be easy :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm.

}

instance.registry.injection('component', 'i18n', 'service:i18n');
instance.registry.injection('controller', 'i18n', 'service:i18n');
instance.registry.injection('route', 'i18n', 'service:i18n');
Expand Down
8 changes: 5 additions & 3 deletions tests/acceptance/t-helper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var app;
module('Acceptance: {{t}} Helper', {
beforeEach: function() {
app = startApp();
visit('/in/en');
visit('/');
},

afterEach: function() {
Expand Down Expand Up @@ -44,7 +44,9 @@ test('updates when dynamic interpolations change', function(assert) {
});

test('updates when the locale changes', function(assert) {
visit('/in/es');
click('.switch-to-es');

assert.textIs('.no-interpolations', 'texto sin interpolaciones');
andThen(function() {
assert.textIs('.no-interpolations', 'texto sin interpolaciones');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import $ from "jquery";

export default Ember.Controller.extend({

i18n: Ember.inject.service(),
clickCount: 0,
dynamicKey: 'no.interpolations',

actions: {
increment: function() {
Expand All @@ -12,6 +14,10 @@ export default Ember.Controller.extend({

changeDynamicKey(newKey) {
this.set('dynamicKey', newKey);
},

switchLocale(locale) {
this.set('i18n.locale', locale);
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var Router = Ember.Router.extend({
});

Router.map(function() {
this.route('translate', { path: '/in/:locale' });
});

export default Router;
20 changes: 0 additions & 20 deletions tests/dummy/app/routes/translate.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@

<span class='dynamic-key'>{{t dynamicKey}}</span>
</section>

<section>
<button class='switch-to-es' {{action "switchLocale" "es"}}>Switch to Spanish</button>
</section>