LanguagePicker.astro 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. ---
  2. /**
  3. * Language switcher component.
  4. * Provides a link to the alternate language version of the current page.
  5. */
  6. import { getAltLang } from '@i18n/utils';
  7. interface Props {
  8. lang?: 'zh' | 'en';
  9. currentPath?: string;
  10. }
  11. const { lang = 'zh', currentPath = '/' } = Astro.props;
  12. const altLang = getAltLang(lang);
  13. // Build the alternate language path by replacing the language prefix
  14. const pathParts = currentPath.split('/').filter(Boolean);
  15. // Remove current lang prefix if present
  16. if (pathParts[0] === lang) {
  17. pathParts.shift();
  18. }
  19. const altPath = `/${altLang}/${pathParts.join('/')}`;
  20. ---
  21. <a
  22. href={altPath}
  23. class="inline-flex items-center gap-1.5 rounded-md px-2.5 py-1.5 text-sm font-medium text-dark-500 hover:text-primary-600 hover:bg-primary-50 transition-colors"
  24. hreflang={altLang === 'zh' ? 'zh-CN' : 'en-US'}
  25. title={lang === 'zh' ? 'Switch to English' : '切换到中文'}
  26. >
  27. <svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
  28. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
  29. </svg>
  30. <span>{lang === 'zh' ? 'EN' : '中文'}</span>
  31. </a>