This repository was archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 182
Update helper to allow binding hash to be passed #423
Closed
Closed
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0a253fc
Update helper to allow binding hash to be passed
k-fish ee9863a
Fix jshint and use lazy context merging instead.
k-fish 6778434
Add acceptance test for contextObject usage
9d3b4c3
Check for context hash keys with getter instead of extending
c3063dd
Use better syntax for check context hash or object
74ad7b1
Fix legacy helper and test
c2083f2
Add test to check that hash will always override positional context f…
91a0ba9
use "component" integration test for {{t}} helper
jamesarosen 1cd8ad2
Merge remote-tracking branch 'origin/integration-tests' into patch-1
9c46fbe
Add integration style tests for context object
33c2211
Address PR Comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,27 @@ | ||
| import Ember from "ember"; | ||
|
|
||
| export default Ember.Helper.extend({ | ||
| i18n: Ember.inject.service(), | ||
| const { get, inject, Helper, Object: EmberObject, observer } = Ember; | ||
|
|
||
| compute: function(params, interpolations) { | ||
| const key = params[0]; | ||
| const i18n = this.get('i18n'); | ||
| return i18n.t(key, interpolations); | ||
| function mergedContext(objectContext, hashContext) { | ||
| return EmberObject.extend({ | ||
| unknownProperty(key) { | ||
| const fromHash = get(hashContext, key); | ||
| return fromHash === undefined ? get(objectContext, key) : fromHash; | ||
| } | ||
| }).create(); | ||
| } | ||
|
|
||
| export default Helper.extend({ | ||
| i18n: inject.service(), | ||
|
|
||
| compute([key, contextObject = {}, ...rest], interpolations) { | ||
| const mergedInterpolations = mergedContext(contextObject, interpolations); | ||
|
|
||
| const i18n = get(this, 'i18n'); | ||
| return i18n.t(key, mergedInterpolations); | ||
| }, | ||
|
|
||
| _recomputeOnLocaleChange: Ember.observer('i18n.locale', function() { | ||
| _recomputeOnLocaleChange: observer('i18n.locale', function() { | ||
| this.recompute(); | ||
| }) | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,8 @@ export default { | |
| 'no.interpolations.either': 'another text without interpolations', | ||
|
|
||
| 'with': { | ||
| interpolations: 'Clicks: {{clicks}}' | ||
| interpolations: 'Clicks: {{clicks}}', | ||
| interpolationsWithContextObject: 'Clicks: {{clicks}}, Clicks from hash: {{otherClicks}}' | ||
|
||
| }, | ||
|
|
||
| 'pluralized.translation': { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,3 @@ | ||
| <section> | ||
| <span class='no-interpolations'>{{t "no.interpolations"}}</span> | ||
| </section> | ||
|
|
||
| <section> | ||
| <span class='static-interpolations'>{{t "with.interpolations" clicks="45"}}</span> | ||
| </section> | ||
|
|
||
| <section> | ||
| <button class='increment' {{action "increment"}}>Increment Click Count</button> | ||
|
|
||
| <span class='dynamic-interpolations'>{{t "with.interpolations" clicks=clickCount}}</span> | ||
| </section> | ||
|
|
||
| <section> | ||
| <button class='change-dynamic-key' {{action "changeDynamicKey" "no.interpolations.either"}}>Change dynamic key</button> | ||
|
|
||
| <span class='dynamic-key'>{{t dynamicKey}}</span> | ||
| </section> | ||
|
|
||
| <section> | ||
| <button class='switch-to-es' {{action "switchLocale" "es"}}>Switch to Spanish</button> | ||
| <section class='no-interpolations'> | ||
| {{t 'no.interpolations'}} | ||
| </section> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import Ember from 'ember'; | ||
| import hbs from 'htmlbars-inline-precompile'; | ||
| import { moduleForComponent, test } from 'ember-qunit'; | ||
| const { run } = Ember; | ||
|
|
||
| moduleForComponent('no-such-component', 't helper', { | ||
| integration: true, | ||
|
|
||
| beforeEach() { | ||
| if (Ember.Helper == null) { | ||
| const initializer = this.container.lookupFactory('initializer:ember-i18n-legacy-helper'); | ||
| initializer.initialize(this.registry); | ||
| } | ||
|
|
||
| const i18n = this.i18n = this.container.lookup('service:i18n'); | ||
| i18n.set('locale', 'en'); | ||
|
|
||
| this.addTranslations = function(locale, translations) { | ||
| run(i18n, 'addTranslations', locale, translations); | ||
| }; | ||
| }, | ||
|
|
||
| afterEach() { | ||
| run(this.i18n, 'destroy'); | ||
| } | ||
| }); | ||
|
|
||
| test('simple text, static key', function(assert) { | ||
| this.addTranslations('en', { 'foo.bar': 'Foo Bar' }); | ||
|
|
||
| this.render(hbs`{{t 'foo.bar'}}`); | ||
| assert.equal(this.$().text(), 'Foo Bar'); | ||
| }); | ||
|
|
||
| test('text with HTML, static key', function(assert) { | ||
| this.addTranslations('en', { name: '<em>Name</em>:' }); | ||
|
|
||
| this.render(hbs`{{t 'name'}}`); | ||
| assert.equal(this.$().html(), '<em>Name</em>:'); | ||
| }); | ||
|
|
||
| test('dynamic key', function(assert) { | ||
| this.addTranslations('en', { foo: 'Foo', bar: 'Bar' }); | ||
| this.set('someKey', 'foo'); | ||
|
|
||
| this.render(hbs`{{t someKey}}`); | ||
| assert.equal(this.$().text(), 'Foo'); | ||
|
|
||
| run(this, 'set', 'someKey', 'bar'); | ||
| assert.equal(this.$().text(), 'Bar'); | ||
| }); | ||
|
|
||
| test('interpolations', function(assert) { | ||
| this.addTranslations('en', { 'bowl of soup': 'A bowl of {{soup}}' }); | ||
| this.set('soup', 'bisque'); | ||
|
|
||
| this.render(hbs`{{t 'bowl of soup' soup=soup}}`); | ||
| assert.equal(this.$().text(), 'A bowl of bisque'); | ||
|
|
||
| run(this, 'set', 'soup', 'clam chowder'); | ||
| assert.equal(this.$().text(), 'A bowl of clam chowder'); | ||
| }); | ||
|
|
||
| test('interpolations with passed context', function(assert) { | ||
| this.addTranslations('en', { 'bowl of soup': 'A bowl of {{soup}}' }); | ||
| this.set('soup', 'bisque'); | ||
|
|
||
| this.set('contextObject', Ember.computed('soup', function() { | ||
| return { | ||
| soup: this.get('soup') | ||
| }; | ||
| })); | ||
|
|
||
| this.render(hbs`{{t 'bowl of soup' contextObject}}`); | ||
| assert.equal(this.$().text(), 'A bowl of bisque'); | ||
|
|
||
| run(this, 'set', 'soup', 'clam chowder'); | ||
| assert.equal(this.$().text(), 'A bowl of clam chowder'); | ||
| }); | ||
|
|
||
| test('interpolations with passed context and a hash', function(assert) { | ||
| this.addTranslations('en', { 'bowl of soup': 'A bowl of {{soup}} or {{salad}}' }); | ||
| this.set('soup', 'bisque'); | ||
| this.set('salad', 'mixed greens'); | ||
|
|
||
| this.set('contextObject', Ember.computed('soup', function() { | ||
| return { | ||
| soup: this.get('soup') | ||
| }; | ||
| })); | ||
|
|
||
| this.render(hbs`{{t 'bowl of soup' contextObject salad=salad}}`); | ||
| assert.equal(this.$().text(), 'A bowl of bisque or mixed greens'); | ||
|
|
||
| run(this, 'set', 'soup', 'clam chowder'); | ||
| run(this, 'set', 'salad', 'cobb salad'); | ||
| assert.equal(this.$().text(), 'A bowl of clam chowder or cobb salad'); | ||
| }); | ||
|
|
||
| test('interpolations with passed context and a hash - hash overrides context', function(assert) { | ||
| this.addTranslations('en', { 'A delicious entree': 'A delicious {{entree}}' }); | ||
| this.set('entreeFromContext', 'steak'); | ||
| this.set('entreeFromHash', 'pasta'); | ||
|
|
||
| this.set('contextObject', Ember.computed('entreeFromContext', function() { | ||
| return { | ||
| soup: this.get('entreeFromContext') | ||
| }; | ||
| })); | ||
|
|
||
| this.render(hbs`{{t 'A delicious entree' contextObject entree=entreeFromHash}}`); | ||
| assert.equal(this.$().text(), 'A delicious pasta'); | ||
|
|
||
| run(this, 'set', 'entreeFromContext', 'burger'); | ||
| run(this, 'set', 'entreeFromHash', 'pizza'); | ||
| assert.equal(this.$().text(), 'A delicious pizza'); | ||
| }); | ||
|
|
||
| test('locale change', function(assert) { | ||
| this.addTranslations('en', { soup: 'Soup' }); | ||
| this.addTranslations('zh', { soup: '湯' }); | ||
|
|
||
| this.render(hbs`{{t 'soup'}}`); | ||
| assert.equal(this.$().text(), 'Soup'); | ||
|
|
||
| run(this.i18n, 'set', 'locale', 'zh'); | ||
| assert.equal(this.$().text(), '湯'); | ||
| }); | ||
|
|
||
| test('pluralization', function(assert) { | ||
| this.addTranslations('en', { | ||
| bowls: { | ||
| one: 'one bowl', | ||
| other: '{{count}} bowls' | ||
| } | ||
| }); | ||
| run(this, 'set', 'numberOfBowls', 0); | ||
|
|
||
| this.render(hbs`{{t 'bowls' count=numberOfBowls}}`); | ||
| assert.equal(this.$().text(), '0 bowls'); | ||
|
|
||
| run(this, 'set', 'numberOfBowls', 1); | ||
| assert.equal(this.$().text(), 'one bowl'); | ||
|
|
||
| run(this, 'set', 'numberOfBowls', 2); | ||
| assert.equal(this.$().text(), '2 bowls'); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're going to run into problems when a property of
objectContextchanges. We'll want a test forwhere we change
obj.somePropertyandhashValueand make sure both are reflected in the translated text.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a fair point. I'll add a test.