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.
This commit is contained in:
davide committed 2026-08-25 14:23:09 +02:00
1 parent f5639111f9
commit c08f53ca69
21 files changed
+3197 -87

No files matched your search

+8 -7
View File
@@ -1,6 +1,7 @@
<script setup lang="ts">
import type { Article } from '#shared/types/blog'
const { locale } = useI18n()
const route = useRoute()
const slug = route.params.slug as string
@@ -13,7 +14,7 @@ if (!article.value) {
const { public: config } = useRuntimeConfig()
const mediaUrl = useMediaUrl()
const canonical = `${config.siteUrl}/blog/${article.value.slug}`
const canonical = `${config.siteUrl}${route.path}`
const cover = computed(() => mediaUrl(article.value?.cover) ?? undefined)
useSeoMeta({
@@ -49,13 +50,13 @@ useHead({
<template>
<article v-if="article">
<header class="article-header">
<nav class="kicker" aria-label="Breadcrumb">
<NuxtLink to="/blog">Articles</NuxtLink>
<nav class="kicker" :aria-label="$t('article.breadcrumb')">
<NuxtLinkLocale to="/blog">{{ $t('list.articles') }}</NuxtLinkLocale>
<template v-if="article.category">
<span aria-hidden="true"> / </span>
<NuxtLink :to="`/category/${article.category.slug}`">
<NuxtLinkLocale :to="`/category/${article.category.slug}`">
{{ article.category.name }}
</NuxtLink>
</NuxtLinkLocale>
</template>
</nav>
@@ -63,7 +64,7 @@ useHead({
<p class="kicker date">
<time :datetime="isoDate(article.publishedAt)">
{{ formatDate(article.publishedAt) }}
{{ formatDate(article.publishedAt, locale) }}
</time>
</p>
</header>
@@ -81,7 +82,7 @@ useHead({
<div class="prose" v-html="article.html" />
<p class="back">
<NuxtLink to="/blog">Back to all articles</NuxtLink>
<NuxtLinkLocale to="/blog">{{ $t('article.back') }}</NuxtLinkLocale>
</p>
</article>
</template>
+16 -13
View File
@@ -1,7 +1,9 @@
<script setup lang="ts">
import type { ArticleSummary, Paginated } from '#shared/types/blog'
const { t } = useI18n()
const route = useRoute()
const page = computed(() => {
const value = Number(route.query.page)
return Number.isInteger(value) && value > 0 ? value : 1
@@ -13,12 +15,12 @@ const { data, error, status } = await useFetch<Paginated<ArticleSummary>>('/api/
const { public: config } = useRuntimeConfig()
const canonical = computed(
() => `${config.siteUrl}/blog${page.value > 1 ? `?page=${page.value}` : ''}`
() => `${config.siteUrl}${route.path}${page.value > 1 ? `?page=${page.value}` : ''}`
)
useSeoMeta({
title: page.value > 1 ? `Articles — page ${page.value}` : 'Articles',
description: 'Every article published on the blog.',
title: () => (page.value > 1 ? t('list.articlesPage', { page: page.value }) : t('list.articles')),
description: () => t('list.archiveDescription'),
ogType: 'website',
})
@@ -28,15 +30,15 @@ useHead({ link: [{ rel: 'canonical', href: canonical }] })
<template>
<div>
<header class="page-header">
<p class="kicker">The archive</p>
<h1>Articles</h1>
<p class="kicker">{{ $t('list.archive') }}</p>
<h1>{{ $t('list.articles') }}</h1>
</header>
<p v-if="error">Articles could not be loaded. Please try again later.</p>
<p v-if="error">{{ $t('list.failed') }}</p>
<p v-else-if="status === 'pending'">Loading…</p>
<p v-else-if="status === 'pending'">{{ $t('list.loading') }}</p>
<p v-else-if="!data?.items.length">Nothing has been published yet.</p>
<p v-else-if="!data?.items.length">{{ $t('list.empty') }}</p>
<template v-else>
<ul class="card-grid">
@@ -45,20 +47,21 @@ useHead({ link: [{ rel: 'canonical', href: canonical }] })
</li>
</ul>
<nav v-if="data.pageCount > 1" class="pagination" aria-label="Pagination">
<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">
Previous page
{{ $t('pagination.previous') }}
</NuxtLink>
<span class="muted">Page {{ data.page }} of {{ data.pageCount }}</span>
<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"
>
Next page
{{ $t('pagination.next') }}
</NuxtLink>
</nav>
</template>
</div>
</template>
+12 -10
View File
@@ -1,6 +1,7 @@
<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
@@ -21,12 +22,12 @@ const { data, error } = await useFetch<Paginated<ArticleSummary>>('/api/articles
const { public: config } = useRuntimeConfig()
const canonical = computed(
() => `${config.siteUrl}/category/${slug}${page.value > 1 ? `?page=${page.value}` : ''}`
() => `${config.siteUrl}${route.path}${page.value > 1 ? `?page=${page.value}` : ''}`
)
useSeoMeta({
title: category.value.name,
description: `Every article in the ${category.value.name} category.`,
description: () => t('list.categoryDescription', { name: category.value?.name ?? '' }),
ogType: 'website',
})
@@ -36,13 +37,13 @@ useHead({ link: [{ rel: 'canonical', href: canonical }] })
<template>
<div>
<header class="page-header">
<p class="kicker">Category</p>
<p class="kicker">{{ $t('list.category') }}</p>
<h1>{{ category?.name }}</h1>
</header>
<p v-if="error">Articles could not be loaded. Please try again later.</p>
<p v-if="error">{{ $t('list.failed') }}</p>
<p v-else-if="!data?.items.length">There are no articles in this category.</p>
<p v-else-if="!data?.items.length">{{ $t('list.categoryEmpty') }}</p>
<template v-else>
<ul class="card-grid">
@@ -51,20 +52,21 @@ useHead({ link: [{ rel: 'canonical', href: canonical }] })
</li>
</ul>
<nav v-if="data.pageCount > 1" class="pagination" aria-label="Pagination">
<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">
Previous page
{{ $t('pagination.previous') }}
</NuxtLink>
<span class="muted">Page {{ data.page }} of {{ data.pageCount }}</span>
<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"
>
Next page
{{ $t('pagination.next') }}
</NuxtLink>
</nav>
</template>
</div>
</template>
+25 -14
View File
@@ -1,6 +1,8 @@
<script setup lang="ts">
import type { ArticleSummary, Paginated } from '#shared/types/blog'
const { t, locale } = useI18n()
const { data, error } = await useFetch<Paginated<ArticleSummary>>('/api/articles', {
key: 'home-articles',
query: { page: 1 },
@@ -14,30 +16,37 @@ const mediaUrl = useMediaUrl()
const featuredCover = computed(() => mediaUrl(featured.value?.cover))
const { public: config } = useRuntimeConfig()
const route = useRoute()
const canonical = computed(() => `${config.siteUrl}${route.path}`)
// The home page is the site itself, so it carries the bare name as its title.
useHead({ titleTemplate: '%s', link: [{ rel: 'canonical', href: config.siteUrl }] })
useHead({ titleTemplate: '%s', link: [{ rel: 'canonical', href: canonical }] })
useSeoMeta({
title: SITE_NAME,
description: SITE_DESCRIPTION,
description: () => t('site.tagline'),
ogTitle: SITE_NAME,
ogDescription: SITE_DESCRIPTION,
ogDescription: () => t('site.tagline'),
ogType: 'website',
ogUrl: config.siteUrl,
ogUrl: canonical,
ogImage: `${config.siteUrl}/logo.png`,
})
</script>
<template>
<div>
<p v-if="error">Articles could not be loaded. Please try again later.</p>
<p v-if="error">{{ $t('list.failed') }}</p>
<p v-else-if="!featured">Nothing has been published yet.</p>
<p v-else-if="!featured">{{ $t('list.empty') }}</p>
<template v-else>
<article class="featured">
<NuxtLink class="featured-cover" :to="`/blog/${featured.slug}`" tabindex="-1" aria-hidden="true">
<NuxtLinkLocale
class="featured-cover"
:to="`/blog/${featured.slug}`"
tabindex="-1"
aria-hidden="true"
>
<img
v-if="featuredCover"
:src="featuredCover"
@@ -46,28 +55,30 @@ useSeoMeta({
:height="featured.cover?.height ?? undefined"
>
<span v-else class="placeholder" />
</NuxtLink>
</NuxtLinkLocale>
<div class="featured-text">
<p class="kicker">
{{ featured.category ? featured.category.name : 'Latest' }}
{{ featured.category ? featured.category.name : $t('home.latest') }}
</p>
<h1>
<NuxtLink :to="`/blog/${featured.slug}`">{{ featured.title }}</NuxtLink>
<NuxtLinkLocale :to="`/blog/${featured.slug}`">{{ featured.title }}</NuxtLinkLocale>
</h1>
<p class="kicker date">
<time :datetime="isoDate(featured.publishedAt)">
{{ formatDate(featured.publishedAt) }}
{{ formatDate(featured.publishedAt, locale) }}
</time>
</p>
<p class="read-more">
<NuxtLink :to="`/blog/${featured.slug}`">Read the article</NuxtLink>
<NuxtLinkLocale :to="`/blog/${featured.slug}`">
{{ $t('home.readArticle') }}
</NuxtLinkLocale>
</p>
</div>
</article>
<template v-if="rest.length">
<h2 class="section-heading kicker">More stories</h2>
<h2 class="section-heading kicker">{{ $t('home.moreStories') }}</h2>
<ul class="card-grid">
<li v-for="article in rest" :key="article.slug">
@@ -77,7 +88,7 @@ useSeoMeta({
</template>
<p class="more">
<NuxtLink to="/blog">View all articles</NuxtLink>
<NuxtLinkLocale to="/blog">{{ $t('home.viewAll') }}</NuxtLinkLocale>
</p>
</template>
</div>