Skip to content

Commit

Permalink
Add method for language switch that changes the url.
Browse files Browse the repository at this point in the history
  • Loading branch information
Elweyn committed Mar 26, 2024
1 parent a80a6ed commit f95b846
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions presenter/src/components/language/LanguageSelector.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<v-select
v-model="$i18n.locale"
v-model="selectedLocale"
density="compact"
name="language"
:items="languages"
Expand All @@ -12,6 +12,7 @@
bg-color="transparent"
hide-details="auto"
flat
@update:model-value="updateLanguage"
>
<template #selection="{ item }">
<span>{{ item.raw.locale.toUpperCase() }}</span>
Expand All @@ -29,9 +30,39 @@
<script lang="ts" setup>
import { ref } from 'vue'
import { localizedLocale } from '#src/locales'
import { usePageContext } from '#context/usePageContext'
import i18n from '#plugins/i18n'
import { LocaleCode, locales, localizedLocale } from '#src/locales'
const pageContext = usePageContext()
const languages = ref(localizedLocale)
const selectedLocale = ref(i18n.global.locale.value)
const updateLanguage = () => {
if (locales.includes(selectedLocale.value)) {
i18n.global.locale.value = selectedLocale.value
}
let { urlOriginal } = pageContext
const urlPaths = urlOriginal.split('/')

Check failure on line 48 in presenter/src/components/language/LanguageSelector.vue

View workflow job for this annotation

GitHub Actions / Unit - Presenter

src/components/language/LanguageSelector.test.ts > LanguageSelector > switch locale > to en

TypeError: Cannot read properties of undefined (reading 'split') ❯ updateLanguage src/components/language/LanguageSelector.vue:48:32 ❯ callWithErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:195:19 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:202:17 ❯ callWithAsyncErrorHandling node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:212:17 ❯ emit node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:713:5 ❯ VueWrapper.setValue node_modules/@vue/test-utils/dist/vue-test-utils.cjs.js:7787:17 ❯ src/components/language/LanguageSelector.test.ts:28:21
let urlWithoutLocale = ''
let leadingSlash = '/'
if (urlPaths[1].indexOf('#') > -1) {
const hashUrl = urlPaths[1].split('#')
urlWithoutLocale = '#' + hashUrl[1]
leadingSlash = ''
} else if (!locales.includes(urlPaths[1] as LocaleCode)) {
urlWithoutLocale = leadingSlash + urlPaths[1]
}
if (locales.includes(selectedLocale.value)) {
urlWithoutLocale += leadingSlash + urlPaths.slice(2).join('/')
urlOriginal = '/' + selectedLocale.value + urlWithoutLocale
}
window.location.href = urlOriginal
}
</script>

<style lang="scss">
Expand Down

0 comments on commit f95b846

Please sign in to comment.