Skip to content
Snippets Groups Projects
LanguageButton.vue 1.48 KiB
Newer Older
  • Learn to ignore specific revisions
  • Cee Nell's avatar
    Cee Nell committed
    <template>
    
    Cee Nell's avatar
    Cee Nell committed
        <div class="dropdown-container">
            <label for="language-select"><i>{{ $t('text.readIn') }}</i></label>
            <select id="language-select" v-model="selectedLanguage" @change="handleLanguageChange" >
            <option value="en">English</option>
            <option value="es">Español</option>
            </select>
    
    Cee Nell's avatar
    Cee Nell committed
        </div>
    
    Cee Nell's avatar
    Cee Nell committed
    </template>
    
    Cee Nell's avatar
    Cee Nell committed
    <script setup>
    import { ref } from 'vue';
    import { useI18n } from 'vue-i18n';
    import { useRouter, useRoute } from 'vue-router';
    
    const { locale } = useI18n();
    const router = useRouter();
    const route = useRoute();
    
    // Create a reactive variable to track the selected language
    const selectedLanguage = ref(locale.value);
    
    function handleLanguageChange(event) {
        switchLanguage(selectedLanguage.value);
    }
    
    function switchLanguage(lang) {
      if (locale.value !== lang) {
    
    Cee Nell's avatar
    Cee Nell committed
        locale.value = lang;
    
    Cee Nell's avatar
    Cee Nell committed
    
    
    Cee Nell's avatar
    Cee Nell committed
        // Remove any existing language prefix from the current path
        const currentPath = route.path.replace(/^\/(en|es)/, '');
    
        // Construct the new path with the selected language
    
    Cee Nell's avatar
    Cee Nell committed
        router.push({
    
    Cee Nell's avatar
    Cee Nell committed
          path: `/${lang}${currentPath || '/'}`
    
    Cee Nell's avatar
    Cee Nell committed
        });
      }
    
    Cee Nell's avatar
    Cee Nell committed
    }
    </script>
    
    
    Cee Nell's avatar
    Cee Nell committed
    
    <style scoped lang="scss">
    
    Cee Nell's avatar
    Cee Nell committed
    .dropdown-container {
      display: flex;
      justify-content: flex-end; /* Align to the right */
      align-items: center;
      padding: 0px;
    
    Cee Nell's avatar
    Cee Nell committed
    }
    
    Cee Nell's avatar
    Cee Nell committed
    
    select {
      margin-left: 10px;
      padding: 5px;
      border-radius: 5px;
      border: 1px solid #ccc;
      font-weight: bold;
      cursor: pointer;
      transition: all 0.2s;
    
    Cee Nell's avatar
    Cee Nell committed
    select:focus {
      outline: none;
      border-color: #007bff;
    
    Cee Nell's avatar
    Cee Nell committed
    }
    
    Cee Nell's avatar
    Cee Nell committed
    
    
    Cee Nell's avatar
    Cee Nell committed
    </style>