You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.8 KiB

  1. const Promise = require('bluebird')
  2. const colors = require('colors/safe')
  3. const fs = Promise.promisifyAll(require('fs-extra'))
  4. const path = require('path')
  5. const request = require('request-promise')
  6. const yaml = require('js-yaml')
  7. const _ = require('lodash')
  8. const config = yaml.safeLoad(fs.readFileSync(path.join(process.cwd(), 'dev/config/config.yml'), 'utf8'))
  9. /**
  10. * Fetch Localization Resources from Lokalise
  11. */
  12. const fetchLocalizationResources = async () => {
  13. console.info(colors.green('Fetching latest localization resources...'))
  14. let langs = await request({
  15. method: 'POST',
  16. uri: `${config.lokalise.api}/string/list`,
  17. form: {
  18. api_token: config.lokalise.key,
  19. id: config.lokalise.project
  20. },
  21. json: true
  22. })
  23. if (langs && langs.strings && _.isPlainObject(langs.strings)) {
  24. _.forIn(langs.strings, (langData, langKey) => {
  25. let lang = {}
  26. let langTotal = 0
  27. langData.forEach(item => {
  28. if (item.is_archived === '1' || _.includes(item.key, '::')) { return }
  29. let keyParts = item.key.split(':')
  30. let keyNamespace = (keyParts.length > 1) ? _.head(keyParts) : 'common'
  31. let keyString = _.last(keyParts)
  32. _.set(lang, `${keyNamespace}.${keyString}`, item.translation)
  33. langTotal++
  34. })
  35. _.forOwn(lang, (langObject, langNamespace) => {
  36. let langYaml = yaml.safeDump(langObject, {
  37. indent: 2,
  38. sortKeys: true,
  39. lineWidth: 2048
  40. })
  41. fs.outputFileSync(path.join(process.cwd(), `server/locales/${langKey}/${langNamespace}.yml`), langYaml, 'utf8')
  42. })
  43. console.info(colors.grey(`└─ ${langKey} - ${langTotal} keys written`))
  44. })
  45. } else {
  46. throw new Error('Failed to fetch language list from Lokalise API.')
  47. }
  48. }
  49. try {
  50. fetchLocalizationResources()
  51. } catch (err) {
  52. console.error(colors.red(err))
  53. }