diff --git a/addon/helper.js b/addon/helper.js index 4354223c..553a42b9 100644 --- a/addon/helper.js +++ b/addon/helper.js @@ -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(); + }) +}); diff --git a/addon/legacy-helper.js b/addon/legacy-helper.js deleted file mode 100644 index dc0f6c90..00000000 --- a/addon/legacy-helper.js +++ /dev/null @@ -1,44 +0,0 @@ -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)); - }); - - // 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; - }; -} - -export default helper; diff --git a/addon/legacy/README.md b/addon/legacy/README.md new file mode 100644 index 00000000..5d145e66 --- /dev/null +++ b/addon/legacy/README.md @@ -0,0 +1 @@ +This folder includes support for Ember 1.12 diff --git a/addon/legacy/helper.js b/addon/legacy/helper.js new file mode 100644 index 00000000..b18c2274 --- /dev/null +++ b/addon/legacy/helper.js @@ -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; +} diff --git a/addon/legacy/initializer.js b/addon/legacy/initializer.js new file mode 100644 index 00000000..b2070c5b --- /dev/null +++ b/addon/legacy/initializer.js @@ -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); + } +}; diff --git a/addon/legacy/stream.js b/addon/legacy/stream.js new file mode 100644 index 00000000..ba362c67 --- /dev/null +++ b/addon/legacy/stream.js @@ -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; diff --git a/addon/stream.js b/addon/stream.js deleted file mode 100644 index 518b406d..00000000 --- a/addon/stream.js +++ /dev/null @@ -1,16 +0,0 @@ -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 -// -// As of v2.7, Streams are moved to `ember-htmlbars`, we need to check if `ember-metal/streams/stream` exists - -const _registry = Ember.__loader.registry; -const _require = Ember.__loader.require; - -export default _registry['ember-metal/streams/stream'] ? _require('ember-metal/streams/stream')['default'] : _require('ember-htmlbars/streams/stream')['default']; -export const readHash = _registry['ember-metal/streams/utils'] ? _require('ember-metal/streams/utils').readHash : _require('ember-htmlbars/streams/utils').readHash; diff --git a/app/initializers/ember-i18n-legacy-helper.js b/app/initializers/ember-i18n-legacy-helper.js new file mode 100644 index 00000000..9304a4a7 --- /dev/null +++ b/app/initializers/ember-i18n-legacy-helper.js @@ -0,0 +1,2 @@ +import initializer from 'ember-i18n/legacy/initializer'; +export default initializer; diff --git a/app/initializers/ember-i18n.js b/app/initializers/ember-i18n.js deleted file mode 100644 index 51912d1b..00000000 --- a/app/initializers/ember-i18n.js +++ /dev/null @@ -1,12 +0,0 @@ -import instanceInitializer from "../instance-initializers/ember-i18n"; - -export default { - name: instanceInitializer.name, - - initialize: function() { - const application = arguments[1] || arguments[0]; // depending on Ember version - if (application.instanceInitializer) { return; } - - instanceInitializer.initialize(application); - } -}; diff --git a/app/instance-initializers/ember-i18n.js b/app/instance-initializers/ember-i18n.js deleted file mode 100644 index cdb6b37b..00000000 --- a/app/instance-initializers/ember-i18n.js +++ /dev/null @@ -1,26 +0,0 @@ -import Ember from "ember"; -import Stream from 'ember-i18n/stream'; -import legacyHelper from "ember-i18n/legacy-helper"; -import ENV from '../config/environment'; - -export default { - name: 'ember-i18n', - - initialize(appOrAppInstance) { - if (legacyHelper != null) { - // Used for Ember < 1.13 - const i18n = appOrAppInstance.container.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); - } - } -}; diff --git a/index.js b/index.js index 97fb50f0..ab7235c5 100644 --- a/index.js +++ b/index.js @@ -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 + }); +} diff --git a/package.json b/package.json index 0628051d..15223c86 100644 --- a/package.json +++ b/package.json @@ -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": {