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

Commit

Permalink
Reserve locale and htmlSafe attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Mitchell committed Jun 28, 2016
1 parent 52b38c8 commit 1db3ff2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
22 changes: 16 additions & 6 deletions addon/services/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const Parent = Ember.Service || Ember.Object;

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

// @public
// The user's locale.
locale: null,
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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;
})

});
32 changes: 30 additions & 2 deletions tests/unit/i18n-t-test.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 = [];
Expand Down

0 comments on commit 1db3ff2

Please sign in to comment.