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