-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathi18n.node.js
More file actions
63 lines (51 loc) · 2 KB
/
i18n.node.js
File metadata and controls
63 lines (51 loc) · 2 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require('fs');
const path = require('path');
const targetDir = 'resources'; // Define the starting directory
const textDomain = 'fluent-crm';
// Function to read directory contents recursively
function readDirRecursively(dir, allFiles = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filepath = path.join(dir, file);
if (fs.statSync(filepath).isDirectory()) {
readDirRecursively(filepath, allFiles);
} else if (path.extname(file) === '.vue' || path.extname(file) === '.js') { // Check for .vue and .js files
allFiles.push(filepath);
}
});
return allFiles;
}
// Function to extract strings from $t() in file content
function extractStrings(files) {
const results = {};
// Updated regex to capture strings with mixed quotes
const regex = /\$t\(['"]([^'"]*?(?:\\['"][^'"]*?)*?)['"]\)/g;
files.forEach(file => {
const content = fs.readFileSync(file, 'utf8');
let match;
while ((match = regex.exec(content)) !== null) {
results[match[1]] = true; // Use the match as a key to avoid duplicates
}
});
return Object.keys(results); // Return unique strings only
}
// Write results to a text file in PHP array format
function writeResults(strings) {
const sortedStrings = strings.sort(); // Sort strings in ascending order
const formattedStrings = sortedStrings.map(str => `'${str}' => __('${str}', '${textDomain}')`).join(",\n");
const finalData = "<?php [\n" + formattedStrings + "\n];";
fs.writeFile('translationStrings.php', finalData, err => {
if (err) {
console.error('Error writing to file:', err);
} else {
console.log('Saved translation strings to translationStrings.php');
}
});
}
// Main process function
function processVueFiles() {
const vueFiles = readDirRecursively(targetDir);
const uniqueStrings = extractStrings(vueFiles);
writeResults(uniqueStrings);
}
processVueFiles();