I18n.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Typecho;
  3. use Typecho\I18n\GetTextMulti;
  4. /**
  5. * 国际化字符翻译
  6. *
  7. * @package I18n
  8. */
  9. class I18n
  10. {
  11. /**
  12. * 是否已经载入的标志位
  13. *
  14. * @access private
  15. * @var GetTextMulti
  16. */
  17. private static $loaded;
  18. /**
  19. * 语言文件
  20. *
  21. * @access private
  22. * @var string
  23. */
  24. private static $lang = null;
  25. /**
  26. * 翻译文字
  27. *
  28. * @access public
  29. *
  30. * @param string $string 待翻译的文字
  31. *
  32. * @return string
  33. */
  34. public static function translate(string $string): string
  35. {
  36. self::init();
  37. return self::$loaded ? self::$loaded->translate($string) : $string;
  38. }
  39. /**
  40. * 初始化语言文件
  41. *
  42. * @access private
  43. */
  44. private static function init()
  45. {
  46. /** GetText支持 */
  47. if (!isset(self::$loaded) && self::$lang && file_exists(self::$lang)) {
  48. self::$loaded = new GetTextMulti(self::$lang);
  49. }
  50. }
  51. /**
  52. * 针对复数形式的翻译函数
  53. *
  54. * @param string $single 单数形式的翻译
  55. * @param string $plural 复数形式的翻译
  56. * @param integer $number 数字
  57. * @return string
  58. */
  59. public static function ngettext(string $single, string $plural, int $number): string
  60. {
  61. self::init();
  62. return self::$loaded ? self::$loaded->ngettext($single, $plural, $number) : ($number > 1 ? $plural : $single);
  63. }
  64. /**
  65. * 词义化时间
  66. *
  67. * @access public
  68. *
  69. * @param int $from 起始时间
  70. * @param int $now 终止时间
  71. *
  72. * @return string
  73. */
  74. public static function dateWord(int $from, int $now): string
  75. {
  76. $between = $now - $from;
  77. /** 如果是一天 */
  78. if ($between >= 0 && $between < 86400 && date('d', $from) == date('d', $now)) {
  79. /** 如果是一小时 */
  80. if ($between < 3600) {
  81. /** 如果是一分钟 */
  82. if ($between < 60) {
  83. if (0 == $between) {
  84. return _t('刚刚');
  85. } else {
  86. return str_replace('%d', $between, _n('一秒前', '%d秒前', $between));
  87. }
  88. }
  89. $min = floor($between / 60);
  90. return str_replace('%d', $min, _n('一分钟前', '%d分钟前', $min));
  91. }
  92. $hour = floor($between / 3600);
  93. return str_replace('%d', $hour, _n('一小时前', '%d小时前', $hour));
  94. }
  95. /** 如果是昨天 */
  96. if (
  97. $between > 0
  98. && $between < 172800
  99. && (date('z', $from) + 1 == date('z', $now) // 在同一年的情况
  100. || date('z', $from) + 1 == date('L') + 365 + date('z', $now))
  101. ) { // 跨年的情况
  102. return _t('昨天 %s', date('H:i', $from));
  103. }
  104. /** 如果是一个星期 */
  105. if ($between > 0 && $between < 604800) {
  106. $day = floor($between / 86400);
  107. return str_replace('%d', $day, _n('一天前', '%d天前', $day));
  108. }
  109. /** 如果是 */
  110. if (date('Y', $from) == date('Y', $now)) {
  111. return date(_t('n月j日'), $from);
  112. }
  113. return date(_t('Y年m月d日'), $from);
  114. }
  115. /**
  116. * 增加语言项
  117. *
  118. * @access public
  119. *
  120. * @param string $lang 语言名称
  121. *
  122. * @return void
  123. */
  124. public static function addLang(string $lang)
  125. {
  126. self::$loaded->addFile($lang);
  127. }
  128. /**
  129. * 获取语言项
  130. *
  131. * @access public
  132. * @return string
  133. */
  134. public static function getLang(): ?string
  135. {
  136. return self::$lang;
  137. }
  138. /**
  139. * 设置语言项
  140. *
  141. * @access public
  142. *
  143. * @param string $lang 配置信息
  144. *
  145. * @return void
  146. */
  147. public static function setLang(string $lang)
  148. {
  149. self::$lang = $lang;
  150. }
  151. }