-
Notifications
You must be signed in to change notification settings - Fork 97
/
generate-translations.js
156 lines (153 loc) · 4.98 KB
/
generate-translations.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { deleteAsync } from 'del'
import glob from './node_modules/glob/glob.js'
import jsonfile from './node_modules/jsonfile/index.js'
import write from './node_modules/write/index.js'
const unordered = {}
let missing = []
const abandoned = {}
const source = jsonfile.readFileSync('./lang/en.json')
const keys = Object.keys(source)
const maxMissingKeys = 50
glob('./lang/*.json', {}, async function (er, files) {
await Promise.all(
files.map(async filename => {
const lang = filename.replace(/^(.+\/)([a-zA-Z0-9-]+)(\.json)$/, '$2')
if (lang !== 'en') {
const json = jsonfile.readFileSync(filename)
const missingKeys = keys.filter(x => !Object.keys(json).includes(x))
if (missingKeys.length <= maxMissingKeys) {
unordered[lang] = missingKeys
missing = missing.concat(unordered[lang])
} else {
abandoned[lang] = missingKeys
}
}
})
)
const complete = Object.keys(unordered).filter(
lang => unordered[lang].length === 0
)
const langs = Object.keys(unordered)
.filter(lang => unordered[lang].length > 0)
.sort()
.reduce((obj, key) => {
obj[key] = unordered[key]
return obj
}, {})
let output = ''
let anchors = ''
output = output + '# Translating.\n\n'
output =
output +
'Thank you for being interested in making Call of Cthulhu 7th Edition for Foundry VTT better!'
if (missing.length > 0) {
output =
output +
' Below is a list of translations keys on existing files that still need translated, based on `en.json`.'
}
output =
output +
' Feel free to create a new `*.json` file for a language that is not shown here!\n\n'
if (complete.length > 1) {
output =
output +
'The following translations are currently up to date **' +
complete.join('**, **') +
'**\n\n'
} else if (complete.length > 0) {
output =
output +
'The **' +
complete.join() +
'** translation is currently up to date\n\n'
}
if (Object.keys(abandoned).length > 0) {
output = output + 'The following translations have more than ' + maxMissingKeys + ' untranslated strings [are you able to help?](./ABANDONED.md)\n\n'
Object.entries(abandoned).forEach(([key, values]) => {
output = output + '[' + key + '.json (' + Object.entries(abandoned[key]).length + ' untranslated strings)](./ABANDONED.md#' + (key + '.json').toLowerCase().replace(/[^a-zA-Z0-9]+/g, '') + ')\n\n'
})
output = output + '\n\n'
}
if (missing.length > 0) {
output = output + '|Key|'
Object.entries(langs).forEach(([key, value]) => {
output = output + key + '|'
})
output = output + '\n'
output = output + '|:---|'
Object.entries(langs).forEach(([key, value]) => {
output = output + ':---:|'
})
output = output + '\n'
output = output + '|**Remaining**:|'
Object.entries(langs).forEach(([key, value]) => {
output = output + '**' + value.length + '**|'
})
output = output + '\n'
missing
.filter((item, index) => {
return missing.indexOf(item) === index
})
.sort()
.forEach(key => {
output =
output +
'|[' +
key +
'](#' +
key.toLowerCase().replace(/[^a-zA-Z0-9]+/g, '') +
')|'
Object.entries(langs).forEach(([lang, value]) => {
output = output + (value.includes(key) ? '❌' : '✅') + '|'
})
output = output + '\n'
anchors =
anchors +
'##### ' +
key +
'\n``` "' +
key +
'": "' +
source[key].replace(/\n/g, '\\n') +
'",```\n'
})
output = output + anchors
}
write('./.github/TRANSLATIONS.md', output)
if (Object.keys(abandoned).length > 0) {
output = ''
output = output + '# Abandoned Translations.\n\n'
output =
output +
'Thank you for being interested in making Call of Cthulhu 7th Edition for Foundry VTT better!'
output =
output +
' Below is a list of translations keys on existing files that still need translated, based on `en.json`.\n\n'
Object.entries(abandoned).forEach(([key, values]) => {
output =
output +
'[' +
key +
'.json (' + Object.entries(abandoned[key]).length + ' untranslated strings)](#' +
(key + '.json').toLowerCase().replace(/[^a-zA-Z0-9]+/g, '') +
')\n\n'
})
output = output + '\n'
Object.entries(abandoned).forEach(([key, values]) => {
output = output + '## ' + key + '.json\n' + Object.entries(abandoned[key]).length + ' untranslated strings\n```\n'
values.forEach(sourceKey => {
output =
output +
'"' +
sourceKey +
'": "' +
source[sourceKey].replace(/\n/g, '\\n') +
'",\n'
})
output = output.substring(0, output.length - 2) + '\n```\n'
})
write('./.github/ABANDONED.md', output)
} else {
deleteAsync('./.github/ABANDONED.md')
}
})