forked from duckduckgo/duckduckgo-locales
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapt-translations-from-smartling.js
65 lines (59 loc) · 1.99 KB
/
adapt-translations-from-smartling.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
#!/opt/nodenv/shims/node
/**
* This file is used by DDG employees to integrate translations coming from Smartling.com into this repo.
* Community translations do not need to use this files.
*/
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const copyFiles = (srcDir, destDir) => {
const files = fs.readdirSync(srcDir)
return files.map(file => {
const destFile = path.join(destDir, file.replace('.pot', '.po'));
const srcFile = path.join(srcDir, file);
try {
const dataBuffer = fs.readFileSync(srcFile);
const fileContent = dataBuffer.toString();
fs.writeFileSync(destFile, fileContent);
console.log(`Copied file from ${srcDir} to ${destFile}`);
} catch (e) {
console.log(`Got an error trying to copy the file ${srcDir}: ${e.message}`)
}
})
}
const start = () => {
try {
rl.question('Where are the translations? ', function(baseFolder) {
// Normalise path input
baseFolder = baseFolder
// Fix apexes added when dropping paths in VSCode
.replace(/^'/, '').replace(/'$/, '')
// Fix whitespace added at the end of the string by macOS Terminal and iTerm
.trim();
console.log(`The base folder is: ${baseFolder}`);
// Filter for folders named like `it_IT`
const folders = fs.readdirSync(baseFolder).filter(name => /^[a-z]{2}(-[A-Z]{2})?$/.test(name));
folders.forEach(name => {
const localesFolderBase = path.join(__dirname, 'locales/');
copyFiles(
path.join(baseFolder, name),
path.join(localesFolderBase, name.replace('-', '_').replace('nb', 'nb_NO'), 'LC_MESSAGES')
);
});
rl.close();
});
rl.on('close', function() {
console.log('\nAll done.');
process.exit(0);
});
} catch (e) {
console.log('Something went wrong!');
console.error(e);
process.exit(1);
}
};
start();