i18n refactoring, locale loaders, custom strings, fix #231

This commit is contained in:
lesion 2023-02-07 17:44:23 +01:00
parent a36c8c29cb
commit 98f6b9618c
No known key found for this signature in database
GPG key ID: 352918250B012177
7 changed files with 61 additions and 34 deletions

13
locales/loader.js Normal file
View 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
}

View file

@ -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'],

View file

@ -35,7 +35,7 @@ export default {
},
activated() {
if (this.$fetchState.timestamp <= Date.now() - 60000) {
this.$fetch();
this.$fetch()
}
},
data ({ $store }) {

View file

@ -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')
}
}

View file

@ -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
// })
}
}

View 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

View file

@ -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))