All files / components/composite LanguageSwitcher.vue

100% Statements 104/104
100% Branches 0/0
100% Functions 0/0
100% Lines 104/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155  1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x         1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                   1x         1x         1x 1x 1x         1x 1x 1x         1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x    
<template>
  <div class="relative">
    <!-- Language Button -->
    <button
      @click.stop="isOpen = !isOpen"
      class="flex items-center gap-1.5 px-2 py-1.5 rounded-lg text-sm font-medium text-secondary-700 hover:bg-secondary-100 transition-colors"
      :aria-label="`Current language: ${currentLocale.name}`"
      aria-haspopup="true"
      :aria-expanded="isOpen"
    >
      <span class="text-base">{{ getFlag(currentLocale.code) }}</span>
      <span>{{ currentLocale.code.toUpperCase() }}</span>
      <svg
        class="w-3.5 h-3.5"
        :class="{ 'rotate-180': isOpen }"
        fill="none"
        stroke="currentColor"
        viewBox="0 0 24 24"
      >
        <path
          stroke-linecap="round"
          stroke-linejoin="round"
          stroke-width="2"
          d="M19 9l-7 7-7-7"
        />
      </svg>
    </button>
 
    <!-- Dropdown Menu -->
    <Transition
      enter-active-class="transition ease-out duration-100"
      enter-from-class="transform opacity-0 scale-95"
      enter-to-class="transform opacity-100 scale-100"
      leave-active-class="transition ease-in duration-75"
      leave-from-class="transform opacity-100 scale-100"
      leave-to-class="transform opacity-0 scale-95"
    >
      <div
        v-if="isOpen"
        v-click-outside="() => isOpen = false"
        class="absolute left-0 mt-2 w-44 rounded-lg shadow-lg bg-white ring-1 ring-black ring-opacity-5 z-50"
        role="menu"
        aria-orientation="vertical"
      >
        <div class="py-1">
          <button
            v-for="locale in availableLocales"
            :key="locale.code"
            @click="switchLocale(locale.code)"
            class="w-full text-left px-4 py-2 text-sm transition-colors"
            :class="[
              currentLocale.code === locale.code
                ? 'bg-primary-50 text-primary-700 font-medium'
                : 'text-secondary-700 hover:bg-secondary-100'
            ]"
            role="menuitem"
          >
            <div class="flex items-center justify-between">
              <div class="flex items-center gap-2">
                <span class="text-lg">{{ getFlag(locale.code) }}</span>
                <span>{{ locale.name }}</span>
              </div>
              <svg
                v-if="currentLocale.code === locale.code"
                class="w-5 h-5 text-primary-600"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  stroke-linecap="round"
                  stroke-linejoin="round"
                  stroke-width="2"
                  d="M5 13l4 4L19 7"
                />
              </svg>
            </div>
          </button>
        </div>
      </div>
    </Transition>
  </div>
</template>
 
<script setup lang="ts">
/**
 * LanguageSwitcher
 * @description Dropdown component for switching between available languages
 * Uses @nuxtjs/i18n for locale management
 */
 
const { locale, locales, setLocale } = useI18n()
 
/**
 * Dropdown open state
 */
const isOpen = ref(false)
 
/**
 * Available locales from i18n config
 */
const availableLocales = computed(() => {
  return locales.value as Array<{ code: string; name: string }>
})
 
/**
 * Current locale object
 */
const currentLocale = computed(() => {
  return availableLocales.value.find(l => l.code === locale.value) || { code: 'en', name: 'English' }
})
 
/**
 * Get flag emoji for locale code
 */
const getFlag = (code: string): string => {
  const flags: Record<string, string> = {
    en: '🇬🇧',
    de: '🇩🇪'
  }
  return flags[code] || '🌐'
}
 
/**
 * Switch to a different locale
 */
const switchLocale = async (code: string) => {
  await setLocale(code as 'en' | 'de')
  isOpen.value = false
}
 
/**
 * Click outside directive
 */
interface HTMLElementWithClickEvent extends HTMLElement {
  clickOutsideEvent?: (event: Event) => void
}
 
const vClickOutside = {
  mounted(el: HTMLElementWithClickEvent, binding: { value: () => void }) {
    el.clickOutsideEvent = (event: Event) => {
      if (!(el === event.target || el.contains(event.target as Node))) {
        binding.value()
      }
    }
    document.addEventListener('click', el.clickOutsideEvent)
  },
  unmounted(el: HTMLElementWithClickEvent) {
    if (el.clickOutsideEvent) {
      document.removeEventListener('click', el.clickOutsideEvent)
    }
  }
}
</script>