Files
Tiber365/src/layouts/BaseLayout.astro
becarta 3168826fa8 Add internationalization support with astro-i18next integration
- Implemented astro-i18next for multi-language support, including English, Dutch, and Italian.
- Configured default locale and language fallback settings.
- Defined routes for localized content in the configuration.
- Updated package.json and package-lock.json to include new dependencies for i18next and related plugins.
2025-05-23 15:10:00 +02:00

118 lines
3.7 KiB
Plaintext

---
import { SITE } from '../site.config';
import i18next, { t } from "i18next";
import { Trans, HeadHrefLangs } from "astro-i18next/components";
import { localizePath } from "astro-i18next";
import '../styles/global.css';
export interface Props {
title?: string;
description?: string;
image?: string;
keywords?: string;
}
const {
title = t('site.title'),
description = t('site.description'),
image = SITE.ogImage,
keywords = t('meta.keywords')
} = Astro.props;
const lang = Astro.currentLocale || 'en';
// Safe URL creation with fallbacks
let canonicalURL;
let ogImageURL;
let twitterImageURL;
try {
const siteURL = Astro.site || new URL('http://localhost:4321');
canonicalURL = new URL(Astro.url.pathname, siteURL);
ogImageURL = new URL(image, siteURL);
twitterImageURL = new URL(image, siteURL);
} catch (error) {
// Fallback URLs if there's an issue
const fallbackSite = 'https://tiber365.it';
canonicalURL = new URL(Astro.url?.pathname || '/', fallbackSite);
ogImageURL = new URL(image, fallbackSite);
twitterImageURL = new URL(image, fallbackSite);
}
const fullTitle = title === t('site.title') ? title : `${title} | ${t('site.title')}`;
// Get all available locales
const locales = ["en", "nl", "it"];
---
<!doctype html>
<html lang={lang} class="scroll-smooth">
<head>
<meta charset="UTF-8" />
<meta name="description" content={description} />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="generator" content={Astro.generator} />
{keywords && <meta name="keywords" content={keywords} />}
<!-- Canonical URL -->
<link rel="canonical" href={canonicalURL} />
<!-- Primary Meta Tags -->
<title>{fullTitle}</title>
<meta name="title" content={fullTitle} />
<meta name="description" content={description} />
<meta name="author" content={SITE.author} />
<!-- Prevent automatic language redirects -->
<meta name="google" content="notranslate" />
<meta http-equiv="Content-Language" content={lang} />
<!-- Theme Color -->
<meta name="theme-color" content="#ffffff" />
<meta name="msapplication-TileColor" content="#3b82f6" />
<!-- Open Graph / Facebook -->
<meta property="og:type" content="website" />
<meta property="og:url" content={canonicalURL} />
<meta property="og:title" content={fullTitle} />
<meta property="og:description" content={description} />
<meta property="og:image" content={ogImageURL} />
<meta property="og:site_name" content={t('site.title')} />
<meta property="og:locale" content={lang === 'en' ? 'en_US' : lang === 'nl' ? 'nl_NL' : 'it_IT'} />
<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image" />
<meta property="twitter:url" content={canonicalURL} />
<meta property="twitter:title" content={fullTitle} />
<meta property="twitter:description" content={description} />
<meta property="twitter:image" content={twitterImageURL} />
<!-- Favicons -->
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="manifest" href="/manifest.json" />
<!-- Language alternates -->
{locales.map(locale => (
<link
rel="alternate"
hreflang={locale}
href={new URL(localizePath(Astro.url.pathname, locale), Astro.site).toString()}
/>
))}
</head>
<body class="min-h-screen bg-background text-foreground">
<slot />
<!-- Initialize animations -->
<script>
import { initScrollAnimations } from '../utils/animations';
import { initTheme } from '../utils/theme';
document.addEventListener('DOMContentLoaded', () => {
initTheme();
initScrollAnimations();
});
</script>
</body>
</html>