Skip to content

Commit 64cb642

Browse files
committed
rewrite translate-child and replace attirbutes with translate format
`translate-child-into` and `translate-child-key-into` have been removed. To translate into an attribute you now specify it before the translation key in the `translate-child` attribute as so: ```` <a translate-child="(aria-label,title,!translate)=SOME_TRANSLATION.KEY"></a> ```` The attributes should be comma delimited and prefixed with a `!` when you want the translation key to be set instead of the translated value.
1 parent f6007ee commit 64cb642

File tree

7 files changed

+146
-60
lines changed

7 files changed

+146
-60
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"main": [
44
"dist/angular-devtrw-translate.js"
55
],
6-
"version": "0.1.7",
6+
"version": "0.2.0",
77
"description": "Provides translate-base and translate-child directives for angular-translate",
88
"authors": [
99
"Steven Nance <steven@devtrw.com>"

demo/index.html

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ <h3>Examples</h3>
6161
class="page"
6262
ng-init="lastLoginUsername = 'someUserName'">
6363
<div class="example">
64-
<h4>translate-values</h4>
64+
<h4>translate child using `translate-values`</h4>
6565
<hr>
6666
<div translate-base="LAST_LOGIN">
6767
<p translate-child="DETAILS" translate-values="{username: lastLoginUsername}"></p>
6868
</div>
6969
</div>
7070

7171
<div class="example">
72-
<h4>translate-child-into</h4>
72+
<h4>translate child into attribute</h4>
7373
<hr>
7474
<div translate-base="PASSWORD_FORM">
7575
<h4 translate-child="TITLE"></h4>
@@ -78,24 +78,14 @@ <h4 translate-child="TITLE"></h4>
7878
<div class="form-group" translate-base="PASSWORD">
7979
<label translate-child="LABEL" for="password-input"></label>
8080
<input type="text" id="password-input"
81-
translate-child="PLACEHOLDER"
82-
translate-child-into="placeholder">
81+
translate-child="(placeholder)=PLACEHOLDER">
8382
</div>
8483
</form>
8584
</div>
8685
</div>
8786

88-
<div class="example" translate-base="GOOGLE_LINK">
89-
<h4>translate-child-into <em>with multiple target attributes</em></h4>
90-
<hr>
91-
<a href="https://google.com"
92-
translate-child="TEXT"
93-
translate-child-into="title,arial-label"
94-
translate-child-key-into="translate"></a>
95-
</div>
96-
9787
<div class="example">
98-
<h4>translate-values <em>and</em> translate-child-into</h4>
88+
<h4>translate child into attribute with translate-values</h4>
9989
<hr>
10090
<div translate-base="USERNAME_FORM">
10191
<h4 translate-child="TITLE"></h4>
@@ -105,16 +95,21 @@ <h4 translate-child="TITLE"></h4>
10595
<label translate-child="LABEL" for="username-input"></label>
10696
<input type="text"
10797
id="username-input"
108-
translate-child="PLACEHOLDER"
109-
translate-values="{lastLoginUser: lastLoginUsername}"
110-
translate-child-into="placeholder">
98+
translate-child="(placeholder)=PLACEHOLDER"
99+
translate-values="{lastLoginUser: lastLoginUsername}">
111100
</div>
112101
</form>
113102
</div>
114103
</div>
115104

105+
<div class="example" translate-base="GOOGLE_LINK">
106+
<h4>translate child key into attribute</h4>
107+
<hr>
108+
<a href="https://google.com" translate-child="(title,aria-label,!translate)=TEXT"></a>
109+
</div>
110+
116111
<div class="example">
117-
<h4>translate-exclude-parent-base</h4>
112+
<h4>exclude parent translate base</h4>
118113
<hr>
119114
<div translate-base="PASSWORD_FORM">
120115
<div translate-base="dtrw.translate.forms.ACTIONS"

dist/angular-devtrw-translate.js

Lines changed: 61 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-devtrw-translate.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-devtrw-translate.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-devtrw-translate",
3-
"version": "0.1.7",
3+
"version": "0.2.0",
44
"author": "Steven Nance <steven@devtrw.com>",
55
"homepage": "https://github.com/devtrw/angular-devtrw-translate",
66
"repository": {

src/translate-child-directive.js

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,56 @@
11
function dtrwTranslateChildDirective($compile, $translate) {
22

3+
function setValueForAttrs(element, targetAttrs, value) {
4+
targetAttrs.forEach((targetAttrs) => {
5+
element.attr(targetAttrs, value);
6+
});
7+
}
8+
9+
function parseTranslateChildAttributes(configString) {
10+
let keyAttributes = [];
11+
let valueAttributes = [];
12+
let attributes = configString
13+
.replace(/[\[\]\(\)\s]+/g, '') // remove parenthesis, brackets, an spaces
14+
.split(',');
15+
16+
attributes.forEach((attributeName) => {
17+
// attribute names prefixed with ! signifies that the translation key should be
18+
// set instead of the translated value
19+
if ('!' === attributeName.charAt(0)) {
20+
keyAttributes.push(attributeName.substr(1));
21+
} else {
22+
valueAttributes.push(attributeName);
23+
}
24+
});
25+
26+
return [keyAttributes, valueAttributes];
27+
}
28+
29+
function parseTranslateChildAttr(attribute) {
30+
console.log('parsing: ', attribute);
31+
let parts = attribute.split('=');
32+
33+
// by default we just set the translation key on the `translate` attributes
34+
if (1 === parts.length) {
35+
return {
36+
translateKey: parts[0],
37+
keyAttributes: ['translate'],
38+
valueAttributes: []
39+
};
40+
}
41+
42+
let parsedAttributes = parseTranslateChildAttributes(parts[0]);
43+
44+
let parsed = {
45+
translateKey: parts[1],
46+
keyAttributes: parsedAttributes[0],
47+
valueAttributes: parsedAttributes[1]
48+
};
49+
console.log('parsed: ', parsed);
50+
51+
return parsed;
52+
}
53+
354
return {
455
restrict: 'A',
556
replace: false,
@@ -9,30 +60,27 @@ function dtrwTranslateChildDirective($compile, $translate) {
960
// Make sure this directive is run first
1061
priority: 1000,
1162
link: function link(scope, element, attrs, translateBaseCtrl) { // jshint ignore:line
12-
let {translateChildInto, translateChildKeyInto, translateValues} = attrs;
13-
let translateKey = translateBaseCtrl ?
14-
translateBaseCtrl.getTranslationKey(attrs.translateChild) :
15-
`COULD_NOT_FIND_TRANSLATE_BASE_CONTROLLER_FOR_CHILD-${attrs.translateChild}`;
16-
17-
function setValueForAttrs(targetAttrs, value) {
18-
targetAttrs
19-
.split(',')
20-
.forEach((targetAttrs) => {
21-
element.attr(targetAttrs, value);
22-
});
23-
}
63+
let {
64+
translateKey,
65+
keyAttributes,
66+
valueAttributes
67+
} = parseTranslateChildAttr(attrs.translateChild);
2468

25-
if (translateChildInto) {
26-
translateValues = (translateValues) ? scope.$eval(translateValues) : {};
27-
setValueForAttrs(translateChildInto, $translate.instant(translateKey, translateValues));
28-
}
69+
translateKey = translateBaseCtrl ?
70+
translateBaseCtrl.getTranslationKey(translateKey) :
71+
`COULD_NOT_FIND_TRANSLATE_BASE_CONTROLLER_FOR_CHILD-${translateKey}`;
2972

30-
if (translateChildKeyInto) {
31-
setValueForAttrs(translateChildKeyInto, translateKey);
73+
if (0 < keyAttributes.length) {
74+
setValueForAttrs(element, keyAttributes, translateKey);
3275
}
3376

34-
if (!translateChildInto && !translateChildKeyInto) {
35-
element.attr('translate', translateKey);
77+
if (0 < valueAttributes.length) {
78+
let translateValues = (attrs.translateValues) ? scope.$eval(attrs.translateValues) : {};
79+
setValueForAttrs(
80+
element,
81+
valueAttributes,
82+
$translate.instant(translateKey, translateValues)
83+
);
3684
}
3785

3886
element.removeAttr('translate-child'); //remove the attribute to avoid indefinite loop

0 commit comments

Comments
 (0)