S2OJ/web/app/models/UOJLocale.php

65 lines
1.7 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
class UOJLocale {
2022-11-06 02:26:21 +00:00
public static $supported_locales = ['zh-cn', 'en'];
public static $supported_modules = ['basic', 'contests', 'problems', 'time', 'user'];
public static $data = [];
public static $required = [];
public static function init() {
if (UOJRequest::get('locale')) {
UOJLocale::setLocale(UOJRequest::get('locale'));
2022-11-18 05:41:17 +00:00
redirectTo(UOJContext::requestPath());
2022-11-06 02:26:21 +00:00
}
self::requireModule('basic');
}
2016-07-18 16:39:37 +00:00
public static function locale() {
$locale = Cookie::get('uoj_locale');
if ($locale != null && !in_array($locale, self::$supported_locales)) {
$locale = null;
Cookie::unsetVar('uoj_locale', '/');
}
if ($locale == null) {
$locale = 'zh-cn';
}
return $locale;
}
public static function setLocale($locale) {
if (!in_array($locale, self::$supported_locales)) {
return false;
}
return Cookie::set('uoj_locale', $locale, time() + 60 * 60 * 24 * 365 * 10, '/');
}
public static function requireModule($name) {
if (in_array($name, self::$required)) {
return;
}
$required[] = $name;
2022-11-06 02:26:21 +00:00
$data = include($_SERVER['DOCUMENT_ROOT'] . '/app/locale/' . $name . '/' . self::locale() . '.php');
2016-07-18 16:39:37 +00:00
$pre = $name == 'basic' ? '' : "$name::";
foreach ($data as $key => $val) {
2022-11-06 02:26:21 +00:00
self::$data[$pre . $key] = $val;
2016-07-18 16:39:37 +00:00
}
}
public static function get($name) {
if (!isset(self::$data[$name])) {
$module_name = strtok($name, '::');
if (!in_array($module_name, self::$supported_modules)) {
return false;
}
self::requireModule($module_name);
}
if (!isset(self::$data[$name])) {
return false;
}
if (is_string(self::$data[$name])) {
return self::$data[$name];
} else {
return call_user_func_array(self::$data[$name], array_slice(func_get_args(), 1));
}
}
}