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.

63 lines
1.5 KiB

  1. const Model = require('objection').Model
  2. /* global WIKI */
  3. /**
  4. * Locales model
  5. */
  6. module.exports = class Locale extends Model {
  7. static get tableName() { return 'locales' }
  8. static get idColumn() { return 'code' }
  9. static get jsonSchema () {
  10. return {
  11. type: 'object',
  12. required: ['code', 'name'],
  13. properties: {
  14. code: {type: 'string'},
  15. isRTL: {type: 'boolean', default: false},
  16. name: {type: 'string'},
  17. nativeName: {type: 'string'},
  18. createdAt: {type: 'string'},
  19. updatedAt: {type: 'string'},
  20. availability: {type: 'integer'}
  21. }
  22. }
  23. }
  24. static get jsonAttributes() {
  25. return ['strings']
  26. }
  27. $beforeUpdate() {
  28. this.updatedAt = new Date().toISOString()
  29. }
  30. $beforeInsert() {
  31. this.createdAt = new Date().toISOString()
  32. this.updatedAt = new Date().toISOString()
  33. }
  34. static async getNavLocales({ cache = false } = {}) {
  35. if (!WIKI.config.lang.namespacing) {
  36. return []
  37. }
  38. if (cache) {
  39. const navLocalesCached = await WIKI.cache.get('nav:locales')
  40. if (navLocalesCached) {
  41. return navLocalesCached
  42. }
  43. }
  44. const navLocales = await WIKI.models.locales.query().select('code', 'nativeName AS name').whereIn('code', WIKI.config.lang.namespaces).orderBy('code')
  45. if (navLocales) {
  46. if (cache) {
  47. await WIKI.cache.set('nav:locales', navLocales, 300)
  48. }
  49. return navLocales
  50. } else {
  51. WIKI.logger.warn('Site Locales for navigation are missing or corrupted.')
  52. return []
  53. }
  54. }
  55. }