Skip to content

Commit 591180b

Browse files
authored
Merge pull request #13 from Craftserve/rozwader/switch-languages
feature: added button for switching languages
2 parents 69f989f + e70e0e8 commit 591180b

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

src/i18n/ui.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export const defaultLang = "pl-PL";
22

3+
export const langs = [
4+
"en",
5+
"pl-PL",
6+
]
7+
38
export const ui = {
49
"en": {
510
"example": "test",
@@ -11,6 +16,7 @@ export const ui = {
1116
"header": "Today is your| day to grow| in ideas.",
1217

1318
"aboutUrl": "about",
19+
"language": "EN",
1420
"contactUrl": "#formSection",
1521

1622
"name": "Name",
@@ -35,6 +41,7 @@ export const ui = {
3541
"header": "Dziś jest Twój| dzień, aby urosnąć| w ideach.",
3642

3743
"aboutUrl": "o-nas",
44+
"language": "PL",
3845
"contactUrl": "#formSection",
3946

4047
"name": "Imię",

src/layouts/Layout.astro

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ import Navbar from "../sections/Navbar.astro";
2929
import { getLangForRedirect } from "../i18n/utils";
3030
const lang = getLangForRedirect(new URL(window.location.href));
3131

32-
const userLang = navigator.language || "pl";
32+
const userLang = localStorage.getItem("lang") || navigator.language || "pl";
3333

3434
if (!userLang.startsWith("pl") && lang !== "en") {
35-
const meta = document.createElement("meta");
36-
meta.httpEquiv = "refresh";
37-
meta.content = "0;url=/en/";
38-
document.head.appendChild(meta);
35+
const meta = document.createElement("meta");
36+
meta.httpEquiv = "refresh";
37+
meta.content = `0;url=/en/`;
38+
document.head.appendChild(meta);
3939
}
40-
</script>
40+
</script>
4141
</body>
4242
</html>
4343

src/scripts/NavbarScripts.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getLangFromUrl } from "../i18n/utils";
1+
import { langs } from "../i18n/ui";
2+
import { getLangForRedirect, getLangFromUrl } from "../i18n/utils";
23

34
document.getElementsByClassName("hamburger-button")[0].addEventListener("click", () => {
45
document.getElementsByClassName("hamburger-menu")[0].classList.toggle("hamburger-menu-open");
@@ -14,3 +15,23 @@ document.getElementsByClassName("logoContainer")[0].addEventListener("click", ()
1415

1516
window.location.assign(`/`);
1617
});
18+
19+
document.getElementById("langSwitch").addEventListener("click", () => {
20+
const lang = getLangFromUrl(new URL(window.location.href));
21+
const currentLangIndex = langs.indexOf(lang);
22+
23+
const nextLang = langs[(currentLangIndex + 1) % langs.length];
24+
const targetUrl = nextLang.startsWith("pl") ? "/" : `/${nextLang}/`;
25+
26+
const meta = document.createElement("meta");
27+
meta.httpEquiv = "refresh";
28+
meta.content = `0;url=${targetUrl}`;
29+
30+
if (!nextLang.startsWith("pl")) {
31+
localStorage.setItem("lang", "en-US");
32+
}else{
33+
localStorage.setItem("lang", "pl-PL");
34+
}
35+
36+
document.head.appendChild(meta);
37+
})

src/sections/Navbar.astro

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { getLangFromUrl, getRoutes, useTranslations } from "../i18n/utils";
55
import Link from "../components/Link.astro";
66
import { Icon } from "astro-icon/components";
77
8+
const lang = getLangFromUrl(Astro.url);
9+
const t = useTranslations(lang);
10+
811
const routes = getRoutes(Astro.url);
912
---
1013

@@ -14,17 +17,16 @@ const routes = getRoutes(Astro.url);
1417
<Image src={LogoDark} alt="Logo" width={40} height={40}/>
1518
<h1 style="font-weight: 700; font-size: medium;">Vitresoft</h1>
1619
</button>
20+
1721

1822
<div class="hamburger-menu">
19-
{
20-
routes.map((route) => {
21-
return (
22-
<Link variant="blank" href={route.url}>
23-
{route.name}
24-
</Link>
25-
);
26-
})
27-
}
23+
{routes.map((route) => {
24+
return <Link variant="blank" href={route.url}>{route.name}</Link>
25+
})}
26+
<button class="languageSection" id="langSwitch">
27+
<span style="font-weight: 700;">{t("language")}</span>
28+
<Icon name="uil:globe" width={25} height={25} />
29+
</button>
2830
</div>
2931
<button type="button" class="hamburger-button">
3032
<Icon class="hamburger-icon" name="tabler:menu-2" />
@@ -50,7 +52,20 @@ const routes = getRoutes(Astro.url);
5052
justify-content: space-between;
5153
}
5254

53-
.logoContainer {
55+
.languageSection{
56+
display: flex;
57+
align-items: center;
58+
gap: 5px;
59+
background-color: transparent;
60+
border: none;
61+
cursor: pointer;
62+
border: 2px solid var(--primary);
63+
color: var(--primary);
64+
border-radius: 8px;
65+
padding: 5px 14px;
66+
}
67+
68+
.logoContainer{
5469
display: flex;
5570
flex-direction: row;
5671
align-items: center;

0 commit comments

Comments
 (0)