diff --git a/config/default.json b/config/default.json index 43ec0d83..ee62fc46 100644 --- a/config/default.json +++ b/config/default.json @@ -2,6 +2,7 @@ "title": "Gancio", "description": "A shared agenda for local communities", "favicon" : "../dist/favicon.ico", + "user_locale": "./user_locale.json", "baseurl": "http://localhost:13120", "server": { "host": "localhost", diff --git a/middleware/i18n.js b/middleware/i18n.js index 8a393b72..419b12aa 100644 --- a/middleware/i18n.js +++ b/middleware/i18n.js @@ -1,6 +1,10 @@ +import acceptLanguage from 'accept-language' export default function ({ req, app, store }) { if (process.server) { - const lang = req.acceptsLanguages('en', 'it') + const acceptedLanguages = req.headers['accept-language'] + const supportedLanguages = ['en', 'it'] + acceptLanguage.languages(supportedLanguages) + const lang = acceptLanguage.get(acceptedLanguages) store.commit('setLocale', lang || 'it') app.i18n.locale = store.state.locale } diff --git a/nuxt.config.js b/nuxt.config.js index 03b1633e..9413cb37 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -13,9 +13,9 @@ module.exports = { link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }] }, dev: (process.env.NODE_ENV !== 'production'), - // serverMiddleware: [ - // { path: '/api', handler: '@/server/api/index.js' } - // ], + serverMiddleware: [ + { path: '/api', handler: '~/server/api/index.js' } + ], server: conf.server, @@ -81,21 +81,21 @@ module.exports = { ** Build configuration */ build: { - optimization: { - splitChunks: { - cacheGroups: { - element: { - test: /[\\/]node_modules[\\/](element-ui)[\\/]/, - name: 'element-ui', - chunks: 'all' - } - } - } - }, + // optimization: { + // splitChunks: { + // cacheGroups: { + // element: { + // test: /[\\/]node_modules[\\/](element-ui)[\\/]/, + // name: 'element-ui', + // chunks: 'all' + // } + // } + // } + // }, transpile: [/^element-ui/, /^vue-awesome/], - splitChunks: { - layouts: true - }, + // splitChunks: { + // layouts: true + // }, cache: true, } } diff --git a/package.json b/package.json index 84e743a1..6a755b34 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,11 @@ "description": "A shared agenda for local communities", "author": "lesion", "scripts": { + "dev:nuxt": "cross-env NODE_ENV=development nuxt dev", "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server", "build": "nuxt build", "start": "cross-env NODE_ENV=production node server/cli.js", + "start:nuxt": "cross-env NODE_ENV=development nuxt start", "lint": "eslint --ext .js,.vue --ignore-path .gitignore .", "doc": "cd docs && bundle exec jekyll b", "precommit": "npm run lint", @@ -38,6 +40,7 @@ "dependencies": { "@nuxtjs/auth": "^4.6.5", "@nuxtjs/axios": "^5.5.3", + "accept-language": "^3.0.18", "axios": "^0.19.0", "bcryptjs": "^2.4.3", "body-parser": "^1.18.3", diff --git a/pages/event/_id.vue b/pages/event/_id.vue index 06a00193..69333c92 100644 --- a/pages/event/_id.vue +++ b/pages/event/_id.vue @@ -102,7 +102,9 @@ export default { try { const [ id, start_datetime ] = params.id.split('_') const event = await $axios.$get(`/event/${id}`) - event.start_datetime = start_datetime*1000 + event.start_datetime = start_datetime ? start_datetime*1000 : event.start_datetime*1000 + event.end_datetime = event.end_datetime*1000 + // event.start_datetime = start_datetime ? start_datetime*1000 : event.start return { event, id } } catch(e) { error({ statusCode: 404, message: 'Event not found'}) diff --git a/plugins/i18n.js b/plugins/i18n.js index 00c35d39..1882696f 100644 --- a/plugins/i18n.js +++ b/plugins/i18n.js @@ -2,17 +2,21 @@ 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' Vue.use(VueI18n) -export default ({ app, store }) => { +export default async ({ app, store }) => { // Set i18n instance on app // This way we can use it in middleware and pages asyncData/fetch + + const user_locale = await app.$axios.$get('/settings/user_locale') app.i18n = new VueI18n({ locale: store.state.locale, fallbackLocale: 'it', messages: { - it, en + it: merge(it, user_locale), + en: merge(en, user_locale) } }) } diff --git a/server/api/controller/event.js b/server/api/controller/event.js index e2b4d781..e6af71fd 100644 --- a/server/api/controller/event.js +++ b/server/api/controller/event.js @@ -100,8 +100,8 @@ const eventController = { order: [ [Comment, 'id', 'DESC'] ] }) - event.start_datetime = event.start_datetime*1000 - event.end_datetime = event.end_datetime*1000 + // event.start_datetime = event.start_datetime*1000 + // event.end_datetime = event.end_datetime*1000 if (event && (event.is_visible || is_admin)) { res.json(event) diff --git a/server/api/controller/settings.js b/server/api/controller/settings.js index f4912175..f6553738 100644 --- a/server/api/controller/settings.js +++ b/server/api/controller/settings.js @@ -1,6 +1,13 @@ const Mastodon = require('mastodon-api') const { setting: Setting } = require('../models') const config = require('config') +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 }, @@ -38,11 +45,13 @@ const settingsController = { }, async getUserLocale(req, res) { - // console.error(res) + console.error(user_locale_path) // load user locale specified in configuration - // res.json({ about: 'dentro user locale' }) - // res.sendStatus(200) - return false + if (user_locale_path) { + res.json(require(user_locale_path)) + } else { + res.json({}) + } }, async setRequest(req, res) { diff --git a/server/api/index.js b/server/api/index.js index 7bdec081..d40b909a 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -14,7 +14,7 @@ const settingsController = require('./controller/settings') const storage = require('./storage') const upload = multer({ storage }) -const api = express.Router() +const api = express() api.use(cookieParser()) api.use(bodyParser.urlencoded({ extended: false })) api.use(bodyParser.json())