salix/print/core/mixins/user-locale.js

32 lines
931 B
JavaScript

const Vue = require('vue');
const db = require('../database');
const config = require('../config');
const fallbackLocale = config.i18n.fallbackLocale;
const userLocale = {
async serverPrefetch() {
if (this.clientId)
this.locale = await this.getLocale(this.clientId);
if (this.locale)
this.$i18n.locale = this.locale;
},
methods: {
getLocale(clientId) {
return db.findOne(`
SELECT IF(u.lang IS NOT NULL, u.lang, LOWER(ct.code)) lang
FROM client c
JOIN country ct ON ct.id = c.countryFk
JOIN account.user u ON u.id = c.id
WHERE c.id = ?`, [clientId]).then(rows => {
if (rows)
return rows.lang;
else return fallbackLocale;
});
}
},
props: ['clientId']
};
Vue.mixin(userLocale);