Add language cookie and switch
This commit is contained in:
parent
2f96c08da3
commit
38960ef308
|
@ -11,7 +11,7 @@ interface Props {
|
||||||
button_name: string;
|
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')));
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ interface NavElement {
|
||||||
dropdowns?: Array<NavElement>;
|
dropdowns?: Array<NavElement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
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<NavElement> = [{
|
const listOfElements: Array<NavElement> = [{
|
||||||
id: "home",
|
id: "home",
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { Resource } from '../lib/classes/managers/ResourceManager';
|
||||||
|
|
||||||
const resourceTypes = await getAllResources();
|
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 ?? '');
|
const planetId = new ObjectId(Astro.cookies.get('planetid')?.value ?? '');
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,23 @@
|
||||||
---
|
---
|
||||||
import PlanetView from '../components/PlanetView.astro';
|
import PlanetView from '../components/PlanetView.astro';
|
||||||
|
import { getHighestWeightedLanguage } from '../lib/utils/langDriver';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
title: string;
|
title: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { title } = Astro.props;
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
---
|
---
|
||||||
|
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
|
|
|
@ -1,15 +1,7 @@
|
||||||
import { GET as langs } from '../../pages/api/lang.json';
|
import { GET as langs } from '../../pages/api/lang.json';
|
||||||
|
|
||||||
export async function getSupportedLanguages() {
|
export async function getSupportedLanguages() {
|
||||||
const metadata: { id: string, name: string }[] = await (await langs()).json();
|
return await (await langs()).json() as { id: string, name: string }[]
|
||||||
|
|
||||||
const response: Array<string> = [];
|
|
||||||
|
|
||||||
metadata.forEach((lang: any) => {
|
|
||||||
response.push(lang.id);
|
|
||||||
});
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getHighestWeightedLanguage(header: string | null): Promise<any> {
|
export async function getHighestWeightedLanguage(header: string | null): Promise<any> {
|
||||||
|
@ -25,7 +17,7 @@ export async function getHighestWeightedLanguage(header: string | null): Promise
|
||||||
const langName = subArray[0];
|
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;
|
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;
|
highestWeight = langWeight;
|
||||||
highestWeightedLang = langName;
|
highestWeightedLang = langName;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +27,7 @@ export async function getHighestWeightedLanguage(header: string | null): Promise
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLocales(language: string) {
|
export async function getLocales(language: string) {
|
||||||
if(!(await getSupportedLanguages()).includes(language)) {
|
if(!(await getSupportedLanguages()).find(l => l.id === language)) {
|
||||||
console.log(await getSupportedLanguages(), language)
|
console.log(await getSupportedLanguages(), language)
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ if(loggedToken === null || username === "") return Astro.redirect('/logout');
|
||||||
const checkUser = await getUserByAccessToken(loggedToken);
|
const checkUser = await getUserByAccessToken(loggedToken);
|
||||||
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout');
|
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);
|
const lang = await getLocales(locale);
|
||||||
|
|
||||||
|
|
|
@ -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')));
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title="Fleet">
|
<Layout title="Fleet">
|
||||||
|
|
|
@ -2,10 +2,28 @@
|
||||||
import Layout from '../../layouts/Layout.astro';
|
import Layout from '../../layouts/Layout.astro';
|
||||||
import NavBar from '../../components/NavBar.astro';
|
import NavBar from '../../components/NavBar.astro';
|
||||||
import { getUserByAccessToken, getUserByNickOrEmail } from '../../lib/db/users';
|
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 ResourceBar from '../../components/ResourceBar.astro';
|
||||||
import format from '../../lib/utils/format';
|
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 loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
|
||||||
const username = Astro.cookies.get('username')?.value ?? "";
|
const username = Astro.cookies.get('username')?.value ?? "";
|
||||||
if(loggedToken === null || username === "") return Astro.redirect('/logout');
|
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);
|
const checkUser = await getUserByAccessToken(loggedToken);
|
||||||
if(checkUser === null || checkUser.username !== username) return Astro.redirect('/logout');
|
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 user = await getUserByNickOrEmail(username);
|
||||||
|
|
||||||
const lang = await getLocales(locale);
|
const lang = await getLocales(locale);
|
||||||
|
|
||||||
|
const currentLanguage = Astro.cookies.get('language')?.value ?? "en";
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title="Profile">
|
<Layout title="Profile">
|
||||||
|
@ -44,6 +65,15 @@ const lang = await getLocales(locale);
|
||||||
<input class="data-form-input" type="password" name="password2" placeholder={getName(lang, 'general', 'input-placeholder-new-password-verify')} />
|
<input class="data-form-input" type="password" name="password2" placeholder={getName(lang, 'general', 'input-placeholder-new-password-verify')} />
|
||||||
<input class="data-form-button" type="button" value={getName(lang, 'general', 'change-password')} />
|
<input class="data-form-button" type="button" value={getName(lang, 'general', 'change-password')} />
|
||||||
</form>
|
</form>
|
||||||
|
<form id="changeLanguageForm" class="data-form" method="post">
|
||||||
|
<h4>{getName(lang, 'general', 'current-language')}: {availableLanguages.find(l => l.id === currentLanguage)?.name ?? "N/A"}</h4>
|
||||||
|
<select class="data-form-input" name="language">
|
||||||
|
{availableLanguages.map((lang) => (
|
||||||
|
<option value={lang.id}>{lang.name}</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
<input type="submit" class="data-form-button" value={getName(lang, 'general', 'change-language')} />
|
||||||
|
</form>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
|
@ -19,7 +19,7 @@ if(checkUser === null || checkUser.username !== username) return Astro.redirect(
|
||||||
const planetId = Astro.cookies.get('planetid')?.value ?? "";
|
const planetId = Astro.cookies.get('planetid')?.value ?? "";
|
||||||
if(planetId === "") return Astro.redirect('/logout');
|
if(planetId === "") 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);
|
const lang = await getLocales(locale);
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ if(!planet) {
|
||||||
return Astro.redirect('/logout');
|
return Astro.redirect('/logout');
|
||||||
}
|
}
|
||||||
|
|
||||||
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 modalSet: { [key: string]: { resources: Array<any>, research: Array<any>, buildings: Array<any> } } = {};
|
const modalSet: { [key: string]: { resources: Array<any>, research: Array<any>, buildings: Array<any> } } = {};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue