forked from auxolotl/website
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/`.
This commit is contained in:
parent
919d623e43
commit
dd3a81b5bf
|
@ -1,11 +1,11 @@
|
|||
---
|
||||
interface Props {}
|
||||
|
||||
import { getLangFromUrl, useTranslations } from '../i18n/utils';
|
||||
import { useTranslations } from '../i18n/utils';
|
||||
import { Image } from 'astro:assets';
|
||||
import aux from '../../public/aux.svg';
|
||||
|
||||
const lang = getLangFromUrl(Astro.url);
|
||||
const { lang } = Astro.params;
|
||||
const translation = useTranslations(lang);
|
||||
---
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
|
||||
import { useTranslations } from '../../i18n/utils';
|
||||
|
||||
const lang = getLangFromUrl(Astro.url);
|
||||
const { lang } = Astro.params;
|
||||
const translation = useTranslations(lang);
|
||||
---
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
---
|
||||
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
|
||||
import { useTranslations } from '../../i18n/utils';
|
||||
import { Image } from 'astro:assets';
|
||||
import aux from '../../../public/aux.svg';
|
||||
|
||||
const lang = getLangFromUrl(Astro.url);
|
||||
const { lang } = Astro.params;
|
||||
const translation = useTranslations(lang);
|
||||
---
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
|
||||
import { useTranslations } from '../../i18n/utils';
|
||||
|
||||
const lang = getLangFromUrl(Astro.url);
|
||||
const { lang } = Astro.params;
|
||||
const translation = useTranslations(lang);
|
||||
---
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
import { getLangFromUrl, useTranslations } from '../../i18n/utils';
|
||||
import { useTranslations } from '../../i18n/utils';
|
||||
|
||||
const lang = getLangFromUrl(Astro.url);
|
||||
const { lang } = Astro.params;
|
||||
const translation = useTranslations(lang);
|
||||
---
|
||||
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
import { ui, defaultLang } from './ui';
|
||||
|
||||
export function getLangFromUrl(url: URL) {
|
||||
const [, lang] = url.pathname.split('/');
|
||||
if (lang in ui) return lang as keyof typeof ui;
|
||||
return defaultLang;
|
||||
}
|
||||
|
||||
export function useTranslations(lang: keyof typeof ui) {
|
||||
return function t(key: keyof typeof ui[typeof defaultLang]) {
|
||||
return ui[lang][key] || ui[defaultLang][key];
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
---
|
||||
import { getLangFromUrl } from '../i18n/utils';
|
||||
import Header from "../components/Header.astro";
|
||||
|
||||
interface Props {
|
||||
|
@ -7,8 +6,7 @@ interface Props {
|
|||
}
|
||||
|
||||
const { title } = Astro.props;
|
||||
|
||||
const lang = getLangFromUrl(Astro.url);
|
||||
const { lang } = Astro.params;
|
||||
---
|
||||
|
||||
<!doctype html>
|
||||
|
|
15
src/pages/[lang]/index.astro
Normal file
15
src/pages/[lang]/index.astro
Normal file
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
import { languages } from "../../i18n/ui";
|
||||
import Layout from "../../layouts/Layout.astro";
|
||||
import Home from "../../components/home/Home.astro";
|
||||
|
||||
export async function getStaticPaths() {
|
||||
return Object.keys(languages).map((name) => (
|
||||
{ params: { lang: name } }
|
||||
))
|
||||
}
|
||||
---
|
||||
|
||||
<Layout title="auxolotl.org">
|
||||
<Home />
|
||||
</Layout>
|
|
@ -1,8 +0,0 @@
|
|||
---
|
||||
import Layout from "../../layouts/Layout.astro";
|
||||
import Home from "../../components/home/Home.astro";
|
||||
---
|
||||
|
||||
<Layout title="auxolotl.org">
|
||||
<Home />
|
||||
</Layout>
|
|
@ -1 +1,5 @@
|
|||
<meta http-equiv="refresh" content="0;url=/en/" />
|
||||
---
|
||||
import { defaultLang } from '../i18n/ui';
|
||||
---
|
||||
|
||||
<meta http-equiv="refresh" content=`0;url=/${defaultLang}/` />
|
||||
|
|
Loading…
Reference in a new issue