feat:#60 added localization switch

This commit is contained in:
masterelmar
2023-11-15 12:46:56 +01:00
parent 3f1a592468
commit 00399c32d9
11 changed files with 204 additions and 98 deletions

View File

@@ -1,11 +1,14 @@
<script lang="ts" setup>
import { Ref, ref } from "vue";
import { computed, ComputedRef, Ref, ref } from "vue";
import {
fetchCourse,
fetchModulesByCourseAndSemester,
} from "../api/fetchCourse";
import ModuleSelection from "./ModuleSelection.vue";
import { Module } from "../model/module.ts";
import { useI18n } from "vue-i18n";
const { t } = useI18n({ useScope: 'global' })
const courses = async () => {
return await fetchCourse();
@@ -13,9 +16,9 @@ const courses = async () => {
const selectedCourse: Ref<{ name: string }> = ref({ name: "" });
const countries: Ref<{ name: string }[]> = ref([]);
const semesters: Ref<{ name: string; value: string }[]> = ref([
{ name: "Wintersemester", value: "ws" },
{ name: "Sommersemester", value: "ss" },
const semesters: ComputedRef<{ name: string; value: string }[]> = computed(() =>[
{ name: t('courseSelection.winterSemester'), value: "ws" },
{ name: t('courseSelection.summerSemester'), value: "ss" },
]);
const selectedSemester: Ref<{ name: string; value: string }> = ref(
@@ -53,7 +56,7 @@ async function getModules() {
<div
class="flex align-items-center justify-content-center h-4rem border-round m-2"
>
<h5 class="text-2xl">Please select a course</h5>
<h5 class="text-2xl">{{ $t('courseSelection.selectCourse') }}</h5>
</div>
<div
class="flex align-items-center justify-content-center border-round m-2"

View File

@@ -1,39 +1,49 @@
<script lang="ts" setup>
import i18n, { supportedLocales } from "../i18n";
import { Ref, ref } from "vue";
import router from "../router";
import { computed } from "vue";
import localeStore from "../store/localeStore.ts";
import { useI18n } from "vue-i18n";
const { t } = useI18n({ useScope: 'global' })
const locales = ref(
Object.keys(supportedLocales).map((code) => ({
code,
name: supportedLocales[code].name,
})),
);
const countries = computed(() => [
{ name: t('english'), code: "en", icon: "🇬🇧" },
{ name: t('german'), code: "de", icon: "🇩🇪" },
]);
// selectedLocal is the string of the selected locale from i18n matched with the locales array
const selectedLocale: Ref<any> = ref();
function displayIcon(code: string) {
return countries.value.find((country) => country.code === code)?.icon;
}
const i18n1 = (i18n.vueI18n);
function displayCountry(code: string) {
return countries.value.find((country) => country.code === code)?.name;
}
function onLocaleChange() {
const newLocale: string = selectedLocale.value.code;
// If the selected locale is the same as the
// active one, do nothing
if (newLocale === i18n.vueI18n.global.locale) {
return;
}
i18n1.global.locale = newLocale;
router.push(`/${newLocale}`);
function updateLocale(locale: string) {
localeStore().setLocale(locale);
}
</script>
<template>
<Dropdown
:options="locales"
optionLabel="name"
v-model="selectedLocale"
@change="onLocaleChange"
:options="$i18n.availableLocales"
v-model="$i18n.locale"
@change="updateLocale($event.data)"
option-label="name"
placeholder="Select a Language"
class="w-full md:w-14rem"
>
<template #value="slotProps">
<div v-if="slotProps.value" class="flex align-items-center">
<div class="mr-2 flag">{{ displayIcon(slotProps.value) }}</div>
<div>{{ displayCountry(slotProps.value) }}</div>
</div>
<span v-else>
{{ slotProps.placeholder }}
</span>
</template>
<template #option="slotProps">
<div class="flex align-items-center">
<div class="mr-2 flag">{{ displayIcon(slotProps.option) }}</div>
<div>{{ displayCountry(slotProps.option) }}</div>
</div>
</template>
</Dropdown>
</template>
</template>

View File

@@ -1,58 +1,50 @@
<script lang="ts" setup>
import { ref } from "vue";
import { computed } from "vue";
import { useI18n } from "vue-i18n";
import LocaleSwitcher from "./LocaleSwitcher.vue";
import i18n from "../i18n";
const { t } = useI18n({ useScope: 'global' })
const { t, locale } = useI18n();
console.debug("locale", locale);
console.debug(useI18n().locale)
const items = ref([
const items = computed(() => [
{
label: t("createCalendar"),
icon: "pi pi-fw pi-plus",
url: `/${locale}`,
to: "/",
},
{
label: t("editCalendar"),
icon: "pi pi-fw pi-pencil",
url: `/${locale}/edit`,
to: "/edit",
},
{
label: t("roomFinder"),
icon: "pi pi-fw pi-calendar",
url: `/${locale}/rooms`,
to: `rooms`,
},
{
label: t("faq"),
icon: "pi pi-fw pi-book",
url: `/${i18n.vueI18n.global.locale}/faq`,
to: `faq`,
},
{
label: t("imprint"),
icon: "pi pi-fw pi-id-card",
url: `/${i18n.vueI18n.global.locale}/imprint`,
to: `imprint`,
},
{
label: t("privacy"),
icon: "pi pi-fw pi-exclamation-triangle",
url: `/${i18n.vueI18n.global.locale}/privacy-policy`,
to: `privacy-policy`,
},
]);
function removeAllItems() {
items.value = [];
}
</script>
<template>
<Menubar :model="items" class="menubar justify-content-center">
<template #start></template>
<template #start>
</template>
<template #end>
<LocaleSwitcher></LocaleSwitcher>
<Button @click="removeAllItems()"></Button>
</template>
</Menubar>
</template>