PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/var/Typecho/I18n.php

http://typecho.googlecode.com/
PHP | 203 lines | 84 code | 19 blank | 100 comment | 32 complexity | 416bdf8cd9b23cade0dbf83cc2aa54e2 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Typecho Blog Platform
  4. *
  5. * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
  6. * @license GNU General Public License 2.0
  7. * @version $Id: I18n.php 106 2008-04-11 02:23:54Z magike.net $
  8. */
  9. /**
  10. * I18n function
  11. *
  12. * @param string $string ???????
  13. * @return string
  14. */
  15. function _t($string)
  16. {
  17. if (func_num_args() <= 1) {
  18. return Typecho_I18n::translate($string);
  19. } else {
  20. $args = func_get_args();
  21. array_shift($args);
  22. return vsprintf(Typecho_I18n::translate($string), $args);
  23. }
  24. }
  25. /**
  26. * I18n function, translate and echo
  27. *
  28. * @param string $string ??????????
  29. * @return void
  30. */
  31. function _e()
  32. {
  33. $args = func_get_args();
  34. echo call_user_func_array('_t', $args);
  35. }
  36. /**
  37. * ???????????
  38. *
  39. * @param string $single ???????
  40. * @param string $plural ???????
  41. * @param integer $number ??
  42. * @return string
  43. */
  44. function _n($single, $plural, $number)
  45. {
  46. return Typecho_I18n::ngettext($single, $plural, $number);
  47. }
  48. /**
  49. * ???????
  50. *
  51. * @package I18n
  52. */
  53. class Typecho_I18n
  54. {
  55. /**
  56. * ??????????
  57. *
  58. * @access private
  59. * @var boolean
  60. */
  61. private static $_loaded = false;
  62. /**
  63. * ????
  64. *
  65. * @access private
  66. * @var string
  67. */
  68. private static $_lang = NULL;
  69. /**
  70. * ???????
  71. *
  72. * @access private
  73. * @return boolean
  74. */
  75. private static function init()
  76. {
  77. /** GetText?? */
  78. require_once 'Typecho/I18n/GetTextMulti.php';
  79. self::$_loaded = new Typecho_I18n_GetTextMulti(self::$_lang);
  80. return true;
  81. }
  82. /**
  83. * ????
  84. *
  85. * @access public
  86. * @param string $string ??????
  87. * @return string
  88. */
  89. public static function translate($string)
  90. {
  91. self::$_lang && empty(self::$_loaded) && self::init();
  92. return self::$_lang ? self::$_loaded->translate($string) : $string;
  93. }
  94. /**
  95. * ???????????
  96. *
  97. * @param string $single ???????
  98. * @param string $plural ???????
  99. * @param integer $number ??
  100. * @return string
  101. */
  102. public static function ngettext($single, $plural, $number)
  103. {
  104. self::$_lang && empty(self::$_loaded) && self::init();
  105. return self::$_lang ? self::$_loaded->ngettext($single, $plural, $number) : ($number > 1 ? $plural : $single);
  106. }
  107. /**
  108. * ?????
  109. *
  110. * @access public
  111. * @param string $from ????
  112. * @param string $now ????
  113. * @return string
  114. */
  115. public static function dateWord($from, $now)
  116. {
  117. $between = $now - $from;
  118. /** ????? */
  119. if ($between > 0 && $between < 86400 && idate('d', $from) == idate('d', $now)) {
  120. /** ?????? */
  121. if ($between < 3600 && idate('H', $from) == idate('H', $now)) {
  122. /** ?????? */
  123. if ($between < 60 && idate('i', $from) == idate('i', $now)) {
  124. $second = idate('s', $now) - idate('s', $from);
  125. if (0 == $second) {
  126. return _t('??');
  127. } else {
  128. return sprintf(_n('%d??', '%d??', $second), $second);
  129. }
  130. }
  131. $min = idate('i', $now) - idate('i', $from);
  132. return sprintf(_n('%d???', '%d???', $min), $min);
  133. }
  134. $hour = idate('H', $now) - idate('H', $from);
  135. return sprintf(_n('%d???', '%d???', $hour), $hour);
  136. }
  137. /** ????? */
  138. if ($between > 0 && $between < 172800 && (idate('z', $from) + 1 == idate('z', $now) || idate('z', $from) > 2 + idate('z', $now))) {
  139. return _t('?? %s', date('H:i', $from));
  140. }
  141. /** ??????? */
  142. if ($between > 0 && $between < 604800 && idate('W', $from) == idate('W', $now)) {
  143. $day = intval($between / (3600 * 24));
  144. return sprintf(_n('%d??', '%d??', $day), $day);
  145. }
  146. /** ??? */
  147. if ($between > 0 && $between < 31622400 && idate('Y', $from) == idate('Y', $now)) {
  148. return date(_t('n?j?'), $from);
  149. }
  150. return date(_t('Y?m?d?'), $from);
  151. }
  152. /**
  153. * ?????
  154. *
  155. * @access public
  156. * @param string $lang ????
  157. * @return void
  158. */
  159. public static function setLang($lang)
  160. {
  161. self::$_lang = $lang;
  162. }
  163. /**
  164. * ?????
  165. *
  166. * @access public
  167. * @param string $lang ????
  168. * @return void
  169. */
  170. public static function addLang($lang)
  171. {
  172. self::$_loaded->addFile($lang);
  173. }
  174. /**
  175. * ?????
  176. *
  177. * @access public
  178. * @return void
  179. */
  180. public static function getLang()
  181. {
  182. return self::$_lang;
  183. }
  184. }