PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/yii/framework/i18n/CPhpMessageSource.php

https://github.com/joshuaswarren/weatherhub
PHP | 140 lines | 55 code | 8 blank | 77 comment | 10 complexity | 35b0d5d567c80a07868ceb2f024b39d3 MD5 | raw file
  1. <?php
  2. /**
  3. * CPhpMessageSource class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CPhpMessageSource represents a message source that stores translated messages in PHP scripts.
  12. *
  13. * CPhpMessageSource uses PHP files and arrays to keep message translations.
  14. * <ul>
  15. * <li>All translations are saved under the {@link basePath} directory.</li>
  16. * <li>Translations in one language are kept as PHP files under an individual subdirectory
  17. * whose name is the same as the language ID. Each PHP file contains messages
  18. * belonging to the same category, and the file name is the same as the category name.</li>
  19. * <li>Within a PHP file, an array of (source, translation) pairs is returned.
  20. * For example:
  21. * <pre>
  22. * return array(
  23. * 'original message 1' => 'translated message 1',
  24. * 'original message 2' => 'translated message 2',
  25. * );
  26. * </pre>
  27. * </li>
  28. * </ul>
  29. * When {@link cachingDuration} is set as a positive number, message translations will be cached.
  30. *
  31. * Starting from version 1.0.10, messages for an extension class (e.g. a widget, a module) can be specially managed and used.
  32. * In particular, if a message belongs to an extension whose class name is Xyz, then the message category
  33. * can be specified in the format of 'Xyz.categoryName'. And the corresponding message file
  34. * is assumed to be 'BasePath/messages/LanguageID/categoryName.php', where 'BasePath' refers to
  35. * the directory that contains the extension class file. When using Yii::t() to translate an extension message,
  36. * the category name should be set as 'Xyz.categoryName'.
  37. *
  38. * @author Qiang Xue <qiang.xue@gmail.com>
  39. * @version $Id: CPhpMessageSource.php 2798 2011-01-01 19:29:03Z qiang.xue $
  40. * @package system.i18n
  41. * @since 1.0
  42. */
  43. class CPhpMessageSource extends CMessageSource
  44. {
  45. const CACHE_KEY_PREFIX='Yii.CPhpMessageSource.';
  46. /**
  47. * @var integer the time in seconds that the messages can remain valid in cache.
  48. * Defaults to 0, meaning the caching is disabled.
  49. */
  50. public $cachingDuration=0;
  51. /**
  52. * @var string the ID of the cache application component that is used to cache the messages.
  53. * Defaults to 'cache' which refers to the primary cache application component.
  54. * Set this property to false if you want to disable caching the messages.
  55. * @since 1.0.10
  56. */
  57. public $cacheID='cache';
  58. /**
  59. * @var string the base path for all translated messages. Defaults to null, meaning
  60. * the "messages" subdirectory of the application directory (e.g. "protected/messages").
  61. */
  62. public $basePath;
  63. private $_files=array();
  64. /**
  65. * Initializes the application component.
  66. * This method overrides the parent implementation by preprocessing
  67. * the user request data.
  68. */
  69. public function init()
  70. {
  71. parent::init();
  72. if($this->basePath===null)
  73. $this->basePath=Yii::getPathOfAlias('application.messages');
  74. }
  75. /**
  76. * Determines the message file name based on the given category and language.
  77. * If the category name contains a dot, it will be split into the module class name and the category name.
  78. * In this case, the message file will be assumed to be located within the 'messages' subdirectory of
  79. * the directory containing the module class file.
  80. * Otherwise, the message file is assumed to be under the {@link basePath}.
  81. * @param string $category category name
  82. * @param string $language language ID
  83. * @return string the message file path
  84. * @since 1.0.10
  85. */
  86. protected function getMessageFile($category,$language)
  87. {
  88. if(!isset($this->_files[$category][$language]))
  89. {
  90. if(($pos=strpos($category,'.'))!==false)
  91. {
  92. $moduleClass=substr($category,0,$pos);
  93. $moduleCategory=substr($category,$pos+1);
  94. $class=new ReflectionClass($moduleClass);
  95. $this->_files[$category][$language]=dirname($class->getFileName()).DIRECTORY_SEPARATOR.'messages'.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$moduleCategory.'.php';
  96. }
  97. else
  98. $this->_files[$category][$language]=$this->basePath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$category.'.php';
  99. }
  100. return $this->_files[$category][$language];
  101. }
  102. /**
  103. * Loads the message translation for the specified language and category.
  104. * @param string $category the message category
  105. * @param string $language the target language
  106. * @return array the loaded messages
  107. */
  108. protected function loadMessages($category,$language)
  109. {
  110. $messageFile=$this->getMessageFile($category,$language);
  111. if($this->cachingDuration>0 && $this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
  112. {
  113. $key=self::CACHE_KEY_PREFIX . $messageFile;
  114. if(($data=$cache->get($key))!==false)
  115. return unserialize($data);
  116. }
  117. if(is_file($messageFile))
  118. {
  119. $messages=include($messageFile);
  120. if(!is_array($messages))
  121. $messages=array();
  122. if(isset($cache))
  123. {
  124. $dependency=new CFileCacheDependency($messageFile);
  125. $cache->set($key,serialize($messages),$this->cachingDuration,$dependency);
  126. }
  127. return $messages;
  128. }
  129. else
  130. return array();
  131. }
  132. }