CMessageSource.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * CMessageSource 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. * CMessageSource is the base class for message translation repository classes.
  12. *
  13. * A message source is an application component that provides message internationalization (i18n).
  14. * It stores messages translated in different languages and provides
  15. * these translated versions when requested.
  16. *
  17. * A concrete class must implement {@link loadMessages} or override {@link translateMessage}.
  18. *
  19. * @property string $language The language that the source messages are written in.
  20. * Defaults to {@link CApplication::language application language}.
  21. *
  22. * @author Qiang Xue <qiang.xue@gmail.com>
  23. * @package system.i18n
  24. * @since 1.0
  25. */
  26. abstract class CMessageSource extends CApplicationComponent
  27. {
  28. /**
  29. * @var boolean whether to force message translation when the source and target languages are the same.
  30. * Defaults to false, meaning translation is only performed when source and target languages are different.
  31. * @since 1.1.4
  32. */
  33. public $forceTranslation=false;
  34. private $_language;
  35. private $_messages=array();
  36. /**
  37. * Loads the message translation for the specified language and category.
  38. * @param string $category the message category
  39. * @param string $language the target language
  40. * @return array the loaded messages
  41. */
  42. abstract protected function loadMessages($category,$language);
  43. /**
  44. * @return string the language that the source messages are written in.
  45. * Defaults to {@link CApplication::language application language}.
  46. */
  47. public function getLanguage()
  48. {
  49. return $this->_language===null ? Yii::app()->sourceLanguage : $this->_language;
  50. }
  51. /**
  52. * @param string $language the language that the source messages are written in.
  53. */
  54. public function setLanguage($language)
  55. {
  56. $this->_language=CLocale::getCanonicalID($language);
  57. }
  58. /**
  59. * Translates a message to the specified language.
  60. *
  61. * Note, if the specified language is the same as
  62. * the {@link getLanguage source message language}, messages will NOT be translated.
  63. *
  64. * If the message is not found in the translations, an {@link onMissingTranslation}
  65. * event will be raised. Handlers can mark this message or do some
  66. * default handling. The {@link CMissingTranslationEvent::message}
  67. * property of the event parameter will be returned.
  68. *
  69. * @param string $category the message category
  70. * @param string $message the message to be translated
  71. * @param string $language the target language. If null (default), the {@link CApplication::getLanguage application language} will be used.
  72. * @return string the translated message (or the original message if translation is not needed)
  73. */
  74. public function translate($category,$message,$language=null)
  75. {
  76. if($language===null)
  77. $language=Yii::app()->getLanguage();
  78. if($this->forceTranslation || $language!==$this->getLanguage())
  79. return $this->translateMessage($category,$message,$language);
  80. else
  81. return $message;
  82. }
  83. /**
  84. * Translates the specified message.
  85. * If the message is not found, an {@link onMissingTranslation}
  86. * event will be raised.
  87. * @param string $category the category that the message belongs to
  88. * @param string $message the message to be translated
  89. * @param string $language the target language
  90. * @return string the translated message
  91. */
  92. protected function translateMessage($category,$message,$language)
  93. {
  94. $key=$language.'.'.$category;
  95. if(!isset($this->_messages[$key]))
  96. $this->_messages[$key]=$this->loadMessages($category,$language);
  97. if(isset($this->_messages[$key][$message]) && $this->_messages[$key][$message]!=='')
  98. return $this->_messages[$key][$message];
  99. elseif($this->hasEventHandler('onMissingTranslation'))
  100. {
  101. $event=new CMissingTranslationEvent($this,$category,$message,$language);
  102. $this->onMissingTranslation($event);
  103. return $event->message;
  104. }
  105. else
  106. return $message;
  107. }
  108. /**
  109. * Raised when a message cannot be translated.
  110. * Handlers may log this message or do some default handling.
  111. * The {@link CMissingTranslationEvent::message} property
  112. * will be returned by {@link translateMessage}.
  113. * @param CMissingTranslationEvent $event the event parameter
  114. */
  115. public function onMissingTranslation($event)
  116. {
  117. $this->raiseEvent('onMissingTranslation',$event);
  118. }
  119. }
  120. /**
  121. * CMissingTranslationEvent represents the parameter for the {@link CMessageSource::onMissingTranslation onMissingTranslation} event.
  122. *
  123. * @author Qiang Xue <qiang.xue@gmail.com>
  124. * @package system.i18n
  125. * @since 1.0
  126. */
  127. class CMissingTranslationEvent extends CEvent
  128. {
  129. /**
  130. * @var string the message to be translated
  131. */
  132. public $message;
  133. /**
  134. * @var string the category that the message belongs to
  135. */
  136. public $category;
  137. /**
  138. * @var string the ID of the language that the message is to be translated to
  139. */
  140. public $language;
  141. /**
  142. * Constructor.
  143. * @param mixed $sender sender of this event
  144. * @param string $category the category that the message belongs to
  145. * @param string $message the message to be translated
  146. * @param string $language the ID of the language that the message is to be translated to
  147. */
  148. public function __construct($sender,$category,$message,$language)
  149. {
  150. parent::__construct($sender);
  151. $this->message=$message;
  152. $this->category=$category;
  153. $this->language=$language;
  154. }
  155. }