Skip to content

Commit dd3a81b

Browse files
hedyhlimrshmllow
authored andcommitted
refactor: Avoid duplicating pages for each language
This makes use of Astro dynamic routing to avoid having to redefine all pages for each language. Note that they still have to be defined in `i18n/ui.ts` which is not refactored in this PR. The Index will also now use the `defaultLang` from `i18n/ui.ts` rather than hardcoding `/en/`.
1 parent 919d623 commit dd3a81b

File tree

10 files changed

+31
-28
lines changed

10 files changed

+31
-28
lines changed

src/components/Header.astro

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
interface Props {}
33
4-
import { getLangFromUrl, useTranslations } from '../i18n/utils';
4+
import { useTranslations } from '../i18n/utils';
55
import { Image } from 'astro:assets';
66
import aux from '../../public/aux.svg';
77
8-
const lang = getLangFromUrl(Astro.url);
8+
const { lang } = Astro.params;
99
const translation = useTranslations(lang);
1010
---
1111

src/components/home/Goals.astro

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
2+
import { useTranslations } from '../../i18n/utils';
33
4-
const lang = getLangFromUrl(Astro.url);
4+
const { lang } = Astro.params;
55
const translation = useTranslations(lang);
66
---
77

src/components/home/Hero.astro

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
2+
import { useTranslations } from '../../i18n/utils';
33
import { Image } from 'astro:assets';
44
import aux from '../../../public/aux.svg';
55
6-
const lang = getLangFromUrl(Astro.url);
6+
const { lang } = Astro.params;
77
const translation = useTranslations(lang);
88
---
99

src/components/home/Roadmap.astro

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
2+
import { useTranslations } from '../../i18n/utils';
33
4-
const lang = getLangFromUrl(Astro.url);
4+
const { lang } = Astro.params;
55
const translation = useTranslations(lang);
66
---
77

src/components/home/Values.astro

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
2+
import { useTranslations } from '../../i18n/utils';
33
4-
const lang = getLangFromUrl(Astro.url);
4+
const { lang } = Astro.params;
55
const translation = useTranslations(lang);
66
---
77

src/i18n/utils.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { ui, defaultLang } from './ui';
22

3-
export function getLangFromUrl(url: URL) {
4-
const [, lang] = url.pathname.split('/');
5-
if (lang in ui) return lang as keyof typeof ui;
6-
return defaultLang;
7-
}
8-
93
export function useTranslations(lang: keyof typeof ui) {
104
return function t(key: keyof typeof ui[typeof defaultLang]) {
115
return ui[lang][key] || ui[defaultLang][key];

src/layouts/Layout.astro

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
---
2-
import { getLangFromUrl } from '../i18n/utils';
32
import Header from "../components/Header.astro";
43
54
interface Props {
65
title: string;
76
}
87
98
const { title } = Astro.props;
10-
11-
const lang = getLangFromUrl(Astro.url);
9+
const { lang } = Astro.params;
1210
---
1311

1412
<!doctype html>

src/pages/[lang]/index.astro

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
import { languages } from "../../i18n/ui";
3+
import Layout from "../../layouts/Layout.astro";
4+
import Home from "../../components/home/Home.astro";
5+
6+
export async function getStaticPaths() {
7+
return Object.keys(languages).map((name) => (
8+
{ params: { lang: name } }
9+
))
10+
}
11+
---
12+
13+
<Layout title="auxolotl.org">
14+
<Home />
15+
</Layout>

src/pages/en/index.astro

-8
This file was deleted.

src/pages/index.astro

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<meta http-equiv="refresh" content="0;url=/en/" />
1+
---
2+
import { defaultLang } from '../i18n/ui';
3+
---
4+
5+
<meta http-equiv="refresh" content=`0;url=/${defaultLang}/` />

0 commit comments

Comments
 (0)