locales.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { Model } from 'objection'
  2. import { find } from 'lodash-es'
  3. import { stat, readFile } from 'node:fs/promises'
  4. import path from 'node:path'
  5. import { DateTime } from 'luxon'
  6. /**
  7. * Locales model
  8. */
  9. export class Locale extends Model {
  10. static get tableName() { return 'locales' }
  11. static get idColumn() { return 'code' }
  12. static get jsonSchema () {
  13. return {
  14. type: 'object',
  15. required: ['code', 'name'],
  16. properties: {
  17. code: {type: 'string'},
  18. isRTL: {type: 'boolean', default: false},
  19. name: {type: 'string'},
  20. nativeName: {type: 'string'},
  21. createdAt: {type: 'string'},
  22. updatedAt: {type: 'string'},
  23. completeness: {type: 'integer'}
  24. }
  25. }
  26. }
  27. static get jsonAttributes() {
  28. return ['strings']
  29. }
  30. $beforeUpdate() {
  31. this.updatedAt = new Date().toISOString()
  32. }
  33. $beforeInsert() {
  34. this.createdAt = new Date().toISOString()
  35. this.updatedAt = new Date().toISOString()
  36. }
  37. static async refreshFromDisk ({ force = false } = {}) {
  38. try {
  39. const localesMeta = (await import(`../locales/metadata.mjs`)).default
  40. WIKI.logger.info(`Found ${localesMeta.languages.length} locales: [ OK ]`)
  41. const dbLocales = await WIKI.db.locales.query().select('code', 'updatedAt')
  42. let localFilesSkipped = 0
  43. for (const lang of localesMeta.languages) {
  44. // -> Build filename
  45. const langFilenameParts = [lang.language]
  46. if (lang.region) {
  47. langFilenameParts.push(lang.region)
  48. }
  49. if (lang.script) {
  50. langFilenameParts.push(lang.script)
  51. }
  52. const langFilename = langFilenameParts.join('-')
  53. // -> Get DB version
  54. const dbLang = find(dbLocales, ['code', langFilename])
  55. // -> Get File version
  56. const flPath = path.join(WIKI.SERVERPATH, `locales/${langFilename}.json`)
  57. try {
  58. const flStat = await stat(flPath)
  59. const flUpdatedAt = DateTime.fromJSDate(flStat.mtime)
  60. // -> Load strings
  61. if (!dbLang || DateTime.fromJSDate(dbLang.updatedAt) < flUpdatedAt || force) {
  62. WIKI.logger.debug(`Loading locale ${langFilename} into DB...`)
  63. const flStrings = JSON.parse(await readFile(flPath, 'utf8'))
  64. await WIKI.db.locales.query().insert({
  65. code: langFilename,
  66. name: lang.name,
  67. nativeName: lang.localizedName,
  68. language: lang.language,
  69. region: lang.region,
  70. script: lang.script,
  71. isRTL: lang.isRtl,
  72. strings: flStrings
  73. }).onConflict('code').merge(['strings', 'updatedAt'])
  74. } else {
  75. WIKI.logger.debug(`Locale ${langFilename} is newer in the DB. Skipping disk version. [ OK ]`)
  76. }
  77. } catch (err) {
  78. localFilesSkipped++
  79. WIKI.logger.debug(`Locale ${langFilename} not found on disk. Missing strings file. [ SKIPPED ]`)
  80. }
  81. }
  82. if (localFilesSkipped > 0) {
  83. WIKI.logger.info(`${localFilesSkipped} locales were defined in the metadata file but not found on disk. [ SKIPPED ]`)
  84. }
  85. } catch (err) {
  86. WIKI.logger.warn(`Failed to load locales from disk: [ FAILED ]`)
  87. WIKI.logger.warn(err)
  88. return false
  89. }
  90. }
  91. static async getLocales ({ cache = true } = {}) {
  92. if (!WIKI.cache.has('locales') || !cache) {
  93. const locales = await WIKI.db.locales.query().select('code', 'isRTL', 'language', 'name', 'nativeName', 'createdAt', 'updatedAt', 'completeness')
  94. WIKI.cache.set('locales', locales)
  95. for (const locale of locales) {
  96. WIKI.cache.set(`locale:${locale.code}`, locale)
  97. }
  98. }
  99. return WIKI.cache.get('locales')
  100. }
  101. static async getStrings (locale) {
  102. const { strings } = await WIKI.db.locales.query().findOne('code', locale).column('strings')
  103. return strings
  104. }
  105. static async reloadCache () {
  106. await WIKI.db.locales.getLocales({ cache: false })
  107. }
  108. static async getNavLocales({ cache = false } = {}) {
  109. return []
  110. // if (!WIKI.config.lang.namespacing) {
  111. // return []
  112. // }
  113. // if (cache) {
  114. // const navLocalesCached = await WIKI.cache.get('nav:locales')
  115. // if (navLocalesCached) {
  116. // return navLocalesCached
  117. // }
  118. // }
  119. // const navLocales = await WIKI.db.locales.query().select('code', 'nativeName AS name').whereIn('code', WIKI.config.lang.namespaces).orderBy('code')
  120. // if (navLocales) {
  121. // if (cache) {
  122. // await WIKI.cache.set('nav:locales', navLocales, 300)
  123. // }
  124. // return navLocales
  125. // } else {
  126. // WIKI.logger.warn('Site Locales for navigation are missing or corrupted.')
  127. // return []
  128. // }
  129. }
  130. }