Port the change language callback to the Account controller.

This commit is contained in:
Alex Tselegidis 2022-01-17 17:42:47 +01:00
parent cd6ec7400c
commit d333869952
2 changed files with 62 additions and 2 deletions

View file

@ -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);
}
}
}

View file

@ -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
};
})();