🚸 🐛 Fix HMR issues & change lang without reload

- Fix HMR issues due to circular import of "language" and "i18n"
- Move i18n to seperate file
- Dynamically update language instead of doing a full page reload
This commit is contained in:
2023-06-05 19:11:15 +02:00
parent d1910a7877
commit 7928e4941f
4 changed files with 26 additions and 18 deletions

17
src/i18n.js Normal file
View File

@ -0,0 +1,17 @@
import { createI18n } from "vue-i18n";
import { strings } from "./strings";
import { ref, watch } from "vue";
export const language = ref(localStorage.getItem("lang") || "en");
let i18n = createI18n({
locale: language.value,
fallbackLocale: "en",
messages: strings,
});
export default i18n;
watch(language, (newValue) => {
localStorage.setItem("lang", newValue);
i18n.global.locale = newValue;
});

View File

@ -2,16 +2,7 @@ import { createApp } from "vue";
import App from "./App.vue"; import App from "./App.vue";
import router from "./router"; import router from "./router";
import { registerSW } from "virtual:pwa-register"; import { registerSW } from "virtual:pwa-register";
import { createI18n } from "vue-i18n"; import i18n from "./i18n";
import { strings } from "./strings";
import { language } from "./store";
const i18n = createI18n({
locale: language.value,
fallbackLocale: "en",
messages: strings,
});
export default i18n;
const app = createApp(App); const app = createApp(App);

View File

@ -1,7 +1,7 @@
import { ref, watch, computed } from "vue"; import { ref, watch, computed } from "vue";
import router from "./router"; import router from "./router";
import i18n from "./main";
import { getNextAndPrevDay } from "./util"; import { getNextAndPrevDay } from "./util";
import i18n from "./i18n";
/* Router */ /* Router */
export const lastRoute = ref(); export const lastRoute = ref();
@ -14,7 +14,7 @@ export const classFilter = ref(localStorage.getItem("classFilter") || "none");
export const timetableGroups = ref( export const timetableGroups = ref(
JSON.parse(localStorage.getItem("timetableGroups") || "[]") JSON.parse(localStorage.getItem("timetableGroups") || "[]")
); );
export const language = ref(localStorage.getItem("lang") || "en");
export const theme = ref(localStorage.getItem("theme") || "auto"); export const theme = ref(localStorage.getItem("theme") || "auto");
watch(classFilter, (newValue) => { watch(classFilter, (newValue) => {
@ -24,10 +24,6 @@ watch(classFilter, (newValue) => {
watch(timetableGroups, (newValue) => { watch(timetableGroups, (newValue) => {
localStorage.setItem("timetableGroups", JSON.stringify(newValue)); localStorage.setItem("timetableGroups", JSON.stringify(newValue));
}); });
watch(language, (newValue) => {
localStorage.setItem("lang", newValue);
location.reload();
});
watch(theme, (newValue) => { watch(theme, (newValue) => {
localStorage.setItem("theme", newValue); localStorage.setItem("theme", newValue);
}); });

View File

@ -5,9 +5,9 @@ import {
classFilter, classFilter,
possibleTimetableGroups, possibleTimetableGroups,
timetableGroups, timetableGroups,
language,
theme, theme,
} from "../store"; } from "../store";
import { language } from "../i18n";
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
const gitHash = GITVERSION; const gitHash = GITVERSION;
@ -20,7 +20,11 @@ const gitHash = GITVERSION;
<p>{{ $t("settings.text.filtering") }}</p> <p>{{ $t("settings.text.filtering") }}</p>
<select v-model="classFilter"> <select v-model="classFilter">
<option value="none">{{ $t("timetable.setup.prompt") }}</option> <option value="none">{{ $t("timetable.setup.prompt") }}</option>
<option v-for="option in classList" :value="option" :key="option"> <option
v-for="option in classList.length > 0 ? classList : [classFilter]"
:value="option"
:key="option"
>
{{ option }} {{ option }}
</option> </option>
<option value="other">{{ $t("settings.other") }}</option> <option value="other">{{ $t("settings.other") }}</option>