Skip to content

Commit

Permalink
adds i18n support discussed in #99
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmanuel committed Jan 30, 2014
1 parent e57350e commit 48afb96
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/ember-easyForm/lib/utilities.js
Expand Up @@ -2,8 +2,26 @@ Ember.EasyForm.humanize = function(string) {
return string.underscore().split('_').join(' ').capitalize();
};

Ember.EasyForm.eachTranslatedAttribute = function(object, fn) {
var isTranslatedAttribute = /(.+)Translation$/,
isTranslatedAttributeMatch;

for (var key in object) {
isTranslatedAttributeMatch = key.match(isTranslatedAttribute);
if (isTranslatedAttributeMatch) {
fn.call(object, isTranslatedAttributeMatch[1], Ember.I18n.t(object[key]));
}
}
};

Ember.EasyForm.processOptions = function(property, options) {
if (options) {
if (Ember.I18n) {
Ember.EasyForm.eachTranslatedAttribute(options.hash, function (attribute, translation) {
options.hash[attribute] = translation;
delete options.hash[attribute + 'Translation'];
});
}
options.hash.property = property;
} else {
options = property;
Expand Down
30 changes: 29 additions & 1 deletion packages/ember-easyForm/tests/utilities_test.js
Expand Up @@ -3,5 +3,33 @@ module('EasyForm utility methods', {
});

test('humanizes string', function() {
equal(Ember.EasyForm.humanize("firstName"), 'First name');
equal(Ember.EasyForm.humanize('firstName'), 'First name');
});

test('humanizes key string', function() {
equal(Ember.EasyForm.humanize('users.first_name'), 'Users.first name');
});

test('mutation of options - only property', function() {
equal(Ember.EasyForm.processOptions('firstName'), 'firstName');
});

test('mutation of options - property and options', function() {
var options = {hash: {placeholder: 'First name'}};
deepEqual(Ember.EasyForm.processOptions('firstName', options), {hash: {placeholder: 'First name', property: 'firstName'}});
});

test('mutation of options - property and *Translation options without Ember.I18n', function() {
var options = {hash: {placeholderTranslation: 'users.first_name'}};
deepEqual(Ember.EasyForm.processOptions('firstName', options), {hash: {placeholderTranslation: 'users.first_name', property: 'firstName'}});
});

test('mutation of options - property and *Translation options with Ember.I18n', function() {
Ember.I18n = {
t: function(key) {
return Ember.EasyForm.humanize(key);
}
};
var options = {hash: {placeholderTranslation: 'users.first_name'}};
deepEqual(Ember.EasyForm.processOptions('firstName', options), {hash: {placeholder: 'Users.first name', property: 'firstName'}});
});

0 comments on commit 48afb96

Please sign in to comment.