-
Notifications
You must be signed in to change notification settings - Fork 1k
Variable Replacement Enhancement #188
Comments
Hey! You can pass dynamic data through translate directive by just using So translate="{{foo.bar}} " should do the trick.
|
True but that won't do message variable replacement will it? "translate="{{foo.bar}}" will translate the value of the scope variable foo.bar (whatever value that is) but there's no parameter being passed / replaced. In my use case, I need to have some variable in the translatable message replaced with something variable (e.g. "Hello {{fisrtName}}"). Does that make sense? |
@totof3110 Sorry, miss-read your question. So what you want, is actually a bit more declarative way to pass variable values through The downside would be that when having 3 or maybe 4 variables values, the usage of the directive could in such cases look like this: <ANY
translate="FOO"
translate-foo="bar.baz"
translate-bar="baz.bar"
translate-baz="foo.bar"
></ANY> However, in that kinda case, one would probably switch over to Thinking about the implementation.. we have to compile the directive by hand since we have to determine what We also have to introduce a different type of value determination, since we wouldn't let angular (or actually interpolate directive) do the interpolation anymore for us. We now have expressions that Have to be evaluated. Another problem would be namespace conflicts. Regarding the example above, we have 3 different This means we have to at least introduce another namespace reserved for that kind of values. So sth. like So, I don't want to smash your idea down here. Actually, I like what you're requesting, since, like I said, the outcome would be pretty cool. I'm just thinking out loud. I would like to ping @DWand and @knalli here and hear what they think about this feature. |
|
@knalli Agreed. Regarding point 3, we still need a solution for #169 which kinda goes hand in hand with existing watcher problems. Extending the existing watcher requires the explicit properties on the scope, which is a bit tricky if we don't even know, which I think we have to rebuild the whole directive anyway to really make use of the advantages we actually should have when using a directive rather than a filter. Currently the directive sets up a watch which makes it not better as a filter unless we don't make any unwatch() there. |
I'd like to say, that it looks interesting. |
Context "point 3" Should work? function link (scope, element, attrs) {
var values = {};
for (var key in attrs) {
if (key.substr(0, 14) === 'translateValue') {
values[key.substr(15)] = attrs[key];
}
}
} |
And, as for the theme of this discussion, it would be also nice to provide a way to pass a js object literals to the directives/filters as a source of actual variables values. Saying "object literals" I also mean a possibility to use nested filter/directive calls with some params. Something like this: {{ val | filter : "{blah : "balh-blah-blah", blahblah : "Blah! {{ val2 | filter : "{onether_blah : "Blah..."}" }}"}" }} |
@DWand : being able to filter message variable would be really nice too. Like: <ANY translate="HELLO" translate-value-name="{{currentUser | fullName}}"></ANY> where "fullName" is a filter that formats someone's name (e.g. preprend a particle like "Mr" or "Ms" depending on the gender of the user). I'm not sure I understand your use-case that combines nested filters though... :-/ |
@totof3110 , I had tried to write a pluralization support based on the filter. So the use case is to be able to use filters in the translations for specific plural form. But it is a theme of the other discussion, haha ) |
…attr you're now able to add translate values via ```js <ANY translate="TRANSLATION_ID" translate-value-foo="bar" translate-value-another="yay" ></ANY> ``` you're able to pass in a string or an interpolation so things like ```js <ANY translate="TRANSLATION_ID" translate-value-foo="{{evaluate}}" ></ANY> ``` also work. And yea this feature is progressive enhancement which means you can use it along with `translate-values` as well without breaking your code. :) Fixes #188
Hey @totof3110 you feature request just landed in |
Awesome! Thanks @PascalPrecht! I'll give it a shot ASAP. :) |
The way to do variable replacement as described in http://pascalprecht.github.io/angular-translate/docs/en/#/guide/06_variable-replacement is a little cumbersome when the variables are dynamic: having to create a translationData object in the controller and pass it to the directive is a little too "intrusive", especially when all the objects needed are usually already in the $scope.
It would be really nice to be able to write something like:
where:
I guess the directive would look at all required parameters and load them from the scope from a "namespaced" attribute (translate- + param name).
Would that even be possible?
Thanks!
The text was updated successfully, but these errors were encountered: