PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/wind/i18n/WindLangResource.php

https://gitlab.com/wuhang2003/phpwind
PHP | 133 lines | 58 code | 7 blank | 68 comment | 11 complexity | 908d3085b141a2e26d58ee767cb2579b MD5 | raw file
  1. <?php
  2. Wind::import('WIND:i18n.WindI18nException');
  3. Wind::import('WIND:i18n.IWindLangResource');
  4. /**
  5. * 语言资源基础实现
  6. * 语言资源基础实现,支持ini格式语言资源类型的解析,该语言资源组件基于wind组件模式进行开发.
  7. * 实现了语言包路径,默认语言文件,语言内容缓存等功能.
  8. *
  9. * @example <code>
  10. * LANG 为包名,如果不填写则默认没有分包处理,资源类将自动在language包下面寻找
  11. * 支持解析格式: LANG:login.fail.expty = 'xxx'
  12. * </code>
  13. * @author Shi Long <long.shi@alibaba-inc.com>
  14. * @copyright ©2003-2103 phpwind.com
  15. * @license http://www.windframework.com
  16. * @version $Id: WindLangResource.php 3850 2012-12-04 07:30:02Z yishuo $
  17. * @package i18n
  18. */
  19. class WindLangResource extends WindModule implements IWindLangResource {
  20. /**
  21. * 缓存key前缀
  22. *
  23. * @var string
  24. */
  25. protected $_cachePrefix = 'Wind.i18n.WindLangResource';
  26. /**
  27. * 消息存储池
  28. *
  29. * @var array
  30. */
  31. protected $_messages = array();
  32. /**
  33. * 默认资源文件
  34. *
  35. * @var string
  36. */
  37. protected $default;
  38. /**
  39. * 资源文件后缀名定义
  40. *
  41. * @var string
  42. */
  43. protected $suffix;
  44. /**
  45. * 语言包目录
  46. *
  47. * @var string
  48. */
  49. protected $path;
  50. /**
  51. * 语言
  52. *
  53. * @var string
  54. */
  55. protected $language;
  56. /**
  57. *
  58. * @var WindLocale
  59. */
  60. protected $locale = null;
  61. /*
  62. * (non-PHPdoc) @see IWindLangResource::lang()
  63. */
  64. public function getMessage($message, $params = array()) {
  65. $message = $this->translate($message);
  66. return empty($params) ? $message : WindUtility::strtr($message, $params);
  67. }
  68. /**
  69. * 获取一条message信息
  70. *
  71. * @param array $messages
  72. * @return string
  73. */
  74. protected function translate($message) {
  75. $package = $file = '';
  76. $_message = $message;
  77. if (strpos($_message, ':') != false) list($package, $_message) = explode(':', $_message, 2);
  78. if (strpos($_message, '.') != false) list($file, $key) = explode('.', $_message, 2);
  79. $path = $this->resolvedPath($package);
  80. if (is_file($path . '/' . $file . $this->suffix)) {
  81. $path = $path . '/' . $file . $this->suffix;
  82. } elseif (is_file($path . '/' . $this->default . $this->suffix)) {
  83. $path = $path . '/' . $this->default . $this->suffix;
  84. $key = $_message;
  85. $file = $this->default;
  86. } else
  87. return $message;
  88. if (!isset($this->_messages[$path])) {
  89. /* @var $cache AbstractWindCache */
  90. $cache = Wind::getComponent('windCache');
  91. $cacheKey = $this->_cachePrefix . $package . $file . filemtime($path);
  92. $messages = null;
  93. if ($cache) $messages = $cache->get($cacheKey);
  94. if (!$messages) {
  95. $messages = parse_ini_file($path);
  96. if ($cache) $cache->set($cacheKey, $messages);
  97. }
  98. $this->_messages[$path] = $messages;
  99. }
  100. return isset($this->_messages[$path][$key]) ? $this->_messages[$path][$key] : $message;
  101. }
  102. /**
  103. * 解析资源文件路径信息
  104. *
  105. * @param string $package
  106. * @return string
  107. */
  108. protected function resolvedPath($package) {
  109. $this->path || $this->path = Wind::getRootPath(Wind::getAppName());
  110. $this->language || $this->language = $this->getRequest()->getAcceptLanguage();
  111. $path = $this->path . '/' . $this->language . '/' . strtolower($package);
  112. $path = Wind::getRealDir(trim($path, '/'), true);
  113. return $path;
  114. }
  115. /*
  116. * (non-PHPdoc) @see WindModule::setConfig()
  117. */
  118. public function setConfig($config) {
  119. parent::setConfig($config);
  120. $this->suffix = $this->getConfig('suffix', '', '');
  121. $this->default = $this->getConfig('default', '', 'message');
  122. $this->path = $this->getConfig('path', '', '');
  123. $this->language = $this->getConfig('language', '', 'zh_cn');
  124. }
  125. }
  126. ?>