Files
blog/frontend/app/pages/category/[slug].vue
T
davide c08f53ca69 Add a language switcher with English, Italian, Spanish and French
The interface is translated through @nuxtjs/i18n: strings live in
i18n/locales/*.json, routes are prefixed except for the default English, and
useLocaleHead emits the lang attribute, the hreflang alternates and og:locale.

The switcher is a native select rather than a custom dropdown: keyboard
support, the platform picker on mobile and correct labelling come for free.

Article content stays single-language — this translates the site chrome only.
2026-08-25 14:23:09 +02:00

73 lines
2.1 KiB
Vue

<script setup lang="ts">
import type { ArticleSummary, Paginated, Category } from '#shared/types/blog'
const { t } = useI18n()
const route = useRoute()
const slug = route.params.slug as string
const page = computed(() => {
const value = Number(route.query.page)
return Number.isInteger(value) && value > 0 ? value : 1
})
const { data: category } = await useFetch<Category>(`/api/categories/${slug}`)
if (!category.value) {
throw createError({ statusCode: 404, statusMessage: 'Category not found', fatal: true })
}
const { data, error } = await useFetch<Paginated<ArticleSummary>>('/api/articles', {
query: { page, category: slug },
})
const { public: config } = useRuntimeConfig()
const canonical = computed(
() => `${config.siteUrl}${route.path}${page.value > 1 ? `?page=${page.value}` : ''}`
)
useSeoMeta({
title: category.value.name,
description: () => t('list.categoryDescription', { name: category.value?.name ?? '' }),
ogType: 'website',
})
useHead({ link: [{ rel: 'canonical', href: canonical }] })
</script>
<template>
<div>
<header class="page-header">
<p class="kicker">{{ $t('list.category') }}</p>
<h1>{{ category?.name }}</h1>
</header>
<p v-if="error">{{ $t('list.failed') }}</p>
<p v-else-if="!data?.items.length">{{ $t('list.categoryEmpty') }}</p>
<template v-else>
<ul class="card-grid">
<li v-for="article in data.items" :key="article.slug">
<ArticleCard :article="article" />
</li>
</ul>
<nav v-if="data.pageCount > 1" class="pagination" :aria-label="$t('pagination.label')">
<NuxtLink v-if="data.page > 1" :to="{ query: { page: data.page - 1 } }" rel="prev">
{{ $t('pagination.previous') }}
</NuxtLink>
<span class="muted">
{{ $t('pagination.position', { page: data.page, total: data.pageCount }) }}
</span>
<NuxtLink
v-if="data.page < data.pageCount"
:to="{ query: { page: data.page + 1 } }"
rel="next"
>
{{ $t('pagination.next') }}
</NuxtLink>
</nav>
</template>
</div>
</template>