This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, the `{{t}}` helper, like the `i18n.t` utility, accepted a translation key and a context hash. This worked when the Handlebars template had the individual keys and values (or value bindings) for the context, but didn't when there was a pre-built object that represented the context. Now it accepts a second ordered (non-hash) argument that represents the context as an object. Hash context properties override those from the context object. ```hbs {{t 'some.key' someObject prop=value}} ``` is approximately the same as ```js i18n.t('some.key', Object.assign({}, someObject, { prop: value })) ``` This is a workaround for the fact that Handlebars does not yet have a syntax for splatting an object into hash arguments. See handlebars-lang/handlebars.js#1050 See handlebars-lang/handlebars.js#1128 See handlebars-lang/handlebars.js#1149 Closes #423
- Loading branch information
1 parent
7d932a0
commit 0854a3f
Showing
3 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 contains 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