Skip to content

Commit 84b9cea

Browse files
committed
feat: render thumbnails based current language
1 parent ad98478 commit 84b9cea

File tree

5 files changed

+27
-13
lines changed

5 files changed

+27
-13
lines changed

apps/blog/src/components/HeroSection.astro

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
import type { Post } from '../lib/posts'
3-
import { formatDate, getPostSlug, getReadingTime } from '../lib/posts'
3+
import { formatDate, getPostHref, getReadingTime } from '../lib/posts'
44
import { useTranslations } from '../i18n/utils'
55
66
interface Props {
@@ -17,7 +17,7 @@ const [first, second, third] = posts
1717
<section class="mb-12">
1818
{posts.length === 1 ? (
1919
<article class="group rounded-xl border bg-card overflow-hidden">
20-
<a href={`/${getPostSlug(first)}`} class="block">
20+
<a href={{getPostHref(first)}} class="block">
2121
{first.data.cover && (
2222
<div class="overflow-hidden">
2323
<img
@@ -49,7 +49,7 @@ const [first, second, third] = posts
4949
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
5050
{[first, second].map((post) => (
5151
<article class="group rounded-xl border bg-card overflow-hidden">
52-
<a href={`/${getPostSlug(post)}`} class="block">
52+
<a href={{getPostHref(post)}} class="block">
5353
{post.data.cover && (
5454
<div class="overflow-hidden">
5555
<img
@@ -82,7 +82,7 @@ const [first, second, third] = posts
8282
) : (
8383
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
8484
<article class="group rounded-xl border bg-card overflow-hidden lg:row-span-2">
85-
<a href={`/${getPostSlug(first)}`} class="block h-full">
85+
<a href={{getPostHref(first)}} class="block h-full">
8686
{first.data.cover && (
8787
<div class="overflow-hidden">
8888
<img
@@ -112,7 +112,7 @@ const [first, second, third] = posts
112112
</article>
113113
{[second, third].filter(Boolean).map((post) => (
114114
<article class="group rounded-xl border bg-card overflow-hidden">
115-
<a href={`/${getPostSlug(post)}`} class="block p-6">
115+
<a href={{getPostHref(post)}} class="block p-6">
116116
{post.data.tags.length > 0 && (
117117
<div class="flex flex-wrap gap-2 mb-3">
118118
{post.data.tags.map((tag) => (

apps/blog/src/pages/[locale]/[slug].astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const localeSwitchUrls = buildLocaleSwitchUrls(allPosts, post, locales)
3232
date={post.data.date}
3333
tags={post.data.tags}
3434
cover={post.data.cover}
35-
thumbnail={`/${getPostSlug(post)}/thumbnail.png`}
35+
thumbnail={`${getPostHref(post)}/thumbnail.png`}
3636
readingTime={readingTime}
3737
headings={headings}
3838
url={articleUrl}

apps/blog/src/pages/[locale]/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const t = useTranslations(locale)
4848
description={post.data.description}
4949
date={post.data.date}
5050
tags={post.data.tags}
51-
cover={post.data.cover ?? `/${getPostSlug(post)}/thumbnail.png`}
51+
cover={post.data.cover ?? `${getPostHref(post)}/thumbnail.png`}
5252
href={getPostHref(post)}
5353
locale={locale}
5454
readingTime={getReadingTime(post.body ?? '')}

apps/website/astro.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export default defineConfig({
1616
title: 'Explainer v2',
1717
description: 'Documentation boilerplate for developers.',
1818
},
19+
{
20+
path: '/thumbnails/fr',
21+
title: 'Explainer v2',
22+
description: 'Boilerplate de documentation pour les développeurs.',
23+
},
1924
],
2025
},
2126
}),

apps/website/src/pages/index.astro

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const appUrlOverrides = {
1414
}
1515
1616
const translations = ui.fr
17+
const thumbnails = { en: '/thumbnail.png', fr: '/thumbnails/fr/thumbnail.png' }
1718
---
1819

1920
<Base title="Explainer" description="Documentation boilerplate for developers." thumbnail="/thumbnail.png">
@@ -32,30 +33,38 @@ const translations = ui.fr
3233
</footer>
3334
</Base>
3435

35-
<script is:inline define:vars={{ translations }}>
36+
<script is:inline define:vars={{ translations, thumbnails }}>
3637
function detectLocale() {
37-
var match = document.cookie.match(/(?:^|; )locale=([^;]*)/)
38-
var cookie = match ? match[1] : null
38+
const match = document.cookie.match(/(?:^|; )locale=([^;]*)/)
39+
const cookie = match ? match[1] : null
3940
if (cookie === 'en' || cookie === 'fr') return cookie
40-
var browser = (navigator.language || '').split('-')[0]
41+
42+
const browser = (navigator.language || '').split('-')[0]
4143
if (browser === 'en' || browser === 'fr') return browser
4244
return 'en'
4345
}
4446

4547
function applyTranslations() {
46-
var locale = detectLocale()
48+
const locale = detectLocale()
4749
document.documentElement.lang = locale
50+
4851
if (locale === 'en') {
4952
document.querySelectorAll('[data-t]').forEach(function (el) {
5053
if (el.dataset.tDefault) el.textContent = el.dataset.tDefault
5154
})
5255
return
5356
}
57+
5458
document.querySelectorAll('[data-t]').forEach(function (el) {
55-
var key = el.getAttribute('data-t')
59+
const key = el.getAttribute('data-t')
5660
if (!el.dataset.tDefault) el.dataset.tDefault = el.textContent
5761
if (translations[key]) el.textContent = translations[key]
5862
})
63+
64+
const thumb = thumbnails[locale] || thumbnails['en']
65+
document.querySelectorAll('meta[property="og:image"], meta[name="twitter:image"]').forEach(function (el) {
66+
el.setAttribute('content', thumb)
67+
})
5968
}
6069

6170
applyTranslations()

0 commit comments

Comments
 (0)