2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Http\Request;
|
2026-02-23 12:58:19 +01:00
|
|
|
use MintyPHP\I18n;
|
2026-02-04 23:31:53 +01:00
|
|
|
use MintyPHP\Router;
|
2026-02-23 12:58:19 +01:00
|
|
|
use MintyPHP\Service\User\UserServicesFactory;
|
|
|
|
|
use MintyPHP\Support\Flash;
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$rawLocale = (string) ($_REQUEST['locale'] ?? (requestInput()->queryAll()['lang'] ?? ''));
|
2026-02-04 23:31:53 +01:00
|
|
|
$locale = strtolower(trim($rawLocale));
|
|
|
|
|
if ($locale !== '' && strpos($locale, '-') !== false) {
|
|
|
|
|
$locale = explode('-', $locale, 2)[0];
|
|
|
|
|
}
|
|
|
|
|
$locales = defined('APP_LOCALES') ? APP_LOCALES : [I18n::$defaultLocale];
|
|
|
|
|
|
|
|
|
|
if ($locale === '' || !in_array($locale, $locales, true)) {
|
|
|
|
|
Flash::error('Language not supported', null, 'lang_not_supported');
|
|
|
|
|
Router::redirect('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
I18n::$locale = $locale;
|
|
|
|
|
if (isset($_SESSION['user']['id'])) {
|
|
|
|
|
$userId = (int) ($_SESSION['user']['id'] ?? 0);
|
|
|
|
|
if ($userId) {
|
2026-03-04 15:56:58 +01:00
|
|
|
(app(UserServicesFactory::class))->createUserAccountService()->setLocale($userId, $locale);
|
2026-02-04 23:31:53 +01:00
|
|
|
$_SESSION['user']['locale'] = $locale;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
setcookie('locale', $locale, time() + 31536000, '/', '', false, true);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$target = Request::safeReturnTarget(requestInput()->queryAll()['return'] ?? '');
|
2026-02-04 23:31:53 +01:00
|
|
|
$target = Request::stripLocale($target);
|
|
|
|
|
$target = Request::withLocale($target, $locale);
|
|
|
|
|
Router::redirect($target);
|