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.

76 lines
2.5 KiB

  1. const fs = require('fs-extra')
  2. const path = require('path')
  3. const _ = require('lodash')
  4. /* global WIKI */
  5. module.exports = {
  6. async init () {
  7. if (!WIKI.config.offline) {
  8. return
  9. }
  10. const sideloadExists = await fs.pathExists(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'sideload'))
  11. if (!sideloadExists) {
  12. return
  13. }
  14. WIKI.logger.info('Sideload directory detected. Looking for packages...')
  15. try {
  16. await this.importLocales()
  17. } catch (err) {
  18. WIKI.logger.warn(err)
  19. }
  20. },
  21. async importLocales() {
  22. const localeExists = await fs.pathExists(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'sideload/locales.json'))
  23. if (localeExists) {
  24. WIKI.logger.info('Found locales master file. Importing locale packages...')
  25. let importedLocales = 0
  26. const locales = await fs.readJson(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'sideload/locales.json'))
  27. if (locales && _.has(locales, 'data.localization.locales')) {
  28. for (const locale of locales.data.localization.locales) {
  29. try {
  30. const localeData = await fs.readJson(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `sideload/${locale.code}.json`))
  31. if (localeData) {
  32. WIKI.logger.info(`Importing ${locale.name} locale package...`)
  33. let lcObj = {}
  34. _.forOwn(localeData, (value, key) => {
  35. if (_.includes(key, '::')) { return }
  36. if (_.isEmpty(value)) { value = key }
  37. _.set(lcObj, key.replace(':', '.'), value)
  38. })
  39. const localeDbExists = await WIKI.models.locales.query().select('code').where('code', locale.code).first()
  40. if (localeDbExists) {
  41. await WIKI.models.locales.query().update({
  42. code: locale.code,
  43. strings: lcObj,
  44. isRTL: locale.isRTL,
  45. name: locale.name,
  46. nativeName: locale.nativeName
  47. }).where('code', locale.code)
  48. } else {
  49. await WIKI.models.locales.query().insert({
  50. code: locale.code,
  51. strings: lcObj,
  52. isRTL: locale.isRTL,
  53. name: locale.name,
  54. nativeName: locale.nativeName
  55. })
  56. }
  57. importedLocales++
  58. }
  59. } catch (err) {
  60. // skip
  61. }
  62. }
  63. WIKI.logger.info(`Imported ${importedLocales} locale packages: [COMPLETED]`)
  64. }
  65. }
  66. }
  67. }