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 inastro.config.mjs:
defaultLocale: 'en'- English is the default languageprefixDefaultLocale: false- English pages don’t have/en/prefix- All other languages use a prefix (
/es/,/fr/,/de/)
Translation Definitions
Translations are defined insrc/utils/i18n.ts:
Translation System
Translation Keys
Theui object contains all translations organized by language:
Utility Functions
Getting Current Language
Extract the language from the URL:Using Translations
Create a translation function for a specific language:Language Switcher Component
The LanguageSwitcher component allows users to change languages:- Shows current language with highlighting
- Preserves the current path when switching languages
- Handles the default locale (no prefix for English)
src/components/shared/LanguageSwitcher.astro:1
URL Structure
Default Language (English)
Other Languages
Adding New Translations
Step 1: Add Translation Keys
Editsrc/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:- Content Structure
- Querying by Language
- Alternative Approach
Type Safety
The i18n system is fully type-safe:Best Practices
i18n Best Practices
i18n Best Practices
- Use semantic keys -
nav.homeinstead ofhome_button - Keep keys organized - Group by component or section
- Provide fallbacks - The system falls back to English if a translation is missing
- Translate all keys - Ensure all languages have all keys defined
- Test all languages - Verify translations in context
- Use proper Unicode - Include accented characters correctly
- Consider RTL languages - Plan for right-to-left languages if needed
- 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
Common i18n Issues
Common i18n Issues
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.Related Pages
Components
See how LanguageSwitcher component works
Content Collections
Learn about organizing localized content
Project Structure
Understand where i18n files are located