Files
CV_Site_Nuxt/app/components/AppLanguageSwitcher.vue
T
Noel Sabia 93ab0c0d79
PR Check / Security scan (Snyk) (pull_request) Skipped
PR Check / Lint + typecheck (pull_request) Successful in 58s
PR Check / Secret scan (gitleaks) (pull_request) Successful in 6s
PR Check / Dependency audit (pull_request) Successful in 9s
PR Check / Dockerfile lint (droast) (pull_request) Successful in 6s
PR Check / AI code review (opencode) (pull_request) Successful in 42s
feat(i18n): add five locales and a header language switcher
Locale files in i18n/locales for en, de, es, it and pl; English stays at / and the others are prefixed. One flag dropdown after the GitHub link on desktop, logo plus the language button on phones. Nav, quick links and footer strings go through $t, and the footer line drops "self-healing on purpose".
2026-10-23 22:00:34 +02:00

76 lines
2.4 KiB
Vue

<script setup lang="ts">
const { locales, locale, setLocale } = useI18n()
const open = ref(false)
const root = ref<HTMLElement | null>(null)
function closeOnOutside(event: MouseEvent) {
if (!root.value?.contains(event.target as Node)) {
open.value = false
}
}
function closeOnEscape(event: KeyboardEvent) {
if (event.key === 'Escape') {
open.value = false
}
}
onMounted(() => {
document.addEventListener('click', closeOnOutside)
document.addEventListener('keydown', closeOnEscape)
})
onBeforeUnmount(() => {
document.removeEventListener('click', closeOnOutside)
document.removeEventListener('keydown', closeOnEscape)
})
async function choose(code: Parameters<typeof setLocale>[0]) {
open.value = false
await setLocale(code)
}
</script>
<template>
<div ref="root" class="relative flex shrink-0 items-center font-label-mono text-label-mono">
<button
type="button"
class="flex items-center gap-1.5 uppercase tracking-wider text-on-surface-variant transition-colors hover:text-on-surface"
aria-haspopup="true"
:aria-expanded="open"
:aria-label="$t('switcher.label')"
@click="open = !open"
>
<span class="h-3.5 w-5 overflow-hidden rounded-[2px]">
<AppFlag :code="locale" />
</span>
<span>{{ locale }}</span>
<span aria-hidden="true" class="text-[0.625rem] text-on-surface-variant">▼</span>
</button>
<ul
v-if="open"
class="absolute right-0 top-full z-10 mt-2 min-w-48 rounded-lg border border-outline-variant/30 bg-surface-container-high p-1 shadow-lg"
>
<li v-for="language in locales" :key="language.code">
<button
type="button"
class="flex w-full items-center gap-3 rounded px-3 py-2 text-left transition-colors"
:class="locale === language.code
? 'bg-surface-variant text-on-surface'
: 'text-on-surface-variant hover:bg-surface-variant hover:text-on-surface'"
:aria-current="locale === language.code ? 'true' : undefined"
@click="choose(language.code)"
>
<span class="h-3.5 w-5 shrink-0 overflow-hidden rounded-[2px]">
<AppFlag :code="language.code" />
</span>
<span class="flex-1">{{ language.name }}</span>
<span class="uppercase tracking-wider">{{ language.code }}</span>
</button>
</li>
</ul>
</div>
</template>