Skip to content

Commit 37dddcd

Browse files
authored
Merge pull request #30 from retrozinndev/devel
✨ feat: use new i18n system and static paths
2 parents aa3ff47 + 4481801 commit 37dddcd

File tree

12 files changed

+146
-59
lines changed

12 files changed

+146
-59
lines changed

package-lock.json

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

src/components/NavBar.astro

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
---
2+
import { useTranslations } from "../i18n/utils.ts";
23
import "../styles/global.css";
34
4-
var options = {
5-
Home: "Home",
6-
About: "About Me",
7-
Projects: "Projects"
8-
};
5+
const tr = useTranslations(Astro.params.lang as never);
96
---
107

11-
<script>
12-
console.log(Astro.currentLocale);
13-
</script>
14-
158
<header>
169
<div class="nav-left">
1710
<a rel="author" href="https://github.com/retrozinndev">
@@ -21,13 +14,13 @@ var options = {
2114
<nav class="navbar">
2215
<ul class="links">
2316
<li class="link">
24-
<a href="/#top" target="_self">{ options.Home }</a>
17+
<a href="/#top" target="_self">{ tr("nav.home") }</a>
2518
</li>
2619
<li class="link">
27-
<a href="/#chars" target="_self">{ options.About }</a>
20+
<a href="/#chars" target="_self">{ tr("nav.about") }</a>
2821
</li>
2922
<li class="link">
30-
<a href="/projects" target="_self">{ options.Projects }</a>
23+
<a href="/projects" target="_self">{ tr("nav.projects") }</a>
3124
</li>
3225
</ul>
3326
</nav>

src/env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/// <reference path="../.astro/types.d.ts" />
12
/// <reference types="astro/client" />

src/i18n/en.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const en = {
2+
3+
nav: {
4+
home: "Home",
5+
about: "About",
6+
projects: "Projects"
7+
},
8+
9+
about: {
10+
title: "Hi, I'm",
11+
description: `I'm a self-taught programmer and tech enthusiast who's learning software
12+
development stuff! I absolutely love making open-source projects! I like: coding,
13+
learning about Linux and making games and apps in my free time!`,
14+
socials_title: "You can find me in:",
15+
projects: "Get in touch with my projects"
16+
},
17+
18+
not_found: {
19+
title: "404 - Not found!",
20+
description: `Looks like I haven't developed this yet!
21+
Feel free to suggest me anything, talk to me on any of`,
22+
social_link: "my socials!"
23+
}
24+
} as const;

src/i18n/pt.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const pt = {
2+
3+
nav: {
4+
home: "Início",
5+
about: "Sobre mim",
6+
projects: "Projetos"
7+
},
8+
9+
about: {
10+
title: "Olá, sou o",
11+
description: `Sou um programador autodidata e entusiasta de tecnologia que está amando o
12+
mundo da programação! Eu gosto muito de fazer projetos open-source! Eu curto: programar,
13+
aprender sobre GNU/Linux, fazer jogos e aplicativos no meu tempo livre!`,
14+
socials_title: "Você pode me encontrar em:",
15+
projects: "Veja os meus projetos"
16+
},
17+
18+
not_found: {
19+
title: "404 - Não encontrado!",
20+
description: `Parece que eu ainda não desenvolvi essa página!
21+
Feel free to suggest me anything, talk to me on any of`,
22+
social_link: "minhas redes sociais!"
23+
}
24+
} as const;

src/i18n/route.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
import { languages } from "./ui";
3+
4+
export const getStaticPaths = () =>
5+
languages.map(lang => ({ params: { lang } }));

src/i18n/ui.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import { en } from"./en.ts";
3+
import { pt } from "./pt.ts";
4+
5+
export let i18n: object = {
6+
en: {
7+
...en
8+
},
9+
pt: {
10+
...pt
11+
}
12+
};
13+
14+
export const languages: Array<string> = Object.keys(i18n);
15+
export const defaultLang = "en" as keyof typeof i18n;
16+
17+
console.log(languages);

src/i18n/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
import { i18n, defaultLang } from "./ui.ts";
3+
4+
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("/", "");
8+
9+
if(currentIntl in i18n)
10+
return currentIntl as keyof typeof i18n;
11+
12+
return currentIntl as string;
13+
}
14+
15+
export function useTranslations(lang: keyof typeof i18n): Function {
16+
return function tr(key: string): string {
17+
const keys = key.split(".");
18+
let translation = i18n[lang], defaultTranslation = i18n[defaultLang];
19+
20+
keys.forEach(key => {
21+
translation = translation[key];
22+
defaultTranslation = defaultTranslation[key];
23+
});
24+
25+
return translation || defaultTranslation || "Couldn't find key to translate.";
26+
}
27+
}

src/pages/404.astro

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
---
2+
23
import "../styles/global.css";
34
import NavBar from "../components/NavBar.astro";
4-
import PageLayout from "../layouts/PageLayout.astro";
55
---
66

7+
<!DOCTYPE html>
8+
<html lang="en">
9+
<head>
10+
<meta charset="UTF-8">
11+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
12+
<title>retrozinndev - Not Found!</title>
13+
<link rel="icon" href="https://github.com/retrozinndev.png" />
14+
</head>
715
<NavBar />
8-
<PageLayout title="retrozinndev | Not Found" icon="https://github.com/retrozinndev.png" lang="en-US">
16+
<body>
917
<div class="center">
1018
<div class="not-found">
11-
<h1>404!</h1>
19+
<h1> 404! </h1>
1220
<p>
13-
Looks like I didn't developed this yet!<br>
21+
Looks like I haven't developed this yet!<br>
1422
Feel free to suggest me anything, talk to me on any of <a href="/#socials">my socials!</a>
1523
</p>
1624
</div>
1725
</div>
18-
</PageLayout>
26+
<body>
27+
28+
</body>
29+
</html>
1930

2031
<style>
2132
.not-found h1 {

src/pages/en/index.astro renamed to src/pages/[lang]/index.astro

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
---
2+
import { getStaticPaths } from "../../i18n/route.ts";
3+
import { useTranslations } from "../../i18n/utils.ts";
24
import SocialLinks from '../../components/SocialLinks.astro';
35
import PageLayout from '../../layouts/PageLayout.astro';
46
import "../../styles/about_page.css";
7+
8+
export { getStaticPaths };
9+
const tr = useTranslations(Astro.params.lang as never);
510
---
611

712
<PageLayout title={ "retrozinndev" + " | " + "Home" } icon="https://github.com/retrozinndev.png" lang="en-US">
813
<body>
914
<div class="container">
1015
<div class="about-section title">
11-
<h1>Hi, I'm <span class="highlight">João!</span></h1>
16+
<h1>{ tr("about.title") } <span class="highlight">João!</span> </h1>
1217
</div>
1318
<div class="about" id="about">
14-
I'm a self-taught programmer and tech enthusiast that's learning software development stuff! <br>
15-
I absolutely love making open-source projects! <br>
16-
I like: coding, learning about Linux and making games and apps in my free time! <br>
19+
{ tr("about.description") }
1720
</div>
18-
<a href="/projects"><h3 class="projects" id="projects">Get in touch with my projects &UpperRightArrow;</h3></a>
21+
<a href="/projects"><h3 class="projects" id="projects">{ tr("about.projects") } &UpperRightArrow;</h3></a>
1922
<div class="socials ul" id="socials">
20-
<h2>You can find me in: </h2>
23+
<h2>{ tr("about.socials_title") }</h2>
2124
<SocialLinks />
2225
</div>
2326
</div>

0 commit comments

Comments
 (0)