Language now managed in VueX store

This commit is contained in:
Alicia Sykes 2021-10-10 15:52:39 +01:00
parent 048f0cb112
commit 4e3b41332d
4 changed files with 28 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import Footer from '@/components/PageStrcture/Footer.vue';
import LoadingScreen from '@/components/PageStrcture/LoadingScreen.vue'; import LoadingScreen from '@/components/PageStrcture/LoadingScreen.vue';
import { welcomeMsg } from '@/utils/CoolConsole'; import { welcomeMsg } from '@/utils/CoolConsole';
import ErrorHandler from '@/utils/ErrorHandler'; import ErrorHandler from '@/utils/ErrorHandler';
import Keys from '@/utils/StoreMutations';
import { import {
localStorageKeys, localStorageKeys,
splashScreenTime, splashScreenTime,
@ -107,6 +108,7 @@ export default {
/* Fetch or detect users language, then apply it */ /* Fetch or detect users language, then apply it */
applyLanguage() { applyLanguage() {
const language = this.getLanguage(); const language = this.getLanguage();
this.$store.commit(Keys.SET_LANGUAGE, language);
this.$i18n.locale = language; this.$i18n.locale = language;
document.getElementsByTagName('html')[0].setAttribute('lang', language); document.getElementsByTagName('html')[0].setAttribute('lang', language);
}, },

View File

@ -28,6 +28,7 @@
import Button from '@/components/FormElements/Button'; import Button from '@/components/FormElements/Button';
import SaveConfigIcon from '@/assets/interface-icons/save-config.svg'; import SaveConfigIcon from '@/assets/interface-icons/save-config.svg';
import ErrorHandler from '@/utils/ErrorHandler'; import ErrorHandler from '@/utils/ErrorHandler';
import Keys from '@/utils/StoreMutations';
import { languages } from '@/utils/languages'; import { languages } from '@/utils/languages';
import { localStorageKeys, modalNames } from '@/utils/defaults'; import { localStorageKeys, modalNames } from '@/utils/defaults';
@ -39,15 +40,28 @@ export default {
}, },
data() { data() {
return { return {
language: this.getCurrentLanguage(), // The currently selected language language: '', // The currently selected language
modalName: modalNames.LANG_SWITCHER, // Key for modal modalName: modalNames.LANG_SWITCHER, // Key for modal
}; };
}, },
created() {
// Initiate the current language, with VueX state
this.language = this.savedLanguage;
},
computed: { computed: {
/* Get appConfig from store */ /* Get appConfig from store */
appConfig() { appConfig() {
return this.$store.getters.appConfig; return this.$store.getters.appConfig;
}, },
/* The ISO code for the users language, synced with VueX store */
savedLanguage: {
get() {
return this.getIsoFromLangObj(this.$store.state.lang);
},
set(newLang) {
this.$store.commit(Keys.SET_LANGUAGE, newLang.code);
},
},
/* Return the array of language objects, plus a friends name */ /* Return the array of language objects, plus a friends name */
languageList: () => languages.map((lang) => { languageList: () => languages.map((lang) => {
const newLang = lang; const newLang = lang;
@ -76,6 +90,7 @@ export default {
if (this.checkLocale(selectedLanguage)) { if (this.checkLocale(selectedLanguage)) {
localStorage.setItem(localStorageKeys.LANGUAGE, selectedLanguage.code); localStorage.setItem(localStorageKeys.LANGUAGE, selectedLanguage.code);
this.applyLanguageLocally(); this.applyLanguageLocally();
this.savedLanguage = selectedLanguage;
const successMsg = `${selectedLanguage.flag} ` const successMsg = `${selectedLanguage.flag} `
+ `${this.$t('language-switcher.success-msg')} ${selectedLanguage.name}`; + `${this.$t('language-switcher.success-msg')} ${selectedLanguage.name}`;
this.$toasted.show(successMsg, { className: 'toast-success' }); this.$toasted.show(successMsg, { className: 'toast-success' });
@ -85,11 +100,10 @@ export default {
ErrorHandler('Unable to apply language'); ErrorHandler('Unable to apply language');
} }
}, },
/* Gets the users current language from local storage */ /* Gets the ISO code for a given language object */
getCurrentLanguage() { getIsoFromLangObj(langObj) {
const getLanguageFromIso = (iso) => languages.find((lang) => lang.code === iso); const getLanguageFromIso = (iso) => languages.find((lang) => lang.code === iso);
const current = localStorage[localStorageKeys.LANGUAGE] || this.appConfig.language; return getLanguageFromIso(langObj);
return getLanguageFromIso(current);
}, },
}, },
}; };

View File

@ -8,11 +8,12 @@ import filterUserSections from '@/utils/CheckSectionVisibility';
Vue.use(Vuex); Vue.use(Vuex);
const { UPDATE_CONFIG, SET_MODAL_OPEN } = Keys; const { UPDATE_CONFIG, SET_MODAL_OPEN, SET_LANGUAGE } = Keys;
const store = new Vuex.Store({ const store = new Vuex.Store({
state: { state: {
config: {}, config: {},
lang: '', // The users language, auto-detected or read from local storage / config
modalOpen: false, // KB shortcut functionality will be disabled when modal is open modalOpen: false, // KB shortcut functionality will be disabled when modal is open
}, },
getters: { getters: {
@ -39,11 +40,15 @@ const store = new Vuex.Store({
[UPDATE_CONFIG](state, config) { [UPDATE_CONFIG](state, config) {
state.config = config; state.config = config;
}, },
[SET_LANGUAGE](state, lang) {
state.lang = lang;
},
[SET_MODAL_OPEN](state, modalOpen) { [SET_MODAL_OPEN](state, modalOpen) {
state.modalOpen = modalOpen; state.modalOpen = modalOpen;
}, },
}, },
actions: { actions: {
/* Called when app first loaded. Reads config and sets state */
initializeConfig({ commit }) { initializeConfig({ commit }) {
const Accumulator = new ConfigAccumulator(); const Accumulator = new ConfigAccumulator();
const config = Accumulator.config(); const config = Accumulator.config();

View File

@ -2,6 +2,7 @@
const KEY_NAMES = [ const KEY_NAMES = [
'UPDATE_CONFIG', 'UPDATE_CONFIG',
'SET_MODAL_OPEN', 'SET_MODAL_OPEN',
'SET_LANGUAGE',
]; ];
// Convert array of key names into an object, and export // Convert array of key names into an object, and export