diff --git a/locales/loader.js b/locales/loader.js new file mode 100644 index 00000000..e289830a --- /dev/null +++ b/locales/loader.js @@ -0,0 +1,13 @@ +export default async (context, locale) => { + try { + if (process.server) { + return context.$axios.$get(`locale/${locale}`) + } else { + return fetch(`${window.location.origin}/api/locale/${locale}`).then(ret => ret.json()) + } + } catch (e) { + console.error(`Error loading locale ${locale}`, e) + } + + return localeMessages +} \ No newline at end of file diff --git a/nuxt.config.js b/nuxt.config.js index 214f9377..72eb06d1 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -89,6 +89,7 @@ module.exports = { code: key, name: locales[key], file: `${key}.json`, + file: 'loader.js', iso: key })), vueI18n: { @@ -98,9 +99,7 @@ module.exports = { langDir: 'locales', lazy: true, strategy: 'no_prefix', - baseUrl: config.baseurl, skipSettingLocaleOnNavigate: true, - skipNuxtState: true }, serverMiddleware: ['server/routes'], diff --git a/pages/index.vue b/pages/index.vue index e02c2fa7..5a01f421 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -35,7 +35,7 @@ export default { }, activated() { if (this.$fetchState.timestamp <= Date.now() - 60000) { - this.$fetch(); + this.$fetch() } }, data ({ $store }) { diff --git a/plugins/axios.js b/plugins/axios.js index dba773fc..6be21654 100644 --- a/plugins/axios.js +++ b/plugins/axios.js @@ -1,5 +1,8 @@ export default function ({ $axios, store }) { if (process.client) { - $axios.defaults.baseURL = window.location.origin + '/api' + $axios.setBaseURL(window.location.origin + '/api') + } else { + const config = require('../server/config') + $axios.setBaseURL(config.baseurl + '/api') } } diff --git a/plugins/i18n.js b/plugins/i18n.js index 0b45be42..88ad4536 100644 --- a/plugins/i18n.js +++ b/plugins/i18n.js @@ -1,36 +1,8 @@ -export default async ({ app, store, res, $vuetify }) => { +export default async ({ app, $vuetify }) => { $vuetify.lang.current = app.i18n.locale app.i18n.onLanguageSwitched = (oldLocale, newLocale) => { $vuetify.lang.current = newLocale } - - // const messages = {} - // if (process.server) { - // if (res.locals) { - // store.commit('setLocale', res.locals.acceptedLocale) - // if (res.locals.user_locale) { - // store.commit('setUserLocale', res.locals.user_locale) - // } - // } - // } - - // messages[store.state.locale] = await import(/* webpackChunkName: "lang-[request]" */`../locales/${store.state.locale}.json`) - - // always include en fallback locale - // if (store.state.locale !== 'en') { - // messages.en = await import('../locales/en.json') - // } - - // if (store.state.user_locale) { - // merge(messages[store.state.locale], store.state.user_locale) - // } - - // Set i18n instance on app - // app.i18n = new VueI18n({ - // locale: store.state.locale, - // fallbackLocale: 'en', - // messages - // }) -} +} \ No newline at end of file diff --git a/server/api/controller/locale.js b/server/api/controller/locale.js new file mode 100644 index 00000000..865b3723 --- /dev/null +++ b/server/api/controller/locale.js @@ -0,0 +1,36 @@ +const merge = require('lodash/merge') +const config = require('../../config') +const path = require('path') +const fs = require('fs') +const log = require('../../log') + +const localeController = { + async get (req, res) { + const locale = req.params.locale + const locales = require('../../../locales/index') + + // check if this locale exists + if (!locales[locale]) { + return res.sendStatus(404) + } + + const defaultLocaleMessages = require(`../../../locales/${locale}.json`) + + // check if we have a user custom messages + let customLocaleMessages = {} + const customLocalePath = path.resolve(config.user_locale, `${locale}.json`) + if (config.user_locale && fs.existsSync(customLocalePath)) { + try { + customLocaleMessages = require(customLocalePath) + } catch (e) { + log.error(`Error reading custom locale messages: ${e}`) + } + } + + const ret = merge(defaultLocaleMessages, customLocaleMessages) + return res.json(ret) + + } +} + +module.exports = localeController \ No newline at end of file diff --git a/server/api/index.js b/server/api/index.js index a44f231d..9fb8ca52 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -20,6 +20,7 @@ const oauthController = require('./controller/oauth') const announceController = require('./controller/announce') const pluginController = require('./controller/plugins') const geocodingController = require('./controller/geocoding') +const localeController = require('./controller/locale') const { DDOSProtectionApiRateLimiter, SPAMProtectionApiRateLimiter } = require('./limiter') const helpers = require('../helpers') const storage = require('./storage') @@ -221,6 +222,9 @@ module.exports = () => { api.get('/clients', isAuth, oauthController.getClients) api.get('/client/:client_id', isAuth, oauthController.getClient) api.post('/client', SPAMProtectionApiRateLimiter, oauthController.createClient) + + // CUSTOM LOCALE + api.get('/locale/:locale', localeController.get) } api.use((_req, res) => res.sendStatus(404))