download.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const path = require('path')
  2. const { promises: fs } = require('fs')
  3. const oneSky = require('@brainly/onesky-utils')
  4. const { sanitize } = require('./sanitize')
  5. const { withAuth } = require('./config')
  6. async function run() {
  7. try {
  8. // The recommended OneSky set-up appears to require an API request to
  9. // generate files on their side, which you could then request and use. We
  10. // only have 1 such file that appears to be misnamed (en-US, despite our
  11. // translations being marked as GB) and very out-of-date.
  12. // However by requesting the "multilingual file" for this file, we get all
  13. // of the translations
  14. const content = await oneSky.getMultilingualFile(
  15. withAuth({
  16. fileName: 'en-US.json',
  17. })
  18. )
  19. const json = JSON.parse(content)
  20. for (const [code, lang] of Object.entries(json)) {
  21. if (code === 'en-GB') {
  22. // OneSky does not have read-after-write consistency.
  23. // Skip the dump of English locales, which may not include locales
  24. // that were just uploaded.
  25. continue
  26. }
  27. for (let [key, value] of Object.entries(lang.translation)) {
  28. // Handle multi-line strings as arrays by joining on newline
  29. if (Array.isArray(value)) {
  30. value = value.join('\n')
  31. }
  32. lang.translation[key] = sanitize(value)
  33. }
  34. await fs.writeFile(
  35. path.join(__dirname, `/../../locales/${code}.json`),
  36. JSON.stringify(
  37. lang.translation,
  38. Object.keys(lang.translation).sort(),
  39. 2
  40. ) + '\n'
  41. )
  42. }
  43. } catch (error) {
  44. console.error(error)
  45. process.exit(1)
  46. }
  47. }
  48. run()