-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
tocsv.js
71 lines (56 loc) · 1.84 KB
/
tocsv.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
const yaml = require('js-yaml')
const fs = require('fs')
const path = require('path')
const language_directory = [path.resolve('languages'), path.resolve('achievements')]
const encoding = {
encoding: 'utf-8'
}
const files = language_directory.reduce((acc, directory) => {
return acc.concat(fs.readdirSync(directory).map((filename) => {
return path.resolve(directory, filename)
}))
}, [])
const result = files.reduce((acc, filepath) => {
let language = path.basename(filepath, '.yml')
let translation = yaml.load(fs.readFileSync(filepath, encoding))
console.error(`Reading ${filepath} (${language})`)
if (!acc.id.find((item) => item == language)) {
acc.id.push(language)
}
Object.keys(translation[language]).forEach((key) => {
let value = translation[language][key]
if (/.*\n$/.test(value)) {
value = value.slice(0, value.length - 1)
}
acc[key] = acc[key] || []
acc[key].push(value)
})
return acc
}, {
id: []
})
let amount_of_translations = result.id.length
console.log(Object.keys(result).reduce((acc, key) => {
let value = result[key]
let amount_of_percent = null
if (amount_of_translations != value.length) {
console.error(`Error on ${key} (Expected: ${amount_of_translations}, actual: ${value.length})`)
}
value.forEach((item) => {
if (amount_of_percent == null) {
amount_of_percent = item.split('%d').length
} else if (item != null && amount_of_percent != item.split('%d').length) {
console.error(`Error with ${key} ${item}`)
}
})
amount_of_percent = null
value.forEach((item) => {
if (amount_of_percent == null) {
amount_of_percent = item.split('%s').length
} else if (item != null && amount_of_percent != item.split('%s').length) {
console.error(`Error with ${key} ${item}`)
}
})
return `${acc}${key},"${value.join('","')}"
`
}, ''))