-
Notifications
You must be signed in to change notification settings - Fork 438
/
translateGlossary.js
44 lines (40 loc) · 1.52 KB
/
translateGlossary.js
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
const data = require('../data/rules/translateGlossary');
const {errMsgTranslateGlossary} = require('../utils/errMsg');
const {isKoreanIncluded} = require('../utils/is');
const {stripDoubleQuotes, stripParentheses} = require('../utils/strip');
/**
* Rule for the Translate Glossary
*
* @param {RuleContext} context
* @returns
*/
module.exports = function ({Syntax, report, getSource, locator, RuleError}) {
return {
[Syntax.Str](node) {
const text = getSource(node);
const textStripped = stripParentheses(stripDoubleQuotes(text));
if (!isKoreanIncluded(textStripped)) return; // Textlint only when korean is included in `textStripped`.
Object.values(data).forEach((type1) => {
Object.values(type1).forEach((type2) => {
type2.forEach(({sources, target}) => {
sources.forEach((source) => {
const matchIndex = text.match(new RegExp(source, 'i')); // Do not use 'g' flag with textlint's CLI 'pretty-error' option. It prevents textlint from finding the exact locations.
const match = textStripped.match(new RegExp(source, 'i'));
if (match) {
report(
node,
new RuleError(errMsgTranslateGlossary(match[0], target), {
padding: locator.range([
matchIndex.index,
matchIndex.index + text.length,
]),
})
);
}
});
});
});
});
},
};
};