Files
mrbs-equbao-2026/web/lib/MRBS/Intl/LocaleSwitcher.php
T
人事系统开发 48092cab42
Docker image / push (push) Canceled after 0s
MRBS 1.12.2 等保2.0二级整改完整提交
包含:登录失败锁定、90天密码有效期、30分钟会话超时、
强制改密、登录审计日志、屏幕水印、企业背景图、
备案信息固定底部、favicon、登录页JS修复等全部改动
2026-09-08 21:19:47 +08:00

55 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace MRBS\Intl;
use MRBS\Language;
use MRBS\System;
class LocaleSwitcher
{
private $category;
private $locale;
private $old_locale;
public function __construct(int $category, string $locale)
{
$this->category = $category;
$this->locale = $locale;
}
public function switch()
{
// It's not worth testing whether the new locale is the same as the old one, thereby saving us setting
// the locale again. That's because setting the locale on Unix systems seems to be about 100 times
// faster than on Windows. So on Unix systems, it's not worth worrying about. And on Windows, we have
// to set the locale again anyway in case another script running in the same process has changed the
// locale since we first set it. See the warning on the PHP manual page for setlocale():
//
// "The locale information is maintained per process, not per thread. If you are running PHP on a
// multithreaded server API like IIS or Apache on Windows, you may experience sudden changes in locale
// settings while a script is running, though the script itself never called setlocale(). This happens
// due to other scripts running in different threads of the same process at the same time, changing the
// process-wide locale using setlocale()."
$this->old_locale = setlocale($this->category, '0');
Language::setLocale($this->category, $this->locale);
}
public function restore()
{
if (!isset($this->old_locale))
{
throw new \RuntimeException("switch() must be called before restore().");
}
if (false === setlocale($this->category, $this->old_locale))
{
// Shouldn't happen as the old locale was what the system told us it was.
throw new \RuntimeException("Could not restore locale to '" . $this->old_locale . "'");
}
}
}