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.

62 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. }
  21. }
  22. }
  23. static get jsonAttributes() {
  24. return ['strings']
  25. }
  26. $beforeUpdate() {
  27. this.updatedAt = new Date().toISOString()
  28. }
  29. $beforeInsert() {
  30. this.createdAt = new Date().toISOString()
  31. this.updatedAt = new Date().toISOString()
  32. }
  33. static async getNavLocales({ cache = false } = {}) {
  34. if (!WIKI.config.lang.namespacing) {
  35. return []
  36. }
  37. if (cache) {
  38. const navLocalesCached = await WIKI.cache.get('nav:locales')
  39. if (navLocalesCached) {
  40. return navLocalesCached
  41. }
  42. }
  43. const navLocales = await WIKI.models.locales.query().select('code', 'nativeName AS name').whereIn('code', WIKI.config.lang.namespaces).orderBy('code')
  44. if (navLocales) {
  45. if (cache) {
  46. await WIKI.cache.set('nav:locales', navLocales, 300)
  47. }
  48. return navLocales
  49. } else {
  50. WIKI.logger.warn('Site Locales for navigation are missing or corrupted.')
  51. return []
  52. }
  53. }
  54. }