Skip to content

Commit

Permalink
setting language route based on http request header now
Browse files Browse the repository at this point in the history
  • Loading branch information
Resaki1 committed Dec 7, 2023
1 parent 96f66ad commit 4627e38
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
14 changes: 9 additions & 5 deletions astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { defineConfig } from "astro/config";

import node from "@astrojs/node";
import react from "@astrojs/react";

// https://astro.build/config
export default defineConfig({
integrations: [react()],
redirects: {
"/legal": "/legal/privacy",
},
integrations: [react()],
redirects: {
"/legal": "/legal/privacy",
},
output: "server",
adapter: node({
mode: "standalone",
}),
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint": "eslint . --ext .js,.ts,.astro"
},
"dependencies": {
"@astrojs/node": "^7.0.0",
"@astrojs/react": "^3.0.6",
"@directus/sdk": "^13.0.2",
"@types/react": "^18.2.42",
Expand Down
25 changes: 16 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/pages/[lang]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import Feedback from "../../views/Feedback/Feedback.astro";
import AboutUs from "../../views/AboutUs/AboutUs.astro";
export const getStaticPaths = (() => {
return [
{params: {lang: "en"}},
{params: {lang: "de"}},
];
return [{ params: { lang: "en" } }, { params: { lang: "de" } }];
}) satisfies GetStaticPaths;
export const prerender = true;
---

<Layout title="Scrumlr">
Expand Down
13 changes: 12 additions & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
---
const AVAILABLE_TRANSLATIONS = ["de", "en"];
const language = Astro.request.headers.get("accept-language");
// Split the language string into an array of languages
const languages = language
?.split(",")
.map((lang) => lang.split(";")[0].trim().split("-")[0]);
// Find the first language in the header that matches one of the available languages
const pageLang =
languages?.find((lang) => AVAILABLE_TRANSLATIONS.includes(lang)) || "en";
---

<meta http-equiv="refresh" content="0;url=/en" />
<meta http-equiv="refresh" content=`0;url=/${pageLang}` />

0 comments on commit 4627e38

Please sign in to comment.