Skip to content

Commit

Permalink
Merge pull request #288 from blz-it/fix-everything
Browse files Browse the repository at this point in the history
Fix Everything
  • Loading branch information
Benjamin-Frost authored Oct 15, 2024
2 parents 96e1fd7 + 700c73d commit 4b2ce41
Show file tree
Hide file tree
Showing 13 changed files with 418 additions and 317 deletions.
1 change: 0 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default defineConfig({
i18n: {
defaultLocale: defaultLang,
locales: Object.keys(languages),
routing: "manual", // see: src/middleware.ts
},
integrations: [tailwind(), react(), icon()],
site: "https://blz-it.de",
Expand Down
10 changes: 9 additions & 1 deletion src/i18n/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ export function useTranslations(lang: Language) {
};
}

const removeLocalePrefix = (path: string): string => {
const localePattern = new RegExp(
`^/(${Object.keys(languages).join("|")})\\b`,
);
return path.replace(localePattern, "");
};

export type LocalePath = { lang: Language; path: string };
export const getLocalePaths = (url: URL): LocalePath[] => {
const cleanPath = removeLocalePrefix(url.pathname);
return Object.keys(languages).map((lang) => ({
lang: lang as Language,
path: getRelativeLocaleUrl(lang, url.pathname.replace(/^\/[a-zA-Z-]+/, "")),
path: getRelativeLocaleUrl(lang, cleanPath),
}));
};

Expand Down
29 changes: 0 additions & 29 deletions src/middleware.ts

This file was deleted.

35 changes: 35 additions & 0 deletions src/pages/404.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
// This page automatically redirects to 404 page for user's locale
import { defaultLang, languages } from "~/i18n";
const langs = Object.keys(languages);
---

<html lang={defaultLang}>
<head>
<meta charset="UTF-8" />
<title>404 PAGE NOT FOUND</title>
<link rel="canonical" href={Astro.site} />

<noscript>
<meta http-equiv="refresh" content={`0;url=/${defaultLang}/404/`} />
</noscript>

<script is:inline define:vars={{ langs, defaultLang }}>
const urlHasLang = langs.includes(location.pathname.split("/")[1]);

// First, try to redirect to the default locale (e.g., page could be monolingual)
if (!urlHasLang) {
location.href = "/" + defaultLang + location.pathname;
return;
}

// If the user's locale is not available, redirect to 404 page
location.href = `/${location.pathname.split("/")[1]}/404/`;
</script>
</head>
<body>
<h1>404 PAGE NOT FOUND</h1>
<a href="/">TOP PAGE</a>
</body>
</html>
150 changes: 0 additions & 150 deletions src/pages/[lang]/index.astro

This file was deleted.

68 changes: 0 additions & 68 deletions src/pages/[lang]/it-software-solutions-for-business.astro

This file was deleted.

68 changes: 0 additions & 68 deletions src/pages/[lang]/mobile-applications-development.astro

This file was deleted.

Loading

0 comments on commit 4b2ce41

Please sign in to comment.