@kununu/phraseapp-cli 4.0.0-beta.3 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README_CHECK_TRANSLATIONS.md +12 -0
- package/check-translations.js +140 -92
- package/package.json +5 -2
|
@@ -50,6 +50,18 @@ You have to add to the ```.phraseapp.json``` file a new var dynamicKeys
|
|
|
50
50
|
}
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
## Structure
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
/
|
|
57
|
+
├─ check-translations.js # Initial file
|
|
58
|
+
├─ dynamic_keys.json # Add all dynamic keys that we have in the app or keys that we get from Ambassador/BE.
|
|
59
|
+
├─ possible_dynamic_keys.json # Automatically created file indicating in which files we might have dynamic keys.
|
|
60
|
+
├─ missing_keys.json # Automatically created file indicating the keys that exist in the app but are not translated.
|
|
61
|
+
├─ not_used_keys.json # Automatically created file indicating the keys that are not being used in the app.
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
|
|
53
65
|
## License
|
|
54
66
|
|
|
55
67
|
Apache-2.0 © [kununu](https://kununu.com)
|
package/check-translations.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
const {
|
|
2
|
-
|
|
1
|
+
const {
|
|
2
|
+
existsSync,
|
|
3
|
+
mkdirSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
unlinkSync,
|
|
6
|
+
writeFileSync,
|
|
7
|
+
} = require('fs');
|
|
8
|
+
|
|
3
9
|
const {parse} = require('@babel/parser');
|
|
4
10
|
const traverse = require('@babel/traverse');
|
|
11
|
+
const {sync} = require('glob');
|
|
5
12
|
|
|
6
13
|
const FOLDER_REPORT_CHECK_TRANSLATIONS = `${process.cwd()}/report-check-translations`;
|
|
7
14
|
const UNUSED_KEYS_FILE = `${FOLDER_REPORT_CHECK_TRANSLATIONS}/not_used_keys.json`;
|
|
@@ -9,104 +16,145 @@ const MISSING_KEYS_FILE = `${FOLDER_REPORT_CHECK_TRANSLATIONS}/missing_keys.json
|
|
|
9
16
|
const POSSIBLE_DYNAMIC_KEYS_FILE = `${FOLDER_REPORT_CHECK_TRANSLATIONS}/possible_dynamic_keys.json`;
|
|
10
17
|
|
|
11
18
|
function extractTranslationKeys() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} else {
|
|
27
|
-
possibleDynamicKeys[file] = possibleDynamicKeys[file] || [];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
CallExpression({ node }) {
|
|
32
|
-
if (node.callee && node.callee.property && ['formatMessage'].includes(node.callee.property.name)) {
|
|
33
|
-
const firstArg = node.arguments[0];
|
|
34
|
-
if (firstArg && firstArg.type === 'ObjectExpression') {
|
|
35
|
-
const idProperty = firstArg.properties.find(prop => prop.key.name === 'id');
|
|
36
|
-
if (idProperty && idProperty.value.type === 'StringLiteral') {
|
|
37
|
-
keys.add(idProperty.value.value);
|
|
38
|
-
} else {
|
|
39
|
-
possibleDynamicKeys[file] = possibleDynamicKeys[file] || [];
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
} catch (error) {
|
|
46
|
-
console.error(`Error processing ${file}:`, error);
|
|
47
|
-
}
|
|
48
|
-
});
|
|
19
|
+
const files = sync(
|
|
20
|
+
`${process.cwd()}/${process.env.SOURCE_DIR}/**/**/*.{js,jsx,ts,tsx}`,
|
|
21
|
+
);
|
|
22
|
+
const keys = new Set();
|
|
23
|
+
const possibleDynamicKeys = {};
|
|
24
|
+
|
|
25
|
+
files.forEach(file => {
|
|
26
|
+
const content = readFileSync(file, 'utf8');
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const ast = parse(content, {
|
|
30
|
+
plugins: ['jsx', 'typescript'],
|
|
31
|
+
sourceType: 'module',
|
|
32
|
+
});
|
|
49
33
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
34
|
+
traverse.default(ast, {
|
|
35
|
+
CallExpression({node}) {
|
|
36
|
+
if (
|
|
37
|
+
node.callee &&
|
|
38
|
+
((node.callee.property &&
|
|
39
|
+
['formatMessage'].includes(node.callee.property.name)) ||
|
|
40
|
+
(node.callee.name &&
|
|
41
|
+
['formatMessage'].includes(node.callee.name)))
|
|
42
|
+
) {
|
|
43
|
+
const firstArg = node.arguments[0];
|
|
44
|
+
|
|
45
|
+
if (firstArg && firstArg.type === 'ObjectExpression') {
|
|
46
|
+
const idProperty = firstArg.properties.find(
|
|
47
|
+
prop => prop.key.name === 'id',
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
if (idProperty && idProperty.value.type === 'StringLiteral') {
|
|
51
|
+
keys.add(idProperty.value.value);
|
|
52
|
+
} else {
|
|
53
|
+
possibleDynamicKeys[file] = possibleDynamicKeys[file] || [];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
JSXOpeningElement({node}) {
|
|
59
|
+
if (node.name.name === 'FormattedMessage') {
|
|
60
|
+
const idAttr = node.attributes.find(
|
|
61
|
+
attr => attr.name.name === 'id',
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
idAttr &&
|
|
66
|
+
idAttr.value &&
|
|
67
|
+
idAttr.value.type === 'StringLiteral'
|
|
68
|
+
) {
|
|
69
|
+
keys.add(idAttr.value.value);
|
|
70
|
+
} else {
|
|
71
|
+
possibleDynamicKeys[file] = possibleDynamicKeys[file] || [];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error(`Error processing ${file}:`, error);
|
|
56
78
|
}
|
|
79
|
+
});
|
|
57
80
|
|
|
58
|
-
|
|
81
|
+
if (Object.keys(possibleDynamicKeys).length > 0) {
|
|
82
|
+
console.log(
|
|
83
|
+
`${Object.keys(possibleDynamicKeys).length} files contain possible dynamic keys were saved in ${POSSIBLE_DYNAMIC_KEYS_FILE}`,
|
|
84
|
+
);
|
|
85
|
+
writeFileSync(
|
|
86
|
+
POSSIBLE_DYNAMIC_KEYS_FILE,
|
|
87
|
+
JSON.stringify(possibleDynamicKeys, null, 2),
|
|
88
|
+
);
|
|
89
|
+
} else if (existsSync(POSSIBLE_DYNAMIC_KEYS_FILE)) {
|
|
90
|
+
unlinkSync(POSSIBLE_DYNAMIC_KEYS_FILE);
|
|
91
|
+
console.log(
|
|
92
|
+
`${POSSIBLE_DYNAMIC_KEYS_FILE} was deleted as there are no possible dynamic keys.`,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return keys;
|
|
59
97
|
}
|
|
60
98
|
|
|
61
99
|
function compareKeys() {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
100
|
+
// create report folder if it doesn't exist to save all the reports
|
|
101
|
+
if (!existsSync(FOLDER_REPORT_CHECK_TRANSLATIONS)) {
|
|
102
|
+
mkdirSync(FOLDER_REPORT_CHECK_TRANSLATIONS, {recursive: true});
|
|
103
|
+
}
|
|
104
|
+
const configPath = `${process.cwd()}/.phraseapp.json`;
|
|
105
|
+
const appKeys = extractTranslationKeys();
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const phrase = JSON.parse(readFileSync(configPath));
|
|
109
|
+
const translations = JSON.parse(
|
|
110
|
+
readFileSync(`${phrase.path}/de_AT.json`, 'utf8'),
|
|
111
|
+
);
|
|
112
|
+
const dynamicKeys = phrase.dynamicKeys || [];
|
|
113
|
+
|
|
114
|
+
const allValidKeys = new Set(Object.keys(translations));
|
|
115
|
+
|
|
116
|
+
let missingKeys = [...appKeys].filter(key => !allValidKeys.has(key));
|
|
117
|
+
|
|
118
|
+
missingKeys = missingKeys.filter(key => key != null && key !== undefined);
|
|
119
|
+
|
|
120
|
+
// Check if there are keys in dynamicKeys that are missing from allValidKeys.
|
|
121
|
+
dynamicKeys.forEach(key => {
|
|
122
|
+
if (!allValidKeys.has(key)) {
|
|
123
|
+
missingKeys.push(key);
|
|
124
|
+
} else {
|
|
125
|
+
allValidKeys.delete(key);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const referenceKeys = new Set([...appKeys, ...dynamicKeys]);
|
|
130
|
+
const unusedKeys = [...allValidKeys].filter(key => !referenceKeys.has(key));
|
|
131
|
+
|
|
132
|
+
if (missingKeys.length > 0) {
|
|
133
|
+
writeFileSync(MISSING_KEYS_FILE, JSON.stringify(missingKeys, null, 2));
|
|
134
|
+
console.log(
|
|
135
|
+
`${missingKeys.length} missing keys were saved in ${MISSING_KEYS_FILE}`,
|
|
136
|
+
);
|
|
137
|
+
} else if (existsSync(MISSING_KEYS_FILE)) {
|
|
138
|
+
unlinkSync(MISSING_KEYS_FILE);
|
|
139
|
+
console.log(
|
|
140
|
+
`${MISSING_KEYS_FILE} was deleted as there are no missing keys.`,
|
|
141
|
+
);
|
|
65
142
|
}
|
|
66
|
-
const configPath = `${process.cwd()}/.phraseapp.json`;
|
|
67
|
-
const appKeys = extractTranslationKeys();
|
|
68
143
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
// Check if there are keys in dynamicKeys that are missing from allValidKeys.
|
|
80
|
-
dynamicKeys.forEach(key => {
|
|
81
|
-
if (!allValidKeys.has(key)) {
|
|
82
|
-
missingKeys.push(key);
|
|
83
|
-
} else {
|
|
84
|
-
allValidKeys.delete(key);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
//
|
|
89
|
-
const referenceKeys = new Set([...appKeys, ...dynamicKeys]);
|
|
90
|
-
const unusedKeys = [...allValidKeys].filter(key => !referenceKeys.has(key));
|
|
91
|
-
|
|
92
|
-
if (missingKeys.length > 0) {
|
|
93
|
-
writeFileSync(MISSING_KEYS_FILE, JSON.stringify(missingKeys, null, 2));
|
|
94
|
-
console.log(`${missingKeys.length} missing keys were saved in ${MISSING_KEYS_FILE}`);
|
|
95
|
-
} else if (existsSync(MISSING_KEYS_FILE)) {
|
|
96
|
-
unlinkSync(MISSING_KEYS_FILE);
|
|
97
|
-
console.log(`${MISSING_KEYS_FILE} was deleted as there are no missing keys.`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (unusedKeys.length > 0) {
|
|
101
|
-
writeFileSync(UNUSED_KEYS_FILE, JSON.stringify(unusedKeys, null, 2));
|
|
102
|
-
console.log(`${unusedKeys.length} unused keys were saved in ${UNUSED_KEYS_FILE}`);
|
|
103
|
-
} else if (existsSync(UNUSED_KEYS_FILE)) {
|
|
104
|
-
unlinkSync(UNUSED_KEYS_FILE);
|
|
105
|
-
console.log(`${UNUSED_KEYS_FILE} was deleted as there are no missing keys.`);
|
|
106
|
-
}
|
|
107
|
-
} catch (error) {
|
|
108
|
-
console.error(error);
|
|
144
|
+
if (unusedKeys.length > 0) {
|
|
145
|
+
writeFileSync(UNUSED_KEYS_FILE, JSON.stringify(unusedKeys, null, 2));
|
|
146
|
+
console.log(
|
|
147
|
+
`${unusedKeys.length} unused keys were saved in ${UNUSED_KEYS_FILE}`,
|
|
148
|
+
);
|
|
149
|
+
} else if (existsSync(UNUSED_KEYS_FILE)) {
|
|
150
|
+
unlinkSync(UNUSED_KEYS_FILE);
|
|
151
|
+
console.log(
|
|
152
|
+
`${UNUSED_KEYS_FILE} was deleted as there are no missing keys.`,
|
|
153
|
+
);
|
|
109
154
|
}
|
|
155
|
+
} catch (error) {
|
|
156
|
+
console.error(error);
|
|
157
|
+
}
|
|
110
158
|
}
|
|
111
159
|
|
|
112
|
-
compareKeys();
|
|
160
|
+
compareKeys();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kununu/phraseapp-cli",
|
|
3
|
-
"version": "4.0.0
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "kununu",
|
|
@@ -9,8 +9,11 @@
|
|
|
9
9
|
"lint": "eslint . --ext jsx --ext js --ext tsx --ext ts --ignore-path .eslintignore --max-warnings 10"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
+
"@babel/parser": "^7.26.10",
|
|
13
|
+
"@babel/traverse": "^7.26.10",
|
|
12
14
|
"colors": "1.4.0",
|
|
13
|
-
"dotenv": "16.4.5"
|
|
15
|
+
"dotenv": "16.4.5",
|
|
16
|
+
"glob": "^11.0.1"
|
|
14
17
|
},
|
|
15
18
|
"devDependencies": {
|
|
16
19
|
"@kununu/eslint-config": "5.0.1"
|