Building a Multilingual Blog with Astro

10727

Building a Multilingual Blog with Astro

Supporting multiple languages in a blog opens it up to a global audience. Here's how to build one using Astro with bilingual content stored in Cloudflare D1.

Content Strategy

Rather than separate routes per language, we store both languages in the same article record:

CREATE TABLE articles (
  id TEXT PRIMARY KEY,
  slug TEXT NOT NULL,
  title_en TEXT NOT NULL,
  content_en TEXT NOT NULL,
  title_id TEXT NOT NULL,
  content_id TEXT NOT NULL,
  ...
);

Language Switching

A simple toggle switches between language versions client-side:

---
const { lang } = Astro.params;
const article = await getArticle(slug);
const title = lang === 'id' ? article.titleId : article.titleEn;
const content = lang === 'id' ? article.contentId : article.contentEn;
---

<h1>{title}</h1>
<div set:html={renderMarkdown(content)} />

<a href={lang === 'en' ? `/id/${slug}` : `/en/${slug}`}>
  {lang === 'en' ? 'Bahasa Indonesia' : 'English'}
</a>

SEO Considerations

  • Use hreflang tags for proper SEO
  • Generate sitemaps with all language variants
  • Set appropriate lang attribute on <html>

Benefits of This Approach

  1. Single source of truth — both languages stay in sync
  2. Easy maintenance — edit one record, both languages update
  3. Atomic updates — publish both languages simultaneously

Comments

commenter 11mo ago

hey ini komentar

Leave a comment

Your name and email are saved locally in your browser so you don't have to re-enter them.