Fix language driver for deployment

This commit is contained in:
Aelita4 2024-06-02 14:10:42 +02:00
parent e17bdda081
commit eb796bfa51
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
16 changed files with 33 additions and 26 deletions

View File

@ -0,0 +1,6 @@
[
{
"id": "en",
"name": "English"
}
]

View File

@ -1,5 +1,5 @@
---
import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver';
import { getHighestWeightedLanguage, getLocales } from '../lib/utils/langDriver';
interface Props {
loggedIn: string;
active: string;
@ -19,7 +19,7 @@ interface NavElement {
dropdowns?: Array<NavElement>;
}
const lang = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'navbar');
const lang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'navbar');
const listOfElements: Array<NavElement> = [{
id: "home",

View File

@ -1,12 +1,12 @@
---
import { ObjectId } from 'mongodb';
import { calculateCurrentAvailableResources, getUserResources } from '../lib/utils/resourceManager';
import { getHighestWeightedLanguage, getLocales } from '../lib/lang/langDriver';
import { getHighestWeightedLanguage, getLocales } from '../lib/utils/langDriver';
import resourceTypes from '../lib/data/resources.json';
const resourceLang = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resources');
const resBarLang = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resourcebar');
const resourceLang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resources');
const resBarLang = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'resourcebar');
const resources = await calculateCurrentAvailableResources(new ObjectId(Astro.cookies.get('userid')?.value ?? ''));

View File

@ -1,18 +1,18 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
export async function getSupportedLanguages() {
const metadata: [] = await (await fetch('http://localhost:4321/lang/metadata.json')).json();
export function getSupportedLanguages() {
const supportedLanguages = fs.readdirSync(path.dirname(fileURLToPath(import.meta.url))).filter((file: string) => {
return fs.statSync(path.join(path.dirname(fileURLToPath(import.meta.url)), file)).isDirectory();
const response: Array<string> = [];
metadata.forEach((lang: any) => {
response.push(lang.id);
});
return supportedLanguages;
return response;
}
export function getHighestWeightedLanguage(header: string | null): any {
export async function getHighestWeightedLanguage(header: string | null): Promise<any> {
if(header === null) return 'en';
const supportedLanguages = getSupportedLanguages();
const supportedLanguages = await getSupportedLanguages();
const langs = header.split(',');
let highestWeight = 0;
@ -33,11 +33,12 @@ export function getHighestWeightedLanguage(header: string | null): any {
}
export async function getLocales(language: string, type: string) {
if(!getSupportedLanguages().includes(language)) {
if(!(await getSupportedLanguages()).includes(language)) {
console.log(await getSupportedLanguages(), language)
return null;
}
const lang = (await import(`./${language}/${type}.json`)).default;
const lang = await (await fetch(`http://localhost:4321/lang/${language}/${type}.json`)).json();
const out: any = {};
for(const category in lang) {

View File

@ -2,7 +2,7 @@
import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro';
import { getUserByAccessToken } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import { getHighestWeightedLanguage, getLocales } from '../../lib/utils/langDriver';
import ResourceBar from '../../components/ResourceBar.astro';
// import { calculateCurrentAvailableResources } from '../../lib/utils/resourceManager';
// import { getUserResources } from '../../lib/utils/resourceManager';
@ -18,7 +18,7 @@ if(checkUser === null || checkUser.username !== username) return Astro.redirect(
// const resources = await getUserResources(checkUser._id);
const locale = getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const langResources = await getLocales(locale, 'resources');
const langGame = await getLocales(locale, 'game');

View File

@ -2,7 +2,7 @@
import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro';
import { getAllUsers, getUserByAccessToken } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import { getHighestWeightedLanguage, getLocales } from '../../lib/utils/langDriver';
import format from '../../lib/utils/format';
const loggedToken = Astro.cookies.get('sessionToken')?.value ?? null;
@ -14,7 +14,7 @@ if(checkUser === null || checkUser.username !== username) return Astro.redirect(
const allUsers = await getAllUsers();
const langGame = await getLocales(getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'game');
const langGame = await getLocales(await getHighestWeightedLanguage(Astro.request.headers.get('accept-language')), 'game');
---
<Layout title="Galaxy view">

View File

@ -2,7 +2,7 @@
import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro';
import { getUserByAccessToken, getUserByNickOrEmail } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import { getHighestWeightedLanguage, getLocales } from '../../lib/utils/langDriver';
import ResourceBar from '../../components/ResourceBar.astro';
import format from '../../lib/utils/format';
@ -13,7 +13,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 = getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const user = await getUserByNickOrEmail(username);

View File

@ -4,7 +4,7 @@ import { Icon } from 'astro-icon/components'
import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro';
import { getUserByAccessToken } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import { getHighestWeightedLanguage, getLocales } from '../../lib/utils/langDriver';
import ResourceBar from '../../components/ResourceBar.astro';
type ResearchDetail = { id: string, level: number, requiredResearch: { id: string, level: number}[], cost: { [key: string]: number } };
@ -18,7 +18,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 = getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const langResources = await getLocales(locale, 'resources');
const langGame = await getLocales(locale, 'game');

View File

@ -2,7 +2,7 @@
import Layout from '../../layouts/Layout.astro';
import NavBar from '../../components/NavBar.astro';
import { getUserByAccessToken } from '../../lib/db/users';
import { getHighestWeightedLanguage, getLocales } from '../../lib/lang/langDriver';
import { getHighestWeightedLanguage, getLocales } from '../../lib/utils/langDriver';
import ResourceBar from '../../components/ResourceBar.astro';
import ships from '../../lib/data/ships.json';
@ -14,7 +14,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 = getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const locale = await getHighestWeightedLanguage(Astro.request.headers.get('accept-language'));
const langShips = await getLocales(locale, 'ships');
const playerShips = checkUser.ships;