Merge pull request #3217 from overleaf/jpa-translations-upload

[misc] i18n: translations upload

GitOrigin-RevId: f4bc24c9a645cc3eb9ab004d98ab4cd89d3cdce2
This commit is contained in:
Simon Detheridge 2020-09-28 11:51:39 +01:00 committed by Copybot
parent 315987dc5a
commit 2efd33eae6
5 changed files with 86 additions and 10 deletions

View file

@ -0,0 +1,29 @@
let userOptions
try {
userOptions = require('../../data/onesky.json')
} catch (err) {
if (!process.env.ONE_SKY_PUBLIC_KEY) {
console.error(
'Cannot detect onesky credentials.\n\tDevelopers: see the docs at',
'https://github.com/overleaf/developer-manual/blob/master/code/translations.md#testing-translations-scripts',
'\n\tOps: environment variable ONE_SKY_PUBLIC_KEY is not set'
)
process.exit(1)
}
}
function withAuth(options) {
return Object.assign(
options,
{
apiKey: process.env.ONE_SKY_PUBLIC_KEY,
secret: process.env.ONE_SKY_PRIVATE_KEY,
projectId: '25049'
},
userOptions
)
}
module.exports = {
withAuth
}

View file

@ -1,6 +1,7 @@
const { promises: fs } = require('fs')
const oneSky = require('@brainly/onesky-utils')
const sanitizeHtml = require('sanitize-html')
const { withAuth } = require('./config')
async function run() {
try {
@ -10,12 +11,11 @@ async function run() {
// 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
const content = await oneSky.getMultilingualFile({
apiKey: process.env.ONE_SKY_PUBLIC_KEY,
secret: process.env.ONE_SKY_PRIVATE_KEY,
projectId: '25049',
fileName: 'en-US.json'
})
const content = await oneSky.getMultilingualFile(
withAuth({
fileName: 'en-US.json'
})
)
const json = JSON.parse(content)
for (const [code, lang] of Object.entries(json)) {

View file

@ -3,9 +3,8 @@
"lockfileVersion": 1,
"dependencies": {
"@brainly/onesky-utils": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/@brainly/onesky-utils/-/onesky-utils-1.4.1.tgz",
"integrity": "sha512-Vi4u1xGUxS0IKqNZkHsgrd9z53/6P3oqdCV6Ao7O9itScnYjXfy00tZoagmAbrLtqbwsI4O08HjVQ2aYwW5vSg==",
"version": "https://github.com/overleaf/nodejs-onesky-utils/archive/jpa-upstream-pr-43.tar.gz",
"integrity": "sha512-kmA0+M1htZVjJrRU2TzWYVrRXSPiZAElKLGxAqhBa1llQj+OqBb7W7t5gDMPdP9Q0mXIgjmaCs6hlPl3awUOEw==",
"requires": {
"md5": "^2.0.0",
"request": "^2.88.0",

View file

@ -1,6 +1,6 @@
{
"dependencies": {
"@brainly/onesky-utils": "^1.4.0",
"@brainly/onesky-utils": "https://github.com/overleaf/nodejs-onesky-utils/archive/jpa-upstream-pr-43.tar.gz",
"sanitize-html": "^1.27.1"
}
}

View file

@ -0,0 +1,48 @@
const { promises: fs } = require('fs')
const { promisify } = require('util')
const oneSky = require('@brainly/onesky-utils')
const { withAuth } = require('./config')
const sleep = promisify(setTimeout)
async function uploadLocales() {
// Docs: https://github.com/onesky/api-documentation-platform/blob/master/resources/file.md#upload---upload-a-file
const blob = await oneSky.postFile(
withAuth({
fileName: 'en-US.json',
language: 'en-GB',
format: 'HIERARCHICAL_JSON',
content: await fs.readFile(`${__dirname}/../../locales/en.json`),
keepStrings: false // deprecate locales that no longer exist in en.json
})
)
return JSON.parse(blob).data.import.id
}
async function getImportTask(importId) {
// Docs: https://github.com/onesky/api-documentation-platform/blob/master/resources/import_task.md
const blob = await oneSky.getImportTask(withAuth({ importId }))
return JSON.parse(blob).data
}
async function pollUploadStatus(importId) {
let task
while ((task = await getImportTask(importId)).status === 'in-progress') {
console.log('onesky is processing the import ...')
await sleep(5000)
}
if (task.status === 'failed') {
console.error({ task })
throw new Error('upload failed')
}
}
async function main() {
const importId = await uploadLocales()
await pollUploadStatus(importId)
}
main().catch(error => {
console.error({ error })
process.exit(1)
})