1: <?php
2: /**
3: * CGettextMessageSource 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: * CGettextMessageSource represents a message source that is based on GNU Gettext.
13: *
14: * Each CGettextMessageSource instance represents the message translations
15: * for a single domain. And each message category represents a message context
16: * in Gettext. Translated messages are stored as either a MO or PO file,
17: * depending on the {@link useMoFile} property value.
18: *
19: * All translations are saved under the {@link basePath} directory.
20: * Translations in one language are kept as MO or PO files under an individual
21: * subdirectory whose name is the language ID. The file name is specified via
22: * {@link catalog} property, which defaults to 'messages'.
23: *
24: * @author Qiang Xue <qiang.xue@gmail.com>
25: * @package system.i18n
26: * @since 1.0
27: */
28: class CGettextMessageSource extends CMessageSource
29: {
30: const CACHE_KEY_PREFIX='Yii.CGettextMessageSource.';
31: const MO_FILE_EXT='.mo';
32: const PO_FILE_EXT='.po';
33:
34: /**
35: * @var integer the time in seconds that the messages can remain valid in cache.
36: * Defaults to 0, meaning the caching is disabled.
37: */
38: public $cachingDuration=0;
39: /**
40: * @var string the ID of the cache application component that is used to cache the messages.
41: * Defaults to 'cache' which refers to the primary cache application component.
42: * Set this property to false if you want to disable caching the messages.
43: */
44: public $cacheID='cache';
45: /**
46: * @var string the base path for all translated messages. Defaults to null, meaning
47: * the "messages" subdirectory of the application directory (e.g. "protected/messages").
48: */
49: public $basePath;
50: /**
51: * @var boolean whether to load messages from MO files. Defaults to true.
52: * If false, messages will be loaded from PO files.
53: */
54: public $useMoFile=true;
55: /**
56: * @var boolean whether to use Big Endian to read and write MO files.
57: * Defaults to false. This property is only used when {@link useMoFile} is true.
58: */
59: public $useBigEndian=false;
60: /**
61: * @var string the message catalog name. This is the name of the message file (without extension)
62: * that stores the translated messages. Defaults to 'messages'.
63: */
64: public $catalog='messages';
65:
66: /**
67: * Initializes the application component.
68: * This method overrides the parent implementation by preprocessing
69: * the user request data.
70: */
71: public function init()
72: {
73: parent::init();
74: if($this->basePath===null)
75: $this->basePath=Yii::getPathOfAlias('application.messages');
76: }
77:
78: /**
79: * Loads the message translation for the specified language and category.
80: * @param string $category the message category
81: * @param string $language the target language
82: * @return array the loaded messages
83: */
84: protected function loadMessages($category, $language)
85: {
86: $messageFile=$this->basePath . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $this->catalog;
87: if($this->useMoFile)
88: $messageFile.=self::MO_FILE_EXT;
89: else
90: $messageFile.=self::PO_FILE_EXT;
91:
92: if ($this->cachingDuration > 0 && $this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
93: {
94: $key = self::CACHE_KEY_PREFIX . $messageFile . "." . $category;
95: if (($data=$cache->get($key)) !== false)
96: return unserialize($data);
97: }
98:
99: if (is_file($messageFile))
100: {
101: if($this->useMoFile)
102: $file=new CGettextMoFile($this->useBigEndian);
103: else
104: $file=new CGettextPoFile();
105: $messages=$file->load($messageFile,$category);
106: if(isset($cache))
107: {
108: $dependency=new CFileCacheDependency($messageFile);
109: $cache->set($key,serialize($messages),$this->cachingDuration,$dependency);
110: }
111: return $messages;
112: }
113: else
114: return array();
115: }
116: }
117: