-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtranslate-directive-translation-extractor.ts
159 lines (140 loc) · 5.13 KB
/
translate-directive-translation-extractor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import {
HtmlTranslationExtractionContext,
AngularElement,
} from "./html-translation-extractor";
import { AngularExpressionMatch } from "./ng-filters";
import { Attribute } from "./element-context";
import { SUPPRESS_ATTRIBUTE_NAME } from "./translate-html-parser";
const translateAttributeRegex = /^translate-attr-.*$/;
export default function translateDirectiveTranslationExtractor(
element: AngularElement,
context: HtmlTranslationExtractionContext
) {
let translateDirective: boolean;
let translationId: string;
const translateAttribute = element.attributes.find(
(attribute) => attribute.name === "translate"
);
if (element.tagName === "translate") {
translateDirective = true;
}
if (translateAttribute) {
translateDirective = true;
translationId = translateAttribute.value;
}
for (const attribute of element.attributes) {
handleAttributesWithTranslateExpressions(attribute, context);
}
if (translateDirective) {
const hasTranslateAttributes = handleTranslateAttributes(element, context);
const defaultTextAttribute = element.attributes.find(
(attr) => attr.name === "translate-default"
);
if (!translationId) {
if (element.texts.length === 1) {
translationId = element.texts[0].text;
} else if (element.texts.length > 1) {
context.emitError(
"The element does not specify a translation id but has multiple child text elements. Specify the translation id on the element to define the translation id.",
element.startPosition
);
return;
}
}
// <any translate='test'></any>
if (translationId) {
context.registerTranslation({
translationId,
defaultText: defaultTextAttribute
? defaultTextAttribute.value
: undefined,
position: element.startPosition,
});
} else if (!hasTranslateAttributes) {
// no translate-attr-* and the element has not specified a translation id, someone is using the directive incorrectly
context.emitSuppressableError(
"the element uses the translate directive but does not specify a translation id nor has any translated attributes (translate-attr-*). Specify a translation id or remove the translate-directive.",
element.startPosition
);
}
} else {
handleTexts(element, context);
}
}
translateDirectiveTranslationExtractor.mayContainTranslations = function (
content: string
): boolean {
return content.indexOf("translate") !== -1;
};
type KeyedAttributes = { [name: string]: Attribute };
function handleTranslateAttributes(
element: AngularElement,
context: HtmlTranslationExtractionContext
) {
const keyedAttributes = element.attributes.reduce((obj, attribute) => {
obj[attribute.name] = attribute;
return obj;
}, {} as KeyedAttributes);
// translate-attr-ATTR
const translateAttributes = element.attributes.filter((attr) =>
translateAttributeRegex.test(attr.name)
);
for (const attribute of translateAttributes) {
const attributeName = attribute.name.substr("translate-attr-".length);
const defaultTextAttribute =
keyedAttributes[`translate-default-attr-${attributeName}`];
context.registerTranslation({
translationId: attribute.value,
defaultText: defaultTextAttribute
? defaultTextAttribute.value
: undefined,
position: attribute.startPosition,
});
}
return translateAttributes.length > 0;
}
// <any attr='{{ x | translate }}'></any>
function handleAttributesWithTranslateExpressions(
attribute: Attribute,
context: HtmlTranslationExtractionContext
) {
for (const expression of attribute.expressions) {
handleAngularExpression(expression, context, attribute.startPosition);
}
}
function handleAngularExpression(
expression: AngularExpressionMatch,
context: HtmlTranslationExtractionContext,
position: number
) {
let translationId = expression.value;
// e.g { translate | var} instead of a string constant
if (!(/^".*"$/.test(translationId) || /^'.*'$/.test(translationId))) {
context.emitSuppressableError(
`A dynamic filter expression is used in the text or an attribute of the element '${context.asHtml()}'. Add the '${SUPPRESS_ATTRIBUTE_NAME}' attribute to suppress the error (ensure that you have registered the translation manually, consider using i18n.registerTranslation).`,
position
);
} else if (expression.previousFilters) {
context.emitSuppressableError(
`Another filter is used before the translate filter in the element ${context.asHtml()}. Add the '${SUPPRESS_ATTRIBUTE_NAME}' to suppress the error (ensure that you have registered the translation manually, consider using i18n.registerTranslation).`,
position
);
} else {
// trim the quotes
translationId = translationId.substring(1, translationId.length - 1);
context.registerTranslation({
translationId,
position,
});
}
}
function handleTexts(
element: AngularElement,
context: HtmlTranslationExtractionContext
) {
for (const text of element.texts) {
for (const expression of text.expressions) {
handleAngularExpression(expression, context, text.startPosition);
}
}
}