Skip to content

Commit

Permalink
app: Dynamic locale components and change
Browse files Browse the repository at this point in the history
  • Loading branch information
busybox11 committed May 22, 2024
1 parent 757c7f5 commit 406379f
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 109 deletions.
109 changes: 2 additions & 107 deletions apps/web/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Locales from "@/components/Home/Locales";
import Image from "next/image";

export default function Page(): JSX.Element {
Expand Down Expand Up @@ -82,113 +83,7 @@ export default function Page(): JSX.Element {
</a>
</div>

<div className="flex flex-row justify-center gap-3 lg:gap-4 flex-wrap">
<a href="?lang=en">
<img
src="/flags/uk.svg"
title="English"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=fr">
<img
src="/flags/fr.svg"
title="Français"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=it">
<img
src="/flags/it.svg"
title="Italiano"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=es">
<img
src="/flags/es.svg"
title="Español"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=ru">
<img
src="/flags/ru.svg"
title="Pусский"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=de">
<img
src="/flags/de.svg"
title="Deutsch"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=id">
<img
src="/flags/id.svg"
title="Indonesia"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=cz">
<img
src="/flags/cz.svg"
title="Czech"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=tr">
<img
src="/flags/tr.svg"
title="Turkish"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=gr">
<img
src="/flags/gr.svg"
title="Greece"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=zh_tw">
<img
src="/flags/zh_tw.svg"
title="Traditionnal Chinese"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=ar">
<img
src="/flags/ar.svg"
title="Arabic"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=eo">
<img
src="/flags/eo.svg"
title="Esperanto"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=nl">
<img
src="/flags/nl.svg"
title="Dutch"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
<a href="?lang=az">
<img
src="/flags/az.png"
title="Azərbaycan"
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</a>
</div>
<Locales />

<h6 className="text-xs text-white/50 hover:text-white/80 transition text-pretty">
By clicking on 'LOGIN TO SPOTIFY', you accept the use of cookies
Expand Down
7 changes: 7 additions & 0 deletions apps/web/app/playing/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Playing() {
return (
<div className="flex flex-col items-center justify-center h-screen">
<div className="text-4xl font-bold">Playing</div>
</div>
);
}
21 changes: 21 additions & 0 deletions apps/web/components/Home/Locales.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use client";

import useLocales from "@/hooks/useLocales";

export default function Locales() {
const { locales, setLocale } = useLocales();

return (
<div className="flex flex-row justify-center gap-3 lg:gap-4 flex-wrap">
{locales.map((locale) => (
<button key={locale.code} onClick={() => setLocale(locale.code)}>
<img
src={locale.flag}
title={locale.name}
className="h-8 w-8 opacity-50 hover:opacity-100 hover:scale-105 transition"
/>
</button>
))}
</div>
);
}
106 changes: 106 additions & 0 deletions apps/web/hooks/useLocales.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { useLocalStorage } from "usehooks-ts";

const locales = [
{
code: "en",
name: "English",
flag: "/flags/uk.svg",
},
{
code: "fr",
name: "Français",
flag: "/flags/fr.svg",
},
{
code: "it",
name: "Italiano",
flag: "/flags/it.svg",
},
{
code: "es",
name: "Español",
flag: "/flags/es.svg",
},
{
code: "ru",
name: "Pусский",
flag: "/flags/ru.svg",
},
{
code: "de",
name: "Deutsch",
flag: "/flags/de.svg",
},
{
code: "id",
name: "Indonesia",
flag: "/flags/id.svg",
},
{
code: "cz",
name: "Czech",
flag: "/flags/cz.svg",
},
{
code: "tr",
name: "Turkish",
flag: "/flags/tr.svg",
},
{
code: "gr",
name: "Greece",
flag: "/flags/gr.svg",
},
{
code: "zh_tw",
name: "Traditionnal Chinese",
flag: "/flags/zh_tw.svg",
},
{
code: "ar",
name: "Arabic",
flag: "/flags/ar.svg",
},
{
code: "eo",
name: "Esperanto",
flag: "/flags/eo.svg",
},
{
code: "nl",
name: "Dutch",
flag: "/flags/nl.svg",
},
{
code: "az",
name: "Azərbaycan",
flag: "/flags/az.png",
},
] as const;

function getBrowserLocale() {
if (typeof navigator === "undefined") return "en";

return navigator.language?.split("-")[0] ?? "en";
}

function getInitialLocale() {
const browserLocale = getBrowserLocale();
return locales.find((locale) => locale.code === browserLocale) ?? locales[0];
}

export default function useLocales() {
const [currentLocale, setCurrentLocale] = useLocalStorage(
"locale",
getInitialLocale()
);

const setLocale = (locale: (typeof locales)[number]["code"]) => {
const newLocale = locales.find((l) => l.code === locale);
if (newLocale) {
setCurrentLocale(newLocale);
}
};

return { locales, currentLocale, setLocale };
}
11 changes: 11 additions & 0 deletions apps/web/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/** @type {import('next').NextConfig} */
module.exports = {
transpilePackages: ["@repo/ui"],

async redirects() {
// Remove .php extension
return [
{
source: "/:path*.php",
destination: "/:path*",
permanent: true,
},
];
},
};
3 changes: 2 additions & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"dependencies": {
"next": "^14.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"usehooks-ts": "^3.1.0"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.1.1",
Expand Down
6 changes: 5 additions & 1 deletion apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
{
"name": "next"
}
]
],
"baseUrl": ".",
"paths": {
"@/*": ["./*"]
}
},
"include": [
"next-env.d.ts",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 406379f

Please sign in to comment.