I really dont like this but I will fix it later

This commit is contained in:
Bye 2024-07-05 12:04:25 +01:00
parent ea29820334
commit f61c97744c
5 changed files with 125 additions and 11 deletions

17
api.php
View file

@ -8,6 +8,23 @@ $routes = [
exit();
},
'i11n' => function () {
global $path;
return match ($path[2]) {
'languages' => [
"response" => [
'success' => true,
'status_code' => 200
],
'body' => [
'current' => $_SESSION['lang'],
'languages' => LANGAUGES
]
],
default => 404,
};
},
'status' => function () {
http_response_code(200);

View file

@ -1,5 +1,33 @@
<?php
const LANGAUGES = [
[
"code" => 'en',
"name" => "English (Traditional)",
"flag" => "uk"
],
[
'code' => 'en_US',
"name" => 'English (Simplified)',
'flag' => 'usa'
],
[
'code' => 'en_UWU',
'name' => 'Cute English',
'flag' => 'owo'
],
[
'code' => 'ga',
'name' => 'Irish',
'flag' => 'ie'
],
[
'code' => 'nb_NO',
'name' => 'Norwegian Bokmål',
'flag' => 'no'
]
];
function get_string($key="generic.generic", $substitutes=[]) {
global $LANG;
@ -27,6 +55,27 @@ function get_string($key="generic.generic", $substitutes=[]) {
return $result;
}
function get_language_code_based_on_browser_locale(): string
{
$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locales = [ // Converts locale to its respective language.
'en_GB' => 'en',
'en_IE' => 'en',
'en' => 'en',
];
if (array_key_exists($locale, $locales)) {
return $locales[$locale];
}
if (str_starts_with("en", $locale)) {
return "en";
}
return $locale;
}
function patch_lang($lang="en"): void
{

View file

@ -25,11 +25,19 @@ require_once 'common/strings.php';
require_once 'common/account_utils.php';
require_once 'common/database.php';
// Starts the session
// TODO: write this to use the database to work across more than one server (e.g. don't use PHP sessions)
session_start();
if (empty($_SESSION)) {
$_SESSION['auth'] = false;
$_SESSION['id'] = null;
}
if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = get_language_code_based_on_browser_locale();
}
$user = null;
if ($_SESSION['auth']) {
@ -61,6 +69,14 @@ if (str_ends_with($path_raw, '/') && $path_raw != '/') {
exit;
}
// If there's a 'lang' query param, change the language!
if (array_key_exists('lang', $query)) {
$_SESSION['lang'] = $query['lang'];
}
patch_lang($_SESSION['lang']);
$routes = [
'' => function () { require 'views/home.php'; },
'api' => function () {
@ -85,6 +101,17 @@ $routes = [
}
exit();
},
'dashboard' => function () {
global $user;
requires_auth();
if (isset($path[2])) {
return 404;
}
require 'views/dashboard.php';
return 200;
},
'profile' => function () {
global $path, $user, $profile_owner; // don't forget this lol
@ -101,15 +128,8 @@ $routes = [
require 'views/profile.php';
return 200;
},
'dashboard' => function () {
requires_auth();
if (isset($path[2])) {
return 404;
}
require 'views/dashboard.php';
return 200;
'settings' => function () {
require 'views/settings.php';
}
];

View file

@ -0,0 +1,28 @@
const select = document.createElement('select');
const script = document.scripts[document.scripts.length - 1];
const langs_req = fetch('/api/i11n/languages')
.then(async data => {
return await data.json();
})
.then(async json => {
console.log(json)
for (const lang of json.body.languages) {
console.log(lang)
let new_opt = document.createElement('option');
new_opt.value = lang.code;
new_opt.innerText = lang.name;
select.appendChild(new_opt);
}
select.value = json.body.current;
script.parentElement.insertBefore(select, script);
})
let separator = (window.location.href.indexOf("?")===-1)?"?":"&";
select.onchange = function () {
window.location.href = window.location.href + separator + `lang=${select.value}`;
}

@ -1 +1 @@
Subproject commit 919271adb8e055d09f08898b812f9e641f693543
Subproject commit d9131afc20afc0bb4205990bb34f8c455ae81f8d