Skip to content

Commit

Permalink
allowEmpty attribute for format-message, format-html-message, and t h…
Browse files Browse the repository at this point in the history
…elper
  • Loading branch information
jasonmit committed Jan 16, 2017
1 parent f0268e0 commit a84841a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/ember-intl/addon/helpers/format-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import { LiteralWrapper } from './l';

const { get, assert } = Ember;

export function getValue([key], { locale:optionalLocale }) {
export function getValue([key], { allowEmpty, locale:optionalLocale }) {
if (key && key instanceof LiteralWrapper) {
return key.value;
}

assert('[ember-intl] translation lookup attempted but no translation key was provided.', key);

return get(this, 'intl').lookup(key, optionalLocale);
return get(this, 'intl').lookup(key, optionalLocale, {
resilient: allowEmpty
});
}

export default factory('message').extend({
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-intl/addon/services/intl.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ const IntlService = Service.extend(Evented, {
});
},

lookup(key, localeName) {
lookup(key, localeName, options = {}) {
const localeNames = makeArray(localeName || get(this, '_locale'));
const translation = get(this, 'adapter').lookup(localeNames, key);

if (!translation) {
if (!options.resilient && !translation) {
const missingMessage = this.owner.resolveRegistration('util:intl/missing-message');

return missingMessage.call(this, key, localeNames);
Expand Down
6 changes: 6 additions & 0 deletions packages/ember-intl/tests/unit/helpers/format-message-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ test('should throw if called with out a value', function(assert) {
}
});

test('should throw if called with out a value', function(assert) {
assert.expect(1);
this.render(hbs`{{t 'does.not.exist' allowEmpty=true}}`);
assert.equal(this.$().text(), '');
});

test('should return a formatted string', function(assert) {
assert.expect(1);

Expand Down

0 comments on commit a84841a

Please sign in to comment.