i18n refactoring, locale loaders, custom strings, fix #231
This commit is contained in:
13
locales/loader.js
Normal file
13
locales/loader.js
Normal file
@@ -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
|
||||||
|
}
|
||||||
@@ -89,6 +89,7 @@ module.exports = {
|
|||||||
code: key,
|
code: key,
|
||||||
name: locales[key],
|
name: locales[key],
|
||||||
file: `${key}.json`,
|
file: `${key}.json`,
|
||||||
|
file: 'loader.js',
|
||||||
iso: key
|
iso: key
|
||||||
})),
|
})),
|
||||||
vueI18n: {
|
vueI18n: {
|
||||||
@@ -98,9 +99,7 @@ module.exports = {
|
|||||||
langDir: 'locales',
|
langDir: 'locales',
|
||||||
lazy: true,
|
lazy: true,
|
||||||
strategy: 'no_prefix',
|
strategy: 'no_prefix',
|
||||||
baseUrl: config.baseurl,
|
|
||||||
skipSettingLocaleOnNavigate: true,
|
skipSettingLocaleOnNavigate: true,
|
||||||
skipNuxtState: true
|
|
||||||
},
|
},
|
||||||
|
|
||||||
serverMiddleware: ['server/routes'],
|
serverMiddleware: ['server/routes'],
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export default {
|
|||||||
},
|
},
|
||||||
activated() {
|
activated() {
|
||||||
if (this.$fetchState.timestamp <= Date.now() - 60000) {
|
if (this.$fetchState.timestamp <= Date.now() - 60000) {
|
||||||
this.$fetch();
|
this.$fetch()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data ({ $store }) {
|
data ({ $store }) {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
export default function ({ $axios, store }) {
|
export default function ({ $axios, store }) {
|
||||||
if (process.client) {
|
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')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,8 @@
|
|||||||
export default async ({ app, store, res, $vuetify }) => {
|
export default async ({ app, $vuetify }) => {
|
||||||
|
|
||||||
$vuetify.lang.current = app.i18n.locale
|
$vuetify.lang.current = app.i18n.locale
|
||||||
|
|
||||||
app.i18n.onLanguageSwitched = (oldLocale, newLocale) => {
|
app.i18n.onLanguageSwitched = (oldLocale, newLocale) => {
|
||||||
$vuetify.lang.current = 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
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
36
server/api/controller/locale.js
Normal file
36
server/api/controller/locale.js
Normal file
@@ -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
|
||||||
@@ -20,6 +20,7 @@ const oauthController = require('./controller/oauth')
|
|||||||
const announceController = require('./controller/announce')
|
const announceController = require('./controller/announce')
|
||||||
const pluginController = require('./controller/plugins')
|
const pluginController = require('./controller/plugins')
|
||||||
const geocodingController = require('./controller/geocoding')
|
const geocodingController = require('./controller/geocoding')
|
||||||
|
const localeController = require('./controller/locale')
|
||||||
const { DDOSProtectionApiRateLimiter, SPAMProtectionApiRateLimiter } = require('./limiter')
|
const { DDOSProtectionApiRateLimiter, SPAMProtectionApiRateLimiter } = require('./limiter')
|
||||||
const helpers = require('../helpers')
|
const helpers = require('../helpers')
|
||||||
const storage = require('./storage')
|
const storage = require('./storage')
|
||||||
@@ -221,6 +222,9 @@ module.exports = () => {
|
|||||||
api.get('/clients', isAuth, oauthController.getClients)
|
api.get('/clients', isAuth, oauthController.getClients)
|
||||||
api.get('/client/:client_id', isAuth, oauthController.getClient)
|
api.get('/client/:client_id', isAuth, oauthController.getClient)
|
||||||
api.post('/client', SPAMProtectionApiRateLimiter, oauthController.createClient)
|
api.post('/client', SPAMProtectionApiRateLimiter, oauthController.createClient)
|
||||||
|
|
||||||
|
// CUSTOM LOCALE
|
||||||
|
api.get('/locale/:locale', localeController.get)
|
||||||
}
|
}
|
||||||
|
|
||||||
api.use((_req, res) => res.sendStatus(404))
|
api.use((_req, res) => res.sendStatus(404))
|
||||||
|
|||||||
Reference in New Issue
Block a user