locales.js 1.52 KB
Newer Older
1 2
const Model = require('objection').Model

3 4
/* global WIKI */

5 6 7
/**
 * Locales model
 */
8
module.exports = class Locale extends Model {
9
  static get tableName() { return 'locales' }
10
  static get idColumn() { return 'code' }
11 12 13 14 15 16 17 18 19 20 21 22

  static get jsonSchema () {
    return {
      type: 'object',
      required: ['code', 'name'],

      properties: {
        code: {type: 'string'},
        isRTL: {type: 'boolean', default: false},
        name: {type: 'string'},
        nativeName: {type: 'string'},
        createdAt: {type: 'string'},
23 24
        updatedAt: {type: 'string'},
        availability: {type: 'integer'}
25 26 27 28
      }
    }
  }

29 30 31 32
  static get jsonAttributes() {
    return ['strings']
  }

33 34 35 36 37 38 39
  $beforeUpdate() {
    this.updatedAt = new Date().toISOString()
  }
  $beforeInsert() {
    this.createdAt = new Date().toISOString()
    this.updatedAt = new Date().toISOString()
  }
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

  static async getNavLocales({ cache = false } = {}) {
    if (!WIKI.config.lang.namespacing) {
      return []
    }

    if (cache) {
      const navLocalesCached = await WIKI.cache.get('nav:locales')
      if (navLocalesCached) {
        return navLocalesCached
      }
    }
    const navLocales = await WIKI.models.locales.query().select('code', 'nativeName AS name').whereIn('code', WIKI.config.lang.namespaces).orderBy('code')
    if (navLocales) {
      if (cache) {
        await WIKI.cache.set('nav:locales', navLocales, 300)
      }
      return navLocales
    } else {
      WIKI.logger.warn('Site Locales for navigation are missing or corrupted.')
      return []
    }
  }
63
}