Overview
TheLangSwitcher component provides a simple toggle for switching between the English and Spanish versions of the Jowy Portfolio. It automatically detects the current language and generates the appropriate URL for the opposite language while preserving the current page path.
Props
This component receives no explicit props. It uses Astro’s built-in i18n context:Astro.url.pathname: Current page pathAstro.currentLocale: Current language locale (“en” or “es”)
Component Behavior
Language Detection
The component automatically:- Reads the current URL path
- Removes any existing language prefix (
/enor/es) - Determines the opposite language
- Generates the correct URL for the same page in the other language
URL Normalization
- Matches
/enor/esat the start of the path - Handles both trailing slashes and direct paths
- Preserves the rest of the URL structure
Language Toggle Logic
getRelativeLocaleUrl() helper to generate properly formatted locale URLs.
Styling
Visual Design
- Neon Style: Applies custom neon text effect via the
neon-styleclass - Bold Typography: Uses bold font weight for emphasis
- Hover Animation: Scales to 125% on hover with smooth 300ms transition
- Responsive Text: Adjusts size based on screen width:
- Small screens:
sm:text-sm - Medium screens:
md:text-base - Large screens:
lg:text-lg
- Small screens:
Positioning
When used in layouts, the component is typically positioned:- Fixed in the top-right corner
- High z-index (
z-9999) to stay above other content - Grouped with other navigation icons
Usage
Basic Implementation
In PageLayout (Typical Usage)
/home/daytona/workspace/source/src/layouts/PageLayout.astro:48
With Other Navigation Elements
i18n Integration
Astro i18n Configuration
The component depends on Astro’s i18n configuration being set up inastro.config.mjs:
Language Routing
The component usesgetRelativeLocaleUrl() from astro:i18n to ensure:
- Proper locale prefixes are added/removed
- URLs are correctly formatted for both languages
- Navigation preserves the current page context
URL Examples
Here’s how the component transforms URLs:- From Spanish
- From English
- Nested Paths
- Current:
/bio(Spanish default, no prefix) - Normalized:
/bio - Target Language:
en - Result:
/en/bio
Technical Implementation
Regex Pattern Breakdown
^- Start of string\/- Literal forward slash(en|es)- Capture either “en” or “es”(\/|$)- Match either another slash or end of string
- Only language prefixes at the start are removed
- Doesn’t affect other parts of the URL
- Handles both
/enand/en/formats
Helper Function
ThegetRelativeLocaleUrl(locale, path) function:
- Takes target locale and normalized path
- Returns properly formatted locale-aware URL
- Handles default locale prefix omission
- Ensures consistent URL structure
Accessibility
The component is a standard anchor link with:- Semantic
<a>element for proper navigation - Visible text label (language code)
- Keyboard navigable (standard tab order)
- Clear hover states for visual feedback
Potential Enhancements
For improved accessibility, consider adding:Customization
Adding More Languages
To support additional languages, modify the logic:Custom Styling
Override the neon effect with custom CSS:/home/daytona/workspace/source/src/components/LangSwitcher.astro:1