From 04943a36b398eac2593a155b4c64e66e6fc0be2c Mon Sep 17 00:00:00 2001 From: lesion Date: Fri, 26 Jul 2019 23:51:32 +0200 Subject: [PATCH] user locale --- config/default.json | 2 +- docs/dev.md | 33 ------------------------------- locales/it.js | 3 +-- plugins/i18n.js | 12 +++++------ server/api/controller/settings.js | 30 +++++++++++++++++----------- server/api/index.js | 1 - views/feed/rss.pug | 2 +- 7 files changed, 28 insertions(+), 55 deletions(-) delete mode 100644 docs/dev.md diff --git a/config/default.json b/config/default.json index ee62fc46..2bffa013 100644 --- a/config/default.json +++ b/config/default.json @@ -2,7 +2,7 @@ "title": "Gancio", "description": "A shared agenda for local communities", "favicon" : "../dist/favicon.ico", - "user_locale": "./user_locale.json", + "user_locale": "./user_locale", "baseurl": "http://localhost:13120", "server": { "host": "localhost", diff --git a/docs/dev.md b/docs/dev.md deleted file mode 100644 index 6eb4fa54..00000000 --- a/docs/dev.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -layout: default -title: Develop -permalink: /dev -nav_order: 5 ---- - -### Development Stack - -**Gancio** is built with following technologies: - -- [Nuxt.js](https://nuxtjs.org/) -- Vue.js -- Express -- Sequelize -- Element.ui - -### Testing on your own machine - -2. Download source -```bash -git clone https://git.lattuga.net/cisti/gancio -``` - -3. Install dependencies -```bash -yarn -``` - -4. Hacking -```bash -yarn dev -``` diff --git a/locales/it.js b/locales/it.js index a684b7c7..802681ce 100644 --- a/locales/it.js +++ b/locales/it.js @@ -1,4 +1,4 @@ -const it = { +export default { common: { add_event: 'Nuovo evento', next: 'Continua', @@ -210,4 +210,3 @@ const it = { } -export default it diff --git a/plugins/i18n.js b/plugins/i18n.js index 1882696f..4f5b69a6 100644 --- a/plugins/i18n.js +++ b/plugins/i18n.js @@ -1,8 +1,7 @@ import Vue from 'vue' import VueI18n from 'vue-i18n' -import it from '../locales/it.js' -import en from '../locales/en.js' import merge from 'lodash/merge' +import locales from '../locales' Vue.use(VueI18n) @@ -11,12 +10,13 @@ export default async ({ app, store }) => { // This way we can use it in middleware and pages asyncData/fetch const user_locale = await app.$axios.$get('/settings/user_locale') + for(let lang in user_locale) { + if (locales[lang]) merge(locales[lang], user_locale[lang]) + } + app.i18n = new VueI18n({ locale: store.state.locale, fallbackLocale: 'it', - messages: { - it: merge(it, user_locale), - en: merge(en, user_locale) - } + messages: locales }) } diff --git a/server/api/controller/settings.js b/server/api/controller/settings.js index f7445c13..add82756 100644 --- a/server/api/controller/settings.js +++ b/server/api/controller/settings.js @@ -1,21 +1,21 @@ const Mastodon = require('mastodon-api') const { setting: Setting } = require('../models') const config = require('config') +const consola = require('consola') const path = require('path') const fs = require('fs') -let user_locale_path = false -if (config.user_locale && fs.existsSync(path.resolve(config.user_locale))) { - user_locale_path = path.resolve(config.user_locale) -} - const settingsController = { settings: { initialized: false }, + user_locale: {}, secretSettings: {}, - // initialize instance settings from db async initialize () { if (!settingsController.settings.initialized) { + + // initialize instance settings from db + // note that this is done only once when the server starts + // and not for each request (it's a kind of cache)! const settings = await Setting.findAll() settingsController.settings.initialized = true settings.forEach( s => { @@ -25,6 +25,18 @@ const settingsController = { settingsController.settings[s.key] = s.value } }) + + // initialize user_locale + if (config.user_locale && fs.existsSync(path.resolve(config.user_locale))) { + const user_locale = fs.readdirSync(path.resolve(config.user_locale)) + user_locale.forEach( async f => { + consola.info(`Loading user locale ${f}`) + const locale = path.basename(f, '.js') + settingsController.user_locale[locale] = + (await import(path.resolve(config.user_locale, f))).default + }) + } + } }, @@ -46,11 +58,7 @@ const settingsController = { async getUserLocale(req, res) { // load user locale specified in configuration - if (user_locale_path) { - res.json(require(user_locale_path)) - } else { - res.json({}) - } + res.json(settingsController.user_locale) }, async setRequest(req, res) { diff --git a/server/api/index.js b/server/api/index.js index d40b909a..798aa3fe 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -94,7 +94,6 @@ api.get('/event/unconfirm/:event_id', jwt, isAuth, isAdmin, eventController.unco // get event api.get('/event/:event_id', jwt, fillUser, eventController.get) - // export events (rss/ics) api.get('/export/:type', exportController.export) diff --git a/views/feed/rss.pug b/views/feed/rss.pug index 41dbfdd5..716b00ca 100644 --- a/views/feed/rss.pug +++ b/views/feed/rss.pug @@ -20,4 +20,4 @@ rss(version='2.0') |
!{event.description}
| ]]> pubDate= new Date(event.createdAt).toUTCString() - guid(isPermaLink='false') #{config.baseurl}/event/#{event.id} \ No newline at end of file + guid(isPermaLink='false') #{config.baseurl}/event/#{event.id}