-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation-tool.js
72 lines (64 loc) · 2.63 KB
/
translation-tool.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
const fs = require('fs')
const path = require('path')
const walkSync = require('./lib/walksync.js')
const commandLineArgs = require('./lib/commandLineArgs.js')
const commandLineUsage = require('./lib/commandLineUsage.js')
const config = require('config')
if (commandLineArgs.help) {
console.log(commandLineUsage)
process.exit()
}
if (!commandLineArgs.source || !commandLineArgs.input || !commandLineArgs.output) {
console.error('Source, input and output parameters are required. Use node translation-tool.js --help to see usage.')
process.exit()
}
const absoluteInputPath = path.resolve(__dirname, commandLineArgs.input)
const absoluteOutputPath = path.resolve(__dirname, commandLineArgs.output)
const absoluteSourcePath = path.resolve(__dirname, commandLineArgs.source)
const translations = JSON.parse(fs.readFileSync(absoluteInputPath, 'utf8'))
Object.keys(translations).map(language => {
const item = translations[language]
item.languageKey = language
return item
}).forEach(languageObject => {
console.log('language ' + languageObject.languageKey)
Object.keys(languageObject).forEach(translationKey => {
if (translationKey === "languageKey")
return;
var found = false
for (const file of walkSync(absoluteSourcePath)) {
if (path.extname(file) !== '.js') {
if (fs.readFileSync(file).indexOf(translationKey) >= 0) {
found = true
break
}
}
}
if (!found) {
console.log('NOT FOUND', translationKey)
if (config.removeUnusedValues) {
translations[languageObject.languageKey][translationKey] = null
} else {
translations[languageObject.languageKey][translationKey] = config.replaceUnusedValuesWith ? config.replaceUnusedValuesWith : '#NOTFOUND#'
}
}
})
if (config.addMissingKeysFromDefaultLanguage) {
var languageKey = config.defaultLanguage ? config.defaultLanguage : 'en'
if (languageKey !== languageObject.languageKey) {
Object.keys(translations[languageKey]).forEach(translationKey => {
if (!(translationKey in translations[languageObject.languageKey])) {
console.log('NOT TRANSLATED', translationKey)
translations[languageObject.languageKey][translationKey] = config.replaceNotTranslatedValuesWith ? config.replaceNotTranslatedValuesWith : '#NOTTRANSLATED#'
}
})
}
}
translations[languageObject.languageKey]['languageKey'] = null
})
function removeNulls(key, value) {
if (value !== null) return value
}
fs.writeFile(absoluteOutputPath, JSON.stringify(translations, removeNulls, config.spacing ? config.spacing : 4), 'utf8', function (err) {
if (err) return console.log(err)
})