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 #392 from jamesarosen/james/legacy-tree
Browse files Browse the repository at this point in the history
Include ember 1.12 support only on 1.12
  • Loading branch information
jamesarosen authored Sep 9, 2016
2 parents c21e598 + 5f98623 commit 8ce07f5
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 117 deletions.
30 changes: 12 additions & 18 deletions addon/helper.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import Ember from "ember";

var Helper = null;
export default Ember.Helper.extend({
i18n: Ember.inject.service(),

if (Ember.Helper) {
Helper = Ember.Helper.extend({
i18n: Ember.inject.service(),
_locale: Ember.computed.readOnly('i18n.locale'),

_locale: Ember.computed.readOnly('i18n.locale'),
compute: function(params, interpolations) {
const key = params[0];
const i18n = this.get('i18n');
return i18n.t(key, interpolations);
},

compute: function(params, interpolations) {
const key = params[0];
const i18n = this.get('i18n');
return i18n.t(key, interpolations);
},

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

export default Helper;
_recomputeOnLocaleChange: Ember.observer('_locale', function() {
this.recompute();
})
});
44 changes: 0 additions & 44 deletions addon/legacy-helper.js

This file was deleted.

1 change: 1 addition & 0 deletions addon/legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This folder includes support for Ember 1.12
36 changes: 36 additions & 0 deletions addon/legacy/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Stream from "./stream";
import { readHash } from "./stream";

export default 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));
});

// Once the view is destroyed destroy the steam as well
env.data.view.one('willDestroyElement', out, function() {
this.destroy();
});

// observe any hash arguments that are streams:
Object.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;
}
23 changes: 23 additions & 0 deletions addon/legacy/initializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Ember from "ember";
import Stream from './stream';
import legacyHelper from "./helper";

// Used for Ember < 1.13
export default {
name: 'ember-i18n-legacy-helper',

initialize(registry) {
const i18n = registry.lookup('service:i18n');

i18n.localeStream = new Stream(function() {
return i18n.get('locale');
});

Ember.addObserver(i18n, 'locale', i18n, function() {
this.localeStream.value(); // force the stream to be dirty
this.localeStream.notify();
});

Ember.HTMLBars._registerHelper('t', legacyHelper);
}
};
11 changes: 11 additions & 0 deletions addon/legacy/stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Ember from 'ember';

// As of v1.12, Streams are still private API. Thus, we need to reach in to
// Ember internals to get access to it.
//
// See https://github.com/emberjs/ember.js/blob/v1.12.0/packages/ember-metal/lib/main.js#L384-L386
// See https://github.com/emberjs/ember.js/pull/9693
// See https://github.com/dockyard/ember-cli-i18n/blob/v0.0.6/addon/utils/stream.js

export default Ember.__loader.require('ember-metal/streams/stream')['default'];
export const readHash = Ember.__loader.require('ember-metal/streams/utils').readHash;
16 changes: 0 additions & 16 deletions addon/stream.js

This file was deleted.

2 changes: 2 additions & 0 deletions app/initializers/ember-i18n-legacy-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import initializer from 'ember-i18n/legacy/initializer';
export default initializer;
12 changes: 0 additions & 12 deletions app/initializers/ember-i18n.js

This file was deleted.

26 changes: 0 additions & 26 deletions app/instance-initializers/ember-i18n.js

This file was deleted.

36 changes: 35 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
/* jshint node: true */
'use strict';
var Funnel = require('broccoli-funnel');
var VersionChecker = require('ember-cli-version-checker');

module.exports = {
name: 'ember-i18n',
isLocalizationFramework: true,

isLocalizationFramework: true
config: function() {
var checker = new VersionChecker(this);
var dep = checker.for('ember', 'bower');
this.hasEmberHelper = !dep.lt('1.13.0');
return this._super.config.apply(this, arguments);
},

treeFor: function(name) {
var result = this._super.treeFor.apply(this, arguments);

if (this.hasEmberHelper) {
result = exclude(result, [
'initializers/ember-i18n-legacy-helper.js',
'modules/ember-i18n/legacy/helper.js',
'modules/ember-i18n/legacy/initializer.js',
'modules/ember-i18n/legacy/stream.js'
]);
} else {
result = exclude(result, [
'helpers/t.js',
'modules/ember-i18n/helper.js'
]);
}

return result;
}
};

function exclude(tree, files) {
return new Funnel(tree, {
exclude: files
});
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"ember-addon"
],
"dependencies": {
"broccoli-funnel": "^1.0.6",
"ember-cli-babel": "^5.0.0",
"ember-cli-version-checker": "^1.1.6",
"ember-getowner-polyfill": "^1.0.0"
},
"ember-addon": {
Expand Down

0 comments on commit 8ce07f5

Please sign in to comment.