-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild-resx.js
105 lines (81 loc) · 3.37 KB
/
build-resx.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
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
const fs = require('fs');
const path = require('path');
const {create} = require('xmlbuilder2');
const basePath = 'crowdin';
const outputFilename = 'docs.en.resx';
const filesToProcessExt = '.html.md'
const KEYS_SEPARATOR = '<!-- CROWDIN KEY SPLIT MARKER -->';
const CROWDIN_MAX_STRING_LENGTH = 65535;
const TRANSLATION_RATIO = 1.1;
const MAX_BYTES_PER_CHAR = 2;
const MAX_STRING_LENGTH = Math.floor(CROWDIN_MAX_STRING_LENGTH / (MAX_BYTES_PER_CHAR * TRANSLATION_RATIO));
const mappings = {
'adguard-mail.com': 'legal-github-docs-mail',
'adguard.com': 'legal-github-docs-adguard',
'adguard-dns.io': 'legal-github-docs-dns',
'adguard-vpn.com': 'legal-github-docs-vpn',
'adguardpartner.com': 'legal-github-docs-aff',
};
const getPartialKeySuffix = (partIndex) => {
return `.__part__.${partIndex + 1}`;
}
console.log('Starting document processing...');
if (!fs.existsSync(basePath)) {
console.log(`Creating output directory: ${basePath}`);
fs.mkdirSync(basePath);
}
for (const [sourceDir, crowdinDir] of Object.entries(mappings)) {
console.log(`\nProcessing directory: ${sourceDir} -> ${crowdinDir}`);
if (!fs.existsSync(sourceDir)) {
console.log(`Skipping ${sourceDir} - directory does not exist`);
continue;
}
const mdFiles = fs.readdirSync(sourceDir, {recursive: true})
.filter(file => file.endsWith(filesToProcessExt));
console.log(`Found ${mdFiles.length} files to process`);
let xmlBuilder = create({version: '1.0', encoding: 'utf-8'}).ele('root');
for (const filePath of mdFiles) {
const fullFilePath = path.join(sourceDir, filePath);
console.log(`Processing file: ${fullFilePath}`);
const content = fs.readFileSync(fullFilePath, 'utf8');
const baseKey = filePath
.replace(filesToProcessExt, '')
.replaceAll('/', '.');
if (content.length > MAX_STRING_LENGTH) {
const parts = content.split(KEYS_SEPARATOR);
if (parts.length === 1) {
console.error(`Error: File ${fullFilePath} exceeds maximum length and no ${KEYS_SEPARATOR} comment found`);
process.exit(1);
}
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (part.length > MAX_STRING_LENGTH) {
console.error(`Error: Part ${i + 1} of file ${fullFilePath} exceeds maximum length`);
process.exit(1);
}
const key = `${baseKey}${getPartialKeySuffix(i)}`;
console.log(`Generated key: ${key}`);
xmlBuilder = xmlBuilder.ele('data').att('name', key)
.ele('value').dat(part).up()
.up();
}
} else {
console.log(`Generated key: ${baseKey}`);
xmlBuilder = xmlBuilder.ele('data').att('name', baseKey)
.ele('value').dat(content).up()
.up();
}
}
const resxContent = xmlBuilder.end({
prettyPrint: true,
indent: ' ',
});
const outputDir = path.join(basePath, crowdinDir);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const outputPath = path.join(outputDir, outputFilename);
console.log(`Writing output to: ${outputPath}`);
fs.writeFileSync(outputPath, resxContent);
}
console.log('\nProcessing complete!');