From d3338699522e78e7b29c135167caf5b4e9289c2b Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Mon, 17 Jan 2022 17:42:47 +0100 Subject: [PATCH] Port the change language callback to the Account controller. --- application/controllers/Account.php | 45 ++++++++++++++++++++++++++- assets/js/http/account_http_client.js | 19 ++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/application/controllers/Account.php b/application/controllers/Account.php index 34f224d3..5041ad63 100644 --- a/application/controllers/Account.php +++ b/application/controllers/Account.php @@ -101,7 +101,7 @@ class Account extends EA_Controller { } /** - * Make sure the username is valid and unique in the database. + * Make sure the username is valid and unique in the database. */ public function validate_username() { @@ -122,4 +122,47 @@ class Account extends EA_Controller { json_exception($e); } } + + /** + * Change system language for current user. + * + * The language setting is stored in session data and retrieved every time the user visits any of the system pages. + */ + public function change_language() + { + try + { + // Check if language exists in the available languages. + + $found = FALSE; + + foreach (config('available_languages') as $lang) + { + if ($lang == request('language')) + { + $found = TRUE; + break; + } + } + + if ( ! $found) + { + throw new Exception('Translations for the given language does not exist (' . request('language') . ').'); + } + + $language = request('language'); + + session(['language' => $language]); + + config(['language' => $language]); + + json_response([ + 'success' => TRUE + ]); + } + catch (Throwable $e) + { + json_exception($e); + } + } } diff --git a/assets/js/http/account_http_client.js b/assets/js/http/account_http_client.js index 33e357ab..d2e44f2e 100644 --- a/assets/js/http/account_http_client.js +++ b/assets/js/http/account_http_client.js @@ -53,8 +53,25 @@ App.Http.Account = (function () { return $.post(url, data); } + /** + * Change language. + * + * @param {String} language + */ + function changeLanguage(language) { + const url = App.Utils.Url.siteUrl('account/change_language'); + + const data = { + csrf_token: App.Vars.csrf_token, + language + }; + + return $.post(url, data); + } + return { save, - validateUsername + validateUsername, + changeLanguage }; })();