<?php
namespace ticky;
class lang {
private static $tlang = [];
private static $range = 'zh-cn';
protected static $tlangDetectVar = 'lang';
protected static $tlangCookieVar = 'ticky_var';
protected static $tlangCookieExpire = 3600;
protected static $allowLangList = [];
protected static $acceptLanguage = ['zh-hans-cn' => 'zh-cn'];
public static function range($range = '') {
if ($range) {
self::$range = $range;
}
return self::$range;
}
public static function set($name, $value = null, $range = '') {
$range = $range ?: self::$range;
if (!isset(self::$tlang[$range])) {
self::$tlang[$range] = [];
}
if (is_array($name)) {
return self::$tlang[$range] = array_change_key_case($name) + self::$tlang[$range];
}
return self::$tlang[$range][strtolower($name)] = $value;
}
public static function load($file, $range = '') {
$range = $range ?: self::$range;
$file = is_string($file) ? [$file] : $file;
if (!isset(self::$tlang[$range])) {
self::$tlang[$range] = [];
}
$tlang = [];
foreach ($file as $_file) {
if (is_file($_file)) {
$_lang = include $_file;
if (is_array($_lang)) {
$tlang = array_change_key_case($_lang) + $tlang;
}
}
}
if (!empty($tlang)) {
self::$tlang[$range] = $tlang + self::$tlang[$range];
}
return self::$tlang[$range];
}
public static function has($name, $range = '') {
$range = $range ?: self::$range;
return isset(self::$tlang[$range][strtolower($name)]);
}
public static function get($name = null, $vars = [], $range = '') {
$range = $range ?: self::$range;
if (empty($name)) {
return self::$tlang[$range];
}
$key = strtolower($name);
$value = isset(self::$tlang[$range][$key]) ? self::$tlang[$range][$key] : $name;
if (!empty($vars) && is_array($vars)) {
if (key($vars) === 0) {
array_unshift($vars, $value);
$value = call_user_func_array('sprintf', $vars);
} else {
$replace = array_keys($vars);
foreach ($replace as &$v) {
$v = "{:{$v}}";
}
$value = str_replace($replace, $vars, $value);
}
}
return $value;
}
public static function detect() {
$tlangSet = '';
if (isset($_GET[self::$tlangDetectVar])) {
$tlangSet = strtolower($_GET[self::$tlangDetectVar]);
} elseif (isset($_COOKIE[self::$tlangCookieVar])) {
$tlangSet = strtolower($_COOKIE[self::$tlangCookieVar]);
} elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
$tlangSet = strtolower($matches[1]);
$acceptLangs = Config::get('header_accept_lang');
if (isset($acceptLangs[$tlangSet])) {
$tlangSet = $acceptLangs[$tlangSet];
} elseif (isset(self::$acceptLanguage[$tlangSet])) {
$tlangSet = self::$acceptLanguage[$tlangSet];
}
}
if (empty(self::$allowLangList) || in_array($tlangSet, self::$allowLangList)) {
self::$range = $tlangSet ?: self::$range;
}
return self::$range;
}
public static function setLangDetectVar($var) {
self::$tlangDetectVar = $var;
}
public static function setLangCookieVar($var) {
self::$tlangCookieVar = $var;
}
public static function setLangCookieExpire($expire) {
self::$tlangCookieExpire = $expire;
}
public static function setAllowLangList($list) {
self::$allowLangList = $list;
}
}