CGettextMessageSource.php 3.8 KB

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