diff --git a/api.php b/api.php index 9d45619..ba8cec4 100644 --- a/api.php +++ b/api.php @@ -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); diff --git a/common/strings.php b/common/strings.php index 93333e7..0fb0606 100644 --- a/common/strings.php +++ b/common/strings.php @@ -1,5 +1,33 @@ '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 { diff --git a/index.php b/index.php index 2ad2cf6..3ca2614 100644 --- a/index.php +++ b/index.php @@ -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'; } ]; diff --git a/scripts/langauge_switcher.js b/scripts/langauge_switcher.js new file mode 100644 index 0000000..185eaab --- /dev/null +++ b/scripts/langauge_switcher.js @@ -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}`; +} \ No newline at end of file diff --git a/strings b/strings index 919271a..d9131af 160000 --- a/strings +++ b/strings @@ -1 +1 @@ -Subproject commit 919271adb8e055d09f08898b812f9e641f693543 +Subproject commit d9131afc20afc0bb4205990bb34f8c455ae81f8d