This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translateDirective): teaches directive custom translate-value-* …
…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
- Loading branch information
Showing
2 changed files
with
71 additions
and
4 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 |
---|---|---|
|
@@ -84,11 +84,18 @@ angular.module('pascalprecht.translate') | |
scope: true, | ||
compile: function (tElement, tAttr) { | ||
|
||
var translateValuesExist = (tAttr.translateValues && !angular.equals(tAttr.translateValues, '')); | ||
var translateInterpolation = (tAttr.translateInterpolation) ? tAttr.translateInterpolation : undefined; | ||
var translateValuesExist = (tAttr.translateValues) ? | ||
tAttr.translateValues : undefined | ||
|
||
var translateInterpolation = (tAttr.translateInterpolation) ? | ||
tAttr.translateInterpolation : undefined; | ||
|
||
var translateValueExist = tElement[0].outerHTML.match(/translate-value-+/i); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
0x-r4bbit
Author
Member
|
||
|
||
return function linkFn(scope, iElement, iAttr) { | ||
|
||
scope.interpolateParams = {}; | ||
|
||
// Ensures any change of the attribute "translate" containing the id will | ||
// be re-stored to the scope's "translationId". | ||
// If the attribute has no content, the element's text value (white spaces trimmed off) will be used. | ||
|
@@ -104,14 +111,27 @@ angular.module('pascalprecht.translate') | |
iAttr.$observe('translateValues', function (interpolateParams) { | ||
if (interpolateParams) { | ||
scope.$parent.$watch(function () { | ||
scope.interpolateParams = $parse(interpolateParams)(scope.$parent); | ||
angular.extend(scope.interpolateParams, $parse(interpolateParams)(scope.$parent)); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
if (translateValueExist) { | ||
for (attr in iAttr) { | ||
if (iAttr.hasOwnProperty(attr) && attr.substr(0, 14) === 'translateValue' && attr !== 'translateValues') { | ||
(function () { | ||
var attrName = attr; | ||
iAttr.$observe(attr, function (value) { | ||
scope.interpolateParams[angular.lowercase(attrName.substr(14))] = value; | ||
}); | ||
}()); | ||
} | ||
} | ||
} | ||
|
||
var updateTranslationFn = (function () { | ||
if (!translateValuesExist) { | ||
if (!translateValuesExist && !translateValueExist) { | ||
return function () { | ||
var unwatch = scope.$watch('translationId', function (value) { | ||
if (value) { | ||
|
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
2 comments
on commit 5c27467
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.
@knalli this is so neat <3
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.
yep :)
constant for that regexp being perhaps more flexible?
/update: at least extracted internally