* Add feed widget * add feed render * Add CommentPage widget * New theme (#1390) * 调整忽略目录 * add theme * fix theme scss build Co-authored-by: fen <f3nb0x@gmail.com> * s/is_writeable/is_writable/g * New upgrade method * merge new fixes from master * add pgsql ssl mode support (ref #1600) (#1623) * Feat/code refactor (#1626) * remove all magic methods, add type for class properties * refactor codes * fix all * refactor code * fix type * fix all * fix request is method * fix all * fix router * fix get page * fix 1.3.0 upgrade * [feat] support high resolution avatar * fix types in i18n component * Implement Ctrl+S or Command+S for save draft (#1628) * Implement Ctrl+S or Command+S for save draft * rename * add Typecho.savePost * fix upload file size * add new uploader * replace new uploader * fix textarea change * fix preview * refactor post edit * fix issue * fix page edit --------- Co-authored-by: joyqi <joyqi@segmentfault.com> Co-authored-by: joyqi <magike.net@gmail.com> * fix #1632 * Add svg to image types * Feat/tree pages (#1646) * add tree trait * finish category tree trait * support select fields * fix select fields * refactor admin trait * fix draft status * Add new contents type "revision" * minor refactor * add more tree view abstracts * add tree trait to pages * get ready for tree view pages * improve page edit * fix revision * fix slug * add router params delegate * fix params delegate * fix * fix * fix all * fix all * fix tree * fix page link * fix feed * fix page * fix permalink * fix permalink input * fix offset query * Support IDN (#1629) * Support IDN * use js * Optimize code * Optimize code * fix URL script * remove unnecessary use --------- Co-authored-by: joyqi <joyqi@segmentfault.com> * fix input element * fix #1651, close #1653 * Use json instead of serialize (#1624) * Use json instead of serialize * Fix Upgrade code * add tree trait * finish category tree trait * support select fields * fix select fields * refactor admin trait * fix draft status * Add new contents type "revision" * minor refactor * add more tree view abstracts * add tree trait to pages * get ready for tree view pages * improve page edit * fix revision * fix slug * add router params delegate * fix params delegate * fix * fix * fix all * fix all * fix tree * fix page link * fix feed * fix page * fix permalink * fix permalink input * fix offset query * Fix typo * remove proxy methods * remove unnecessary useage --------- Co-authored-by: joyqi <joyqi@segmentfault.com> Co-authored-by: joyqi <magike.net@gmail.com> * Fix Prevent XSS vulnerability in default theme (#1654) * Fix Prevent XSS vulnerability in default theme * Update var/Typecho/Db/Adapter/Pdo.php * fix the getter --------- Co-authored-by: joyqi <joyqi@segmentfault.com> * add throwCallback to widget response * fix: cut down fields when selecting recent posts * fix typo errors * fix typo errors * fix http client cookie * add throw finish * fix theme lang * fix default theme * fix query * add open graph and twitter card support add canonical link * fix canonical link meta * fix theme classic-22 * remove unnecessary scss file when packaging * init plugin signal * improve: remove feather-icon js file * fix: typo * improve: post detail layout * fix tags saving * improve: nav search * fix: theme screenshot * fix: theme page layout * remove php 7.2/7.3 env --------- Co-authored-by: fen <f3nb0x@gmail.com> Co-authored-by: Lu Fei <52o@qq52o.cn>
170 lines
4.0 KiB
PHP
170 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace Typecho;
|
|
|
|
use Typecho\I18n\GetTextMulti;
|
|
|
|
/**
|
|
* 国际化字符翻译
|
|
*
|
|
* @package I18n
|
|
*/
|
|
class I18n
|
|
{
|
|
/**
|
|
* 是否已经载入的标志位
|
|
*
|
|
* @access private
|
|
* @var GetTextMulti|null
|
|
*/
|
|
private static ?GetTextMulti $loaded = null;
|
|
|
|
/**
|
|
* 语言文件
|
|
*
|
|
* @access private
|
|
* @var string|null
|
|
*/
|
|
private static ?string $lang = null;
|
|
|
|
/**
|
|
* 翻译文字
|
|
*
|
|
* @access public
|
|
*
|
|
* @param string $string 待翻译的文字
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function translate(string $string): string
|
|
{
|
|
self::init();
|
|
return self::$loaded ? self::$loaded->translate($string) : $string;
|
|
}
|
|
|
|
/**
|
|
* 初始化语言文件
|
|
*
|
|
* @access private
|
|
*/
|
|
private static function init()
|
|
{
|
|
/** GetText支持 */
|
|
if (!isset(self::$loaded) && self::$lang && file_exists(self::$lang)) {
|
|
self::$loaded = new GetTextMulti(self::$lang);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 针对复数形式的翻译函数
|
|
*
|
|
* @param string $single 单数形式的翻译
|
|
* @param string $plural 复数形式的翻译
|
|
* @param integer $number 数字
|
|
* @return string
|
|
*/
|
|
public static function ngettext(string $single, string $plural, int $number): string
|
|
{
|
|
self::init();
|
|
return self::$loaded ? self::$loaded->ngettext($single, $plural, $number) : ($number > 1 ? $plural : $single);
|
|
}
|
|
|
|
/**
|
|
* 词义化时间
|
|
*
|
|
* @access public
|
|
*
|
|
* @param int $from 起始时间
|
|
* @param int $now 终止时间
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function dateWord(int $from, int $now): string
|
|
{
|
|
$between = $now - $from;
|
|
|
|
/** 如果是一天 */
|
|
if ($between >= 0 && $between < 86400 && date('d', $from) == date('d', $now)) {
|
|
/** 如果是一小时 */
|
|
if ($between < 3600) {
|
|
/** 如果是一分钟 */
|
|
if ($between < 60) {
|
|
if (0 == $between) {
|
|
return _t('刚刚');
|
|
} else {
|
|
return str_replace('%d', $between, _n('一秒前', '%d秒前', $between));
|
|
}
|
|
}
|
|
|
|
$min = floor($between / 60);
|
|
return str_replace('%d', $min, _n('一分钟前', '%d分钟前', $min));
|
|
}
|
|
|
|
$hour = floor($between / 3600);
|
|
return str_replace('%d', $hour, _n('一小时前', '%d小时前', $hour));
|
|
}
|
|
|
|
/** 如果是昨天 */
|
|
if (
|
|
$between > 0
|
|
&& $between < 172800
|
|
&& (date('z', $from) + 1 == date('z', $now) // 在同一年的情况
|
|
|| date('z', $from) + 1 == date('L') + 365 + date('z', $now))
|
|
) { // 跨年的情况
|
|
return _t('昨天 %s', date('H:i', $from));
|
|
}
|
|
|
|
/** 如果是一个星期 */
|
|
if ($between > 0 && $between < 604800) {
|
|
$day = floor($between / 86400);
|
|
return str_replace('%d', $day, _n('一天前', '%d天前', $day));
|
|
}
|
|
|
|
/** 如果是 */
|
|
if (date('Y', $from) == date('Y', $now)) {
|
|
return date(_t('n月j日'), $from);
|
|
}
|
|
|
|
return date(_t('Y年m月d日'), $from);
|
|
}
|
|
|
|
/**
|
|
* 增加语言项
|
|
*
|
|
* @access public
|
|
*
|
|
* @param string $lang 语言名称
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function addLang(string $lang)
|
|
{
|
|
self::$loaded->addFile($lang);
|
|
}
|
|
|
|
/**
|
|
* 获取语言项
|
|
*
|
|
* @access public
|
|
* @return string
|
|
*/
|
|
public static function getLang(): ?string
|
|
{
|
|
return self::$lang;
|
|
}
|
|
|
|
/**
|
|
* 设置语言项
|
|
*
|
|
* @access public
|
|
*
|
|
* @param string $lang 配置信息
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function setLang(string $lang)
|
|
{
|
|
self::$lang = $lang;
|
|
}
|
|
}
|