-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsetranslation.js
More file actions
39 lines (34 loc) · 1.18 KB
/
parsetranslation.js
File metadata and controls
39 lines (34 loc) · 1.18 KB
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
const fs = require('fs');
const path = require('path');
const directoryPath = 'src';
const translationFilePath = './translation.json';
let translations = {};
function traverseDirectory(dir) {
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
if (fs.lstatSync(filePath).isDirectory()) {
traverseDirectory(filePath);
} else if (filePath.endsWith('.svelte')) {
parseSvelteFile(filePath);
}
});
}
function parseSvelteFile(filePath) {
let content = fs.readFileSync(filePath, 'utf-8');
const regex = />([^<{}]+)</g;
let match;
while ((match = regex.exec(content)) !== null) {
const staticText = match[1].trim();
if (staticText) {
const translationKey = staticText.replace(/\s+/g, '_').toLowerCase();
translations[translationKey] = staticText;
content = content.replace(staticText, `{$t("${translationKey}")}`);
}
}
fs.writeFileSync(filePath, content, 'utf-8');
}
function saveTranslations() {
fs.writeFileSync(translationFilePath, JSON.stringify(translations, null, 2), 'utf-8');
}
traverseDirectory(directoryPath);
saveTranslations();