forked from WatWowMap/pogo-translations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
188 lines (171 loc) · 5.8 KB
/
update.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const fs = require('fs')
const path = require('path')
const { generate } = require('pogo-data-generator')
const primary = require('./templates/primary.json')
const enManualFallback = require('./static/manual/en.json')
const update = async () => {
const collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base',
})
console.log('Fetching latest locales')
const { translations } = await generate({
template: primary,
// translationApkUrl:
// 'https://raw.githubusercontent.com/turtiesocks/pogo_assets/master/Texts/Latest%20APK/JSON/i18n_english.json',
})
const pogoLocalesFolder = path.resolve(__dirname, './static/manual')
const inMemory = {}
fs.readdir(pogoLocalesFolder, (err, files) => {
files.forEach((file) => {
console.log(`Starting ${file} merge`)
const short = path.basename(file, '.json')
const safe = translations[short] ? short : 'en'
const manualTranslations = fs.readFileSync(
path.resolve(pogoLocalesFolder, file),
{ encoding: 'utf8', flag: 'r' }
)
const manualKeys = {
...enManualFallback,
...JSON.parse(manualTranslations.toString()),
}
const sortedObj = {}
const sortedKeys = [
...Object.keys(translations[safe]),
...Object.keys(manualKeys),
].sort(collator.compare)
sortedKeys.forEach((key) => {
sortedObj[key] = manualKeys[key] || translations[safe][key]
})
inMemory[short] = sortedObj
console.log(`Writing ${file} locale`)
fs.writeFile(
path.resolve(path.resolve(__dirname, './static/locales'), file),
JSON.stringify(sortedObj, null, 2),
'utf8',
() => {}
)
})
})
const available = await fs.promises.readdir(pogoLocalesFolder)
console.log('Generating latest index')
fs.writeFile(
'index.json',
JSON.stringify(available, null, 2),
'utf8',
() => {}
)
return { inMemory, available }
}
const enRef = async (inMemory, available) => {
console.log('Generating locales based on English as the reference now')
const manualCategories = {
characterCategories: {},
costumes: {},
descriptions: {},
evolutionQuests: {},
forms: {},
grunts: {},
items: {},
lures: {},
misc: {},
moves: {},
pokemon: {},
pokemonCategories: {},
questConditions: {},
questRewardTypes: {},
questTypes: {},
questTitles: {},
types: {},
weather: {},
}
if (inMemory.en) {
Object.entries(inMemory.en).forEach(([key, value]) => {
if (key.startsWith('poke_type')) {
manualCategories.types[key] = value
} else if (key.startsWith('pokemon_category')) {
manualCategories.pokemonCategories[key] = value
} else if (key.startsWith('poke')) {
manualCategories.pokemon[key] = value
} else if (key.startsWith('form')) {
manualCategories.forms[key] = value
} else if (key.startsWith('costume')) {
manualCategories.costumes[key] = value
} else if (key.startsWith('quest_') || key.startsWith('challenge_')) {
const newValue =
value && value.includes('%{')
? value.replace(/%\{/g, '{{').replace(/\}/g, '}}')
: value
if (key.startsWith('quest_condition_')) {
manualCategories.questConditions[key] = newValue
} else if (key.startsWith('quest_reward_')) {
manualCategories.questRewardTypes[key] = newValue
} else if (key.startsWith('quest_title_')) {
manualCategories.questTitles[key] = newValue
} else if (key.endsWith('plural') || key.endsWith('singular')) {
manualCategories.evolutionQuests[key] = newValue
} else {
manualCategories.questTypes[key] = newValue
}
} else if (key.startsWith('grunt')) {
manualCategories.grunts[key] = value
} else if (key.startsWith('character')) {
manualCategories.characterCategories[key] = value
} else if (key.startsWith('weather')) {
manualCategories.weather[key] = value
} else if (key.startsWith('desc')) {
manualCategories.descriptions[key] = value
} else if (key.startsWith('item')) {
manualCategories.items[key] = value
} else if (key.startsWith('lure')) {
manualCategories.lures[key] = value
} else if (key.startsWith('move')) {
manualCategories.moves[key] = value
} else {
manualCategories.misc[key] = value
}
})
available.forEach((locale) => {
const languageRef = {}
const mergedRef = {}
const short = locale.replace('.json', '')
Object.keys(manualCategories).forEach((category) => {
languageRef[category] = {}
Object.entries(manualCategories[category]).forEach(([key, value]) => {
if (key !== 'form_133') {
let localeValue = inMemory[short][key]
if (localeValue && localeValue.includes('%{')) {
localeValue = localeValue
.replace(/%\{/g, '{{')
.replace(/\}/g, '}}')
}
languageRef[category][value] = localeValue
mergedRef[value] = localeValue
}
})
fs.writeFile(
path.resolve(
path.resolve(__dirname, './static/englishRef'),
`${category}_${locale}`
),
JSON.stringify(languageRef[category], null, 2),
'utf8',
() => {}
)
})
fs.writeFile(
path.resolve(path.resolve(__dirname, './static/enRefMerged'), locale),
JSON.stringify(mergedRef, null, 2),
'utf8',
() => {}
)
})
}
}
module.exports.update = update
module.exports.enRef = enRef
if (require.main === module) {
update()
.then(async ({ inMemory, available }) => enRef(inMemory, available))
.then(() => console.log('Translations generated'))
}