Skip to content

Commit

Permalink
Closes softlayer#6
Browse files Browse the repository at this point in the history
  • Loading branch information
juwara0 committed Dec 1, 2015
1 parent c4f6af7 commit be9f3aa
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
4 changes: 2 additions & 2 deletions addon/components/sl-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ export default Ember.Component.extend({
translateString() {
const parametersHash = {};

Object.keys( this ).map( key => {
if ( /^\$\d+/.test( key ) ) {
Object.keys( this.attrs ).map( key => {
if ( /^\$\w+/.test( key ) ) {
parametersHash[key] = this.get( key );
}
});
Expand Down
2 changes: 1 addition & 1 deletion addon/services/sl-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default Ember.Service.extend({
};

if ( 1 === pluralErrorTracker ) {
Ember.warn( 'If either "pluralKey" or "pluralCount" are provided then both must be.' +
Ember.warn( 'If either "pluralKey" or "pluralCount" are provided then both must be. ' +
'Singular key value was returned.' );

return getTokenValue( token );
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/app/routes/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default Ember.Route.extend({
'SIMPLE_KEY': 'I have been translated',
'SINGULAR_KEY': 'View my family',
'PLURAL_KEY': 'View my families',
'REPLACED_KEY': 'I have replaced {0} and {1}'
'REPLACED_KEY': 'I have replaced {0} and {TEST}'
});
},

Expand Down
6 changes: 3 additions & 3 deletions tests/dummy/app/templates/demo.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<br><br>
<h4>Replaced Values In Keys</h4>
Original String: {{sl-translate key="REPLACED_KEY"}}<br>
Replaced String: {{sl-translate key="REPLACED_KEY" $0="First" $1="Unicorn"}}
Replaced String: {{sl-translate key="REPLACED_KEY" $0="First" $TEST="Unicorn"}}

<br><br>
<h4>Bound Replacement Values In Keys</h4>
String Will Update: {{sl-translate key="REPLACED_KEY" $0="First" $1=valueToDisplay}}
String Will Update: {{sl-translate key="REPLACED_KEY" $0="First" $TEST=valueToDisplay}}
<br><br>
<button {{action "updateStringValues"}}>Update Values</button>
<br><br>

<h4>Used alongside other properties or attribute bindings</h4>
{{sl-translate tagName="em" key="REPLACED_KEY" $0="First" $1="Dragon"}}
{{sl-translate tagName="em" key="REPLACED_KEY" $0="First" $TEST="Dragon"}}
8 changes: 4 additions & 4 deletions tests/integration/components/sl-translate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ moduleForComponent( 'sl-translate', 'Integration | Component | sl translate', {
'SIMPLE_KEY': 'I have been translated',
'SINGULAR_KEY': 'View my family',
'PLURAL_KEY': 'View my families',
'REPLACED_KEY': 'I have replaced {0} and {1}'
'REPLACED_KEY': 'I have replaced {0} and {test}'
})
);
},
Expand Down Expand Up @@ -59,12 +59,12 @@ test( 'Replaced Values In Keys', function( assert ) {

assert.strictEqual(
this.$( '>:first-child' ).text().trim(),
'I have replaced {0} and {1}',
'I have replaced {0} and {test}',
'Original String was correct'
);

this.render( hbs`
{{sl-translate key="REPLACED_KEY" $0="First" $1="Unicorn"}}
{{sl-translate key="REPLACED_KEY" $0="First" $test="Unicorn"}}
` );

assert.strictEqual(
Expand All @@ -79,7 +79,7 @@ test( 'Bound Replacement Values In Keys', function( assert ) {
this.set( 'valueToDisplay', 'the Bound Value' );

this.render( hbs`
{{sl-translate key="REPLACED_KEY" $0="First" $1=valueToDisplay}}
{{sl-translate key="REPLACED_KEY" $0="First" $test=valueToDisplay}}
` );

assert.strictEqual(
Expand Down
85 changes: 66 additions & 19 deletions tests/unit/components/sl-translate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const translateService = Ember.Object.create({
this.set( 'pluralKey', data.pluralKey );
this.set( 'pluralCount', data.pluralCount );
this.set( 'parameters', data.parameters );

return 'TRANSLATE: ' + data.key;
}
});
Expand Down Expand Up @@ -41,12 +40,9 @@ test( 'Default property values', function( assert ) {

test( 'setTranslatedString() is called when the willRender() event occurs', function( assert ) {
const component = this.subject( {
translateService,
key: 'the_key',
pluralKey: 'plural_key',
pluralCount: 'plural_count',
$0: 'a',
$1: 'b'
setTranslatedString: function() {
return;
}
});

const spy = sinon.spy( component, 'setTranslatedString' );
Expand All @@ -64,12 +60,9 @@ test(
'setTranslatedString() sets internalTranslatedString property with value from translateString()',
function( assert ) {
const component = this.subject({
translateService,
key: 'the_key',
pluralKey: 'plural_key',
pluralCount: 'plural_count',
$0: 'a',
$1: 'b'
translateString: function() {
return 'TRANSLATE: the_key';
}
});

const spy = sinon.spy( component, 'translateString' );
Expand Down Expand Up @@ -102,6 +95,8 @@ test( 'translateString() returns expected value', function( assert ) {
$1: 'b'
});

this.render();

assert.equal(
component.translateString(),
'TRANSLATE: the_key',
Expand All @@ -120,6 +115,8 @@ test( 'setTranslatedString() sets internalTranslatedString and translatedString
$1: 'b'
});

this.render();

Ember.run( () => {
component.setTranslatedString();
});
Expand All @@ -142,7 +139,11 @@ test( 'translatedString() returns correct value', function( assert ) {
$1: 'b'
} );

component.trigger( 'willRender' );
this.render();

Ember.run( () => {
component.trigger( 'willRender' );
});

assert.strictEqual(
component.get( 'translatedString' ),
Expand All @@ -152,14 +153,32 @@ test( 'translatedString() returns correct value', function( assert ) {
});

test( 'translateString() calls translateKey() on the translation service with given values', function( assert ) {
this.subject({
const component = this.subject({
translateService,
key: 'the_key',
pluralKey: 'plural_key',
pluralCount: 'plural_count',
$0: 'a',
$1: 'b',
c: 'c'
c: 'c',
$lowercase: 'lowercase',
$Uppercase: 'Uppercase',
$CAPITALS: 'CAPITALS'
});

Ember.run( () => {
component.set( 'attrs', {
key: 'the_key',
pluralKey: 'plural_key',
pluralCount: 'plural_count',
$0: 'a',
$1: 'b',
c: 'c',
$lowercase: 'lowercase',
$Uppercase: 'Uppercase',
$CAPITALS: 'CAPITALS'
});
component.trigger( 'willRender' );
});

assert.strictEqual(
Expand All @@ -176,7 +195,13 @@ test( 'translateString() calls translateKey() on the translation service with gi
);
assert.deepEqual(
translateService.get( 'parameters' ),
{ $0: 'a', $1: 'b' }
{
$0: 'a',
$1: 'b',
$lowercase: 'lowercase',
$Uppercase: 'Uppercase',
$CAPITALS: 'CAPITALS'
}
);
});

Expand All @@ -189,16 +214,38 @@ test( 'translateString() only accepts the correct parameter key pattern', functi
$0: 'a',
$12: 'b',
r: 'c',
$10000: 'd'
$10000: 'd',
$lowercase: 'lowercase',
$Uppercase: 'Uppercase',
$CAPITALS: 'CAPITALS'
});

Ember.run( () => {
component.set( 'attrs', {
key: 'the_key',
pluralKey: 'plural_key',
pluralCount: 'plural_count',
$0: 'a',
$12: 'b',
r: 'c',
$10000: 'd',
$lowercase: 'lowercase',
$Uppercase: 'Uppercase',
$CAPITALS: 'CAPITALS'
});
component.translateString();
});

assert.deepEqual(
translateService.get( 'parameters' ),
{ $0: 'a', $12: 'b', $10000: 'd' }
{
$0: 'a',
$12: 'b',
$10000: 'd',
$lowercase: 'lowercase',
$Uppercase: 'Uppercase',
$CAPITALS: 'CAPITALS'
}
);
});

Expand Down

0 comments on commit be9f3aa

Please sign in to comment.