From 80b2f1acfceca1ae58d9c19cb770fd8a85292a3e Mon Sep 17 00:00:00 2001 From: garaone Date: Mon, 15 Jul 2024 09:46:24 +0200 Subject: [PATCH] configurable languages --- private/.env.example | 1 + private/app/php/language_controller.php | 23 +++++++++++++++++++---- private/views/settings.php | 22 +++++++--------------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/private/.env.example b/private/.env.example index 336d441..9dbd8e0 100644 --- a/private/.env.example +++ b/private/.env.example @@ -49,3 +49,4 @@ FAVICON_URI="" # Default Language, leave blank to use default language of the user's browser. DEFAULT_LANGUAGE="de_DE" +ACTIVE_LANGUAGES="de_DE en_US es_ES fr_FR it_IT" diff --git a/private/app/php/language_controller.php b/private/app/php/language_controller.php index f308773..ab66841 100644 --- a/private/app/php/language_controller.php +++ b/private/app/php/language_controller.php @@ -5,6 +5,23 @@ require_once BOOTSTRAP_PATH; require_once LIBRARY_PATH . 'csrf.php'; + function activeLanguages() { + + if (file_exists(ENV_FILE_PATH)) { + $env = parse_ini_file(ENV_FILE_PATH); + } + + $activeLanguages = isset($env) ? $env['ACTIVE_LANGUAGES'] : getenv('ACTIVE_LANGUAGES'); + $activeLanguages = explode(' ', $activeLanguages); + + if ( !count( $activeLanguages ) ) { + // default if no active languages are set + return ['de_DE', 'en_US', 'es_ES', 'fr_FR', 'it_IT']; + } + + return $activeLanguages; + } + function setLanguage(){ //LANGUAGE CHANGE... if(isset($_SESSION['language'])){ @@ -22,7 +39,7 @@ function setLanguage(){ } else{ //hard code to german - $languages = ['de_DE', 'en_US', 'es_ES', 'fr_FR', 'it_IT']; + $languages = activeLanguages(); $acceptLang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; $matchingLang = substr($acceptLang, 0, 2); foreach ($languages as $lang) { @@ -44,8 +61,6 @@ function setLanguage(){ $_SESSION['translation'] = $translation; } - - // Check if the request is POST if ($_SERVER['REQUEST_METHOD'] === 'POST') { // CSRF Protection @@ -60,7 +75,7 @@ function setLanguage(){ } // Check if the requested language is valid - $languages = ['de_DE', 'en_US', 'es_ES', 'fr_FR', 'it_IT']; + $languages = activeLanguages(); $jsonString = file_get_contents("php://input"); diff --git a/private/views/settings.php b/private/views/settings.php index 26092a1..dba5a3c 100644 --- a/private/views/settings.php +++ b/private/views/settings.php @@ -28,21 +28,13 @@

- - DE - - - EN - - - ES - - - FR - - - IT - + ' . $langShort . ''; + } + ?>