forked from binogure-studio/city-game-studio-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtocsv.js
66 lines (51 loc) · 1.66 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
const yaml = require('js-yaml')
const fs = require('fs')
const path = require('path')
const language_directory = path.resolve('languages')
const encoding = {
encoding: 'utf-8'
}
const files = fs.readdirSync(language_directory)
const result = files.reduce((acc, filename) => {
console.error(`Reading ${filename}`)
let filepath = path.resolve(language_directory, filename)
let language = path.basename(filename, '.yml')
let translation = yaml.load(fs.readFileSync(filepath, encoding))
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}`)
}
})
return `${acc}${key},"${value.join('","')}"
`
}, ''))