CDbMessageSource.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * CDbMessageSource class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CDbMessageSource represents a message source that stores translated messages in database.
  12. *
  13. * The database must contain the following two tables:
  14. * <pre>
  15. * CREATE TABLE SourceMessage
  16. * (
  17. * id INTEGER PRIMARY KEY,
  18. * category VARCHAR(32),
  19. * message TEXT
  20. * );
  21. * CREATE TABLE Message
  22. * (
  23. * id INTEGER,
  24. * language VARCHAR(16),
  25. * translation TEXT,
  26. * PRIMARY KEY (id, language),
  27. * CONSTRAINT FK_Message_SourceMessage FOREIGN KEY (id)
  28. * REFERENCES SourceMessage (id) ON DELETE CASCADE ON UPDATE RESTRICT
  29. * );
  30. * </pre>
  31. * The 'SourceMessage' table stores the messages to be translated, and the 'Message' table
  32. * stores the translated messages. The name of these two tables can be customized by setting
  33. * {@link sourceMessageTable} and {@link translatedMessageTable}, respectively.
  34. *
  35. * When {@link cachingDuration} is set as a positive number, message translations will be cached.
  36. *
  37. * @property CDbConnection $dbConnection The DB connection used for the message source.
  38. *
  39. * @author Qiang Xue <qiang.xue@gmail.com>
  40. * @package system.i18n
  41. * @since 1.0
  42. */
  43. class CDbMessageSource extends CMessageSource
  44. {
  45. const CACHE_KEY_PREFIX='Yii.CDbMessageSource.';
  46. /**
  47. * @var string the ID of the database connection application component. Defaults to 'db'.
  48. */
  49. public $connectionID='db';
  50. /**
  51. * @var string the name of the source message table. Defaults to 'SourceMessage'.
  52. */
  53. public $sourceMessageTable='SourceMessage';
  54. /**
  55. * @var string the name of the translated message table. Defaults to 'Message'.
  56. */
  57. public $translatedMessageTable='Message';
  58. /**
  59. * @var integer the time in seconds that the messages can remain valid in cache.
  60. * Defaults to 0, meaning the caching is disabled.
  61. */
  62. public $cachingDuration=0;
  63. /**
  64. * @var string the ID of the cache application component that is used to cache the messages.
  65. * Defaults to 'cache' which refers to the primary cache application component.
  66. * Set this property to false if you want to disable caching the messages.
  67. */
  68. public $cacheID='cache';
  69. /**
  70. * Loads the message translation for the specified language and category.
  71. * @param string $category the message category
  72. * @param string $language the target language
  73. * @return array the loaded messages
  74. */
  75. protected function loadMessages($category,$language)
  76. {
  77. if($this->cachingDuration>0 && $this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
  78. {
  79. $key=self::CACHE_KEY_PREFIX.'.messages.'.$category.'.'.$language;
  80. if(($data=$cache->get($key))!==false)
  81. return unserialize($data);
  82. }
  83. $messages=$this->loadMessagesFromDb($category,$language);
  84. if(isset($cache))
  85. $cache->set($key,serialize($messages),$this->cachingDuration);
  86. return $messages;
  87. }
  88. private $_db;
  89. /**
  90. * Returns the DB connection used for the message source.
  91. * @throws CException if {@link connectionID} application component is invalid
  92. * @return CDbConnection the DB connection used for the message source.
  93. * @since 1.1.5
  94. */
  95. public function getDbConnection()
  96. {
  97. if($this->_db===null)
  98. {
  99. $this->_db=Yii::app()->getComponent($this->connectionID);
  100. if(!$this->_db instanceof CDbConnection)
  101. throw new CException(Yii::t('yii','CDbMessageSource.connectionID is invalid. Please make sure "{id}" refers to a valid database application component.',
  102. array('{id}'=>$this->connectionID)));
  103. }
  104. return $this->_db;
  105. }
  106. /**
  107. * Loads the messages from database.
  108. * You may override this method to customize the message storage in the database.
  109. * @param string $category the message category
  110. * @param string $language the target language
  111. * @return array the messages loaded from database
  112. * @since 1.1.5
  113. */
  114. protected function loadMessagesFromDb($category,$language)
  115. {
  116. $command=$this->getDbConnection()->createCommand()
  117. ->select("t1.message AS message, t2.translation AS translation")
  118. ->from(array("{$this->sourceMessageTable} t1","{$this->translatedMessageTable} t2"))
  119. ->where('t1.id=t2.id AND t1.category=:category AND t2.language=:language',array(':category'=>$category,':language'=>$language))
  120. ;
  121. $messages=array();
  122. foreach($command->queryAll() as $row)
  123. $messages[$row['message']]=$row['translation'];
  124. return $messages;
  125. }
  126. }