diff --git a/src/components/ItemCard.astro b/src/components/ItemCard.astro index 8bc2019..b7be9f5 100644 --- a/src/components/ItemCard.astro +++ b/src/components/ItemCard.astro @@ -11,7 +11,7 @@ interface Props { button_name: string; } -const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); +const lang = await getLocales(Astro.cookies.get('language')?.value ?? await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); --- diff --git a/src/components/NavBar.astro b/src/components/NavBar.astro index ac4b460..8afcc74 100644 --- a/src/components/NavBar.astro +++ b/src/components/NavBar.astro @@ -19,7 +19,7 @@ interface NavElement { dropdowns?: Array; } -const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); +const lang = await getLocales(Astro.cookies.get('language')?.value ?? await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); const listOfElements: Array = [{ id: "home", diff --git a/src/components/ResourceBar.astro b/src/components/ResourceBar.astro index 3033256..43dae4e 100644 --- a/src/components/ResourceBar.astro +++ b/src/components/ResourceBar.astro @@ -7,7 +7,7 @@ import { Resource } from '../lib/classes/managers/ResourceManager'; const resourceTypes = await getAllResources(); -const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); +const lang = await getLocales(Astro.cookies.get('language')?.value ?? await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); const planetId = new ObjectId(Astro.cookies.get('planetid')?.value ?? ''); diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index 0018557..7734ce7 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -1,11 +1,23 @@ --- import PlanetView from '../components/PlanetView.astro'; +import { getHighestWeightedLanguage } from '../lib/utils/langDriver'; interface Props { title: string; } const { title } = Astro.props; + +if(!Astro.cookies.has('language')) { + const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); + + Astro.cookies.set('language', locale, { + path: "/", + maxAge: 60 * 60 * 24 * 365, + sameSite: "lax", + secure: true + }); +} --- diff --git a/src/lib/utils/langDriver.ts b/src/lib/utils/langDriver.ts index c5a916c..144418f 100644 --- a/src/lib/utils/langDriver.ts +++ b/src/lib/utils/langDriver.ts @@ -1,15 +1,7 @@ import { GET as langs } from '../../pages/api/lang.json'; export async function getSupportedLanguages() { - const metadata: { id: string, name: string }[] = await (await langs()).json(); - - const response: Array = []; - - metadata.forEach((lang: any) => { - response.push(lang.id); - }); - - return response; + return await (await langs()).json() as { id: string, name: string }[] } export async function getHighestWeightedLanguage(header: string | null): Promise { @@ -25,7 +17,7 @@ export async function getHighestWeightedLanguage(header: string | null): Promise const langName = subArray[0]; const langWeight = subArray.filter((sub: string) => sub.includes("q=")).length > 0 ? parseFloat(subArray.filter((sub: string) => sub.includes("q="))[0].split("=")[1]) : 1; - if(langWeight > highestWeight && supportedLanguages.includes(langName)) { + if(langWeight > highestWeight && supportedLanguages.find(l => l.id === langName)) { highestWeight = langWeight; highestWeightedLang = langName; } @@ -35,7 +27,7 @@ export async function getHighestWeightedLanguage(header: string | null): Promise } export async function getLocales(language: string) { - if(!(await getSupportedLanguages()).includes(language)) { + if(!(await getSupportedLanguages()).find(l => l.id === language)) { console.log(await getSupportedLanguages(), language) return null; } diff --git a/src/pages/game/buildings.astro b/src/pages/game/buildings.astro index 7a4ebfc..d27402e 100644 --- a/src/pages/game/buildings.astro +++ b/src/pages/game/buildings.astro @@ -19,7 +19,7 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout'); const checkUser = await getUserByAccessToken(loggedToken); if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); -const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); +const locale = Astro.cookies.get('language')?.value ?? await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); const lang = await getLocales(locale); diff --git a/src/pages/game/fleet.astro b/src/pages/game/fleet.astro index 2fa0298..c1c9cc6 100644 --- a/src/pages/game/fleet.astro +++ b/src/pages/game/fleet.astro @@ -44,7 +44,7 @@ for(const system of userSystems) { } } -const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); +const lang = await getLocales(Astro.cookies.get('language')?.value ?? await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'))); --- diff --git a/src/pages/game/profile.astro b/src/pages/game/profile.astro index b6b06ac..aa9f9df 100644 --- a/src/pages/game/profile.astro +++ b/src/pages/game/profile.astro @@ -2,10 +2,28 @@ import Layout from '../../layouts/Layout.astro'; import NavBar from '../../components/NavBar.astro'; import { getUserByAccessToken, getUserByNickOrEmail } from '../../lib/db/users'; -import { getHighestWeightedLanguage, getLocales, getName } from '../../lib/utils/langDriver'; +import { getHighestWeightedLanguage, getLocales, getName, getSupportedLanguages } from '../../lib/utils/langDriver'; import ResourceBar from '../../components/ResourceBar.astro'; import format from '../../lib/utils/format'; +const availableLanguages = await getSupportedLanguages(); + +if(Astro.request.method === "POST") { + const body = await Astro.request.formData(); + const selectedLanguage = body.get('language')?.toString() ?? null; + + if(!selectedLanguage) return Astro.redirect('/profile'); + + if(!availableLanguages.map(l => l.id).includes(selectedLanguage)) return Astro.redirect('/profile'); + + Astro.cookies.set('language', selectedLanguage, { + path: '/', + maxAge: 60 * 60 * 24 * 365, + sameSite: 'lax', + secure: true + }); +} + const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null; const username = Astro.cookies.get('username')?.value ?? ""; if(loggedToken === null || username === "") return Astro.redirect('/logout'); @@ -13,11 +31,14 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout'); const checkUser = await getUserByAccessToken(loggedToken); if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout'); -const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')); +const locale = Astro.cookies.get('language')?.value ?? await getHighestWeightedLanguage(Astro.request.headers.get('accept-language') ?? "en"); const user = await getUserByNickOrEmail(username); const lang = await getLocales(locale); + +const currentLanguage = Astro.cookies.get('language')?.value ?? "en"; + --- @@ -44,6 +65,15 @@ const lang = await getLocales(locale); +
+

{getName(lang, 'general', 'current-language')}: {availableLanguages.find(l => l.id === currentLanguage)?.name ?? "N/A"}

+ + +