Skip to content

Commit 2520682

Browse files
committed
💥 fix: intl in 404 if available check
1 parent 4407f7e commit 2520682

File tree

6 files changed

+35
-28
lines changed

6 files changed

+35
-28
lines changed

package-lock.json

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"astro": "astro"
1111
},
1212
"dependencies": {
13-
"astro": "^4.5.14",
1413
"@astrojs/check": "^0.5.10",
15-
"typescript": "^5.4.3"
14+
"astro": "^4.5.14",
15+
"typescript": "^5.4.5"
1616
}
17-
}
17+
}

src/i18n/en.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ export const en = {
1515
projects: "Get in touch with my projects"
1616
},
1717

18-
not_found: {
18+
404: {
1919
title: "404 - Not found!",
2020
description: {
2121
l1: "Looks like I haven't developed this yet!",
22-
l2: "Feel free to suggest me anything, talk to me on any of",
23-
socials: "my socials!"
24-
}
22+
l2: "Feel free to suggest me anything, talk to me on any of"
23+
},
24+
socials: "my socials!"
2525
},
2626

2727
footer: {

src/i18n/pt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ export const pt = {
1515
projects: "Veja os meus projetos"
1616
},
1717

18-
not_found: {
18+
404: {
1919
title: "404 - Não encontrado!",
2020
description: {
2121
l1: "Parece que eu ainda não desenvolvi essa página!",
22-
l2: "Se quiser sugerir alguma ideia, me chame em qualquer uma das",
23-
socials: "minhas redes sociais!"
24-
}
22+
l2: "Se quiser sugerir alguma ideia, me chame em qualquer uma das"
23+
},
24+
socials: "minhas redes sociais!"
2525
},
2626

2727
footer: {

src/i18n/utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11

2-
import { i18n, defaultLang } from "./ui.ts";
2+
import { languages, i18n, defaultLang } from "./ui.ts";
33

44
export function getLanguageFromURL(url: URL): string {
5-
var splittedURL = url.pathname.split("/");
6-
var currentIntl = splittedURL[1]; // Gets the second URL part
7-
var domain = splittedURL[0].replace("/", "");
5+
var splittedURL: string[] = url.pathname.split("/");
6+
var currentIntl: string = splittedURL[1]; // Gets the second URL part
87

98
if(currentIntl in i18n)
109
return currentIntl as keyof typeof i18n;
@@ -25,3 +24,7 @@ export function useTranslations(lang: keyof typeof i18n): Function {
2524
return translation || defaultTranslation || "Couldn't find key to translate.";
2625
}
2726
}
27+
28+
export function getLanguages() {
29+
return languages;
30+
}

src/pages/404.astro

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
---
2-
import { getLanguageFromURL, useTranslations } from "../i18n/utils";
2+
import { getLanguages, getLanguageFromURL, useTranslations } from "../i18n/utils";
33
import "../styles/global.css";
44
import NavBar from "../components/NavBar.astro";
55
6-
Astro.params.lang = getLanguageFromURL(Astro.url);
6+
Astro.params.lang = "en";
7+
const langs: string[] = getLanguages();
78
8-
if(Astro.params.lang === undefined) {
9-
Astro.params.lang = "en";
10-
}
9+
langs.forEach(intl => {
10+
if(getLanguageFromURL(Astro.url) == intl) {
11+
Astro.params.lang = intl;
12+
}
13+
});
1114
1215
const tr = useTranslations(Astro.params.lang as never);
1316
---
@@ -24,10 +27,10 @@ const tr = useTranslations(Astro.params.lang as never);
2427
<body>
2528
<div class="center">
2629
<div class="not-found">
27-
<h1> { tr("not_found.title") } </h1>
30+
<h1> { tr("404.title") } </h1>
2831
<p>
29-
{ tr("not_found.description.l1") } <br>
30-
{ tr("not_found.description.l2") } <a href={ `/${Astro.params.lang}/#socials` }>{ tr("not_found.description.socials") }</a>
32+
{ tr("404.description.l1") } <br>
33+
{ tr("404.description.l2") } <a href={ `/${Astro.params.lang}/#socials` }>{ tr("404.socials") }</a>
3134
</p>
3235
</div>
3336
</div>

0 commit comments

Comments
 (0)