Skip to main content

Overview

Astro Portfolio v3 includes built-in internationalization (i18n) support, allowing the site to be available in multiple languages. The system uses Astro’s native i18n configuration combined with a custom translation utility.
The i18n configuration is defined in ~/workspace/source/astro.config.mjs:48-54 and translation utilities are in ~/workspace/source/src/utils/i18n.ts.

Supported Languages

The portfolio supports four languages:

English (en)

Default language - no URL prefix required

Español (es)

Spanish - accessible at /es/

Français (fr)

French - accessible at /fr/

Deutsch (de)

German - accessible at /de/

Configuration

Astro Config

The i18n setup in astro.config.mjs:
Key settings:
  • defaultLocale: 'en' - English is the default language
  • prefixDefaultLocale: false - English pages don’t have /en/ prefix
  • All other languages use a prefix (/es/, /fr/, /de/)

Translation Definitions

Translations are defined in src/utils/i18n.ts:

Translation System

Translation Keys

The ui object contains all translations organized by language:

Utility Functions

Getting Current Language

Extract the language from the URL:
Usage:

Using Translations

Create a translation function for a specific language:
Usage in components:

Language Switcher Component

The LanguageSwitcher component allows users to change languages:
Features:
  • Shows current language with highlighting
  • Preserves the current path when switching languages
  • Handles the default locale (no prefix for English)
Location: src/components/shared/LanguageSwitcher.astro:1

URL Structure

Default Language (English)

Other Languages

Adding New Translations

Step 1: Add Translation Keys

Edit src/utils/i18n.ts and add new keys to all language objects:

Step 2: Use in Components

Adding a New Language

To add a new language (e.g., Italian):

Step 1: Update Astro Config

Step 2: Add Language to i18n Utilities

Step 3: Test the New Language

Visit /it/ to see the Italian version of your site.

Localized Content

For fully localized content (not just UI translations), you can organize content by language:

Type Safety

The i18n system is fully type-safe:

Best Practices

  1. Use semantic keys - nav.home instead of home_button
  2. Keep keys organized - Group by component or section
  3. Provide fallbacks - The system falls back to English if a translation is missing
  4. Translate all keys - Ensure all languages have all keys defined
  5. Test all languages - Verify translations in context
  6. Use proper Unicode - Include accented characters correctly
  7. Consider RTL languages - Plan for right-to-left languages if needed
  8. Keep translations short - UI space is limited, especially in mobile views

Advanced Features

Custom Language Detection

Add browser language detection:

Translation Pluralization

For more complex translations with plurals:

Formatted Dates

Localize dates using the Intl API:

Troubleshooting

Issue: Language switcher doesn’t preserve pathSolution: Make sure to remove the language prefix before constructing the new URL:
Issue: Translations showing English fallbackSolution: Verify all translation keys exist in all language objects.Issue: TypeScript errors with translation keysSolution: The ui object uses as const for type inference. Make sure it’s applied.

Components

See how LanguageSwitcher component works

Content Collections

Learn about organizing localized content

Project Structure

Understand where i18n files are located