From 1db3ff27182a0bbd79ad881f4a6b442a42a90650 Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Tue, 28 Jun 2016 13:08:13 -0700 Subject: [PATCH] Reserve locale and htmlSafe attributes --- addon/services/i18n.js | 22 ++++++++++++++++------ tests/unit/i18n-t-test.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/addon/services/i18n.js b/addon/services/i18n.js index d4814534..678f6f48 100644 --- a/addon/services/i18n.js +++ b/addon/services/i18n.js @@ -9,7 +9,6 @@ const Parent = Ember.Service || Ember.Object; // @public export default Parent.extend(Evented, { - // @public // The user's locale. locale: null, @@ -22,7 +21,15 @@ export default Parent.extend(Evented, { // // Returns the translation `key` interpolated with `data` // in the current `locale`. - t: function(key, data = {}) { + t(key, data = {}) { + Ember.deprecate('locale is a reserved attribute', !data.hasOwnProperty('locale'), { + id: 'ember-i18n.reserve-locale' + }); + + Ember.deprecate('htmlSafe is a reserved attribute', !data.hasOwnProperty('htmlSafe'), { + id: 'ember-i18n.reserve-htmlSafe' + }); + const locale = this.get('_locale'); assert("I18n: Cannot translate when locale is null", locale); const count = get(data, 'count'); @@ -40,7 +47,7 @@ export default Parent.extend(Evented, { }, // @public - exists: function(key, data = {}) { + exists(key, data = {}) { const locale = this.get('_locale'); assert("I18n: Cannot check existance when locale is null", locale); const count = get(data, 'count'); @@ -50,7 +57,7 @@ export default Parent.extend(Evented, { }, // @public - addTranslations: function(locale, translations) { + addTranslations(locale, translations) { addTranslations(locale, translations, getOwner(this)); this._addLocale(locale); @@ -66,7 +73,10 @@ export default Parent.extend(Evented, { if (this.get('locale') == null) { var defaultLocale = (ENV.i18n || {}).defaultLocale; if (defaultLocale == null) { - warn('ember-i18n did not find a default locale; falling back to "en".', false, { id: 'ember-i18n.default-locale' }); + warn('ember-i18n did not find a default locale; falling back to "en".', false, { + id: 'ember-i18n.default-locale' + }); + defaultLocale = 'en'; } this.set('locale', defaultLocale); @@ -82,7 +92,7 @@ export default Parent.extend(Evented, { _locale: computed('locale', function() { const locale = this.get('locale'); + return locale ? new Locale(this.get('locale'), getOwner(this)) : null; }) - }); diff --git a/tests/unit/i18n-t-test.js b/tests/unit/i18n-t-test.js index be5aba2a..d92e7a75 100644 --- a/tests/unit/i18n-t-test.js +++ b/tests/unit/i18n-t-test.js @@ -1,10 +1,14 @@ import Ember from 'ember'; import { moduleFor, test } from 'ember-qunit'; -const { run } = Ember; +const { run, deprecate:originalDeprecate } = Ember; moduleFor('service:i18n', 'I18nService#t', { - integration: true + integration: true, + + afterEach() { + Ember.deprecate = originalDeprecate; + } }); test('falls back to parent locale', function(assert) { @@ -36,6 +40,30 @@ test('returns "missing translation" translations', function(assert) { assert.equal('Missing translation: not.yet.translated', result); }); +test('warns on the presence of htmlSafe and locale', function(assert) { + const service = this.subject(); + let deprecations = 0; + + Ember.deprecate = function(message, predicate, options = {}) { + if (predicate) { return; } + + const { id } = options; + + if (id === 'ember-i18n.reserve-htmlSafe' || id === 'ember-i18n.reserve-locale') { + deprecations += 1; + } + }; + + service.t('not.yet.translated', { htmlSafe: true }); + assert.equal(deprecations, 1); + + service.t('not.yet.translated', { locale: true }); + assert.equal(deprecations, 2); + + service.t('not.yet.translated'); + assert.equal(deprecations, 2); +}); + test('emits "missing" events', function(assert) { const i18n = this.subject({ locale: 'en' }); const calls = [];