2021-09-14 06:43:57 -04:00
|
|
|
const path = require('path')
|
2020-08-18 06:09:28 -04:00
|
|
|
const { promises: fs } = require('fs')
|
|
|
|
const oneSky = require('@brainly/onesky-utils')
|
2022-02-02 05:24:08 -05:00
|
|
|
const { sanitize } = require('./sanitize')
|
2020-09-28 06:51:39 -04:00
|
|
|
const { withAuth } = require('./config')
|
2020-08-18 06:09:28 -04:00
|
|
|
|
|
|
|
async function run() {
|
|
|
|
try {
|
|
|
|
// The recommended OneSky set-up appears to require an API request to
|
|
|
|
// generate files on their side, which you could then request and use. We
|
|
|
|
// only have 1 such file that appears to be misnamed (en-US, despite our
|
|
|
|
// translations being marked as GB) and very out-of-date.
|
|
|
|
// However by requesting the "multilingual file" for this file, we get all
|
|
|
|
// of the translations
|
2020-09-28 06:51:39 -04:00
|
|
|
const content = await oneSky.getMultilingualFile(
|
|
|
|
withAuth({
|
2021-04-27 03:52:58 -04:00
|
|
|
fileName: 'en-US.json',
|
2020-09-28 06:51:39 -04:00
|
|
|
})
|
|
|
|
)
|
2020-08-18 06:09:28 -04:00
|
|
|
const json = JSON.parse(content)
|
|
|
|
|
|
|
|
for (const [code, lang] of Object.entries(json)) {
|
2020-10-21 04:35:32 -04:00
|
|
|
if (code === 'en-GB') {
|
|
|
|
// OneSky does not have read-after-write consistency.
|
|
|
|
// Skip the dump of English locales, which may not include locales
|
|
|
|
// that were just uploaded.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:09:28 -04:00
|
|
|
for (let [key, value] of Object.entries(lang.translation)) {
|
|
|
|
// Handle multi-line strings as arrays by joining on newline
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value = value.join('\n')
|
|
|
|
}
|
|
|
|
lang.translation[key] = sanitize(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
await fs.writeFile(
|
2021-09-14 06:43:57 -04:00
|
|
|
path.join(__dirname, `/../../locales/${code}.json`),
|
2023-01-09 06:20:22 -05:00
|
|
|
JSON.stringify(
|
|
|
|
lang.translation,
|
|
|
|
Object.keys(lang.translation).sort(),
|
|
|
|
2
|
|
|
|
) + '\n'
|
2020-08-18 06:09:28 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|