download.js 1.7 KB

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