system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CMessageSource

system.i18n
继承 abstract class CMessageSource » CApplicationComponent » CComponent
实现 IApplicationComponent
子类 CDbMessageSource, CGettextMessageSource, CPhpMessageSource
可用自 1.0
源码 framework/i18n/CMessageSource.php
CMessageSource is the base class for message translation repository classes.

A message source is an application component that provides message internationalization (i18n). It stores messages translated in different languages and provides these translated versions when requested.

A concrete class must implement loadMessages or override translateMessage.

公共属性

隐藏继承的属性

属性类型描述被定义在
behaviors array the behaviors that should be attached to this component. CApplicationComponent
forceTranslation boolean whether to force message translation when the source and target languages are the same. CMessageSource
isInitialized boolean Checks if this application component has been initialized. CApplicationComponent
language string the language that the source messages are written in. CMessageSource

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() Checks if this application component has been initialized. CApplicationComponent
getLanguage() Returns the language that the source messages are written in. Defaults to application language. CMessageSource
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the application component. CApplicationComponent
onMissingTranslation() Raised when a message cannot be translated. CMessageSource
raiseEvent() Raises an event. CComponent
setLanguage() Sets the language that the source messages are written in. CMessageSource
translate() Translates a message to the specified language. CMessageSource

受保护的方法

隐藏继承的方法

方法描述被定义在
loadMessages() Loads the message translation for the specified language and category. CMessageSource
translateMessage() Translates the specified message. CMessageSource

事件

隐藏继承的事件

事件描述被定义在
onMissingTranslation Raised when a message cannot be translated. CMessageSource

属性详情

forceTranslation 属性 (自版本 v1.1.4 可用)
public boolean $forceTranslation;

whether to force message translation when the source and target languages are the same. Defaults to false, meaning translation is only performed when source and target languages are different.

language 属性
public string getLanguage()
public void setLanguage(string $language)

the language that the source messages are written in. Defaults to application language.

方法详情

getLanguage() 方法
public string getLanguage()
{return} string the language that the source messages are written in. Defaults to application language.
源码: framework/i18n/CMessageSource.php#51 (显示)
public function getLanguage()
{
    return 
$this->_language===null Yii::app()->sourceLanguage $this->_language;
}

loadMessages() 方法
abstract protected array loadMessages(string $category, string $language)
$category string the message category
$language string the target language
{return} array the loaded messages
源码: framework/i18n/CMessageSource.php#45 (显示)
abstract protected function loadMessages($category,$language);

Loads the message translation for the specified language and category.

onMissingTranslation() 方法
public void onMissingTranslation(CMissingTranslationEvent $event)
$event CMissingTranslationEvent the event parameter
源码: framework/i18n/CMessageSource.php#123 (显示)
public function onMissingTranslation($event)
{
    
$this->raiseEvent('onMissingTranslation',$event);
}

Raised when a message cannot be translated. Handlers may log this message or do some default handling. The CMissingTranslationEvent::message property will be returned by translateMessage.

setLanguage() 方法
public void setLanguage(string $language)
$language string the language that the source messages are written in.
源码: framework/i18n/CMessageSource.php#59 (显示)
public function setLanguage($language)
{
    
$this->_language=CLocale::getCanonicalID($language);
}

translate() 方法
public string translate(string $category, string $message, string $language=NULL)
$category string the message category
$message string the message to be translated
$language string the target language. If null (default), the application language will be used.
{return} string the translated message (or the original message if translation is not needed)
源码: framework/i18n/CMessageSource.php#80 (显示)
public function translate($category,$message,$language=null)
{
    if(
$language===null)
        
$language=Yii::app()->getLanguage();
    if(
$this->forceTranslation || $language!==$this->getLanguage())
        return 
$this->translateMessage($category,$message,$language);
    else
        return 
$message;
}

Translates a message to the specified language.

Note, if the specified language is the same as the source message language, messages will NOT be translated.

If the message is not found in the translations, an onMissingTranslation event will be raised. Handlers can mark this message or do some default handling. The CMissingTranslationEvent::message property of the event parameter will be returned.

translateMessage() 方法
protected string translateMessage(string $category, string $message, string $language)
$category string the category that the message belongs to
$message string the message to be translated
$language string the target language
{return} string the translated message
源码: framework/i18n/CMessageSource.php#99 (显示)
protected function translateMessage($category,$message,$language)
{
    
$key=$language.'.'.$category;
    if(!isset(
$this->_messages[$key]))
        
$this->_messages[$key]=$this->loadMessages($category,$language);
    if(isset(
$this->_messages[$key][$message]) && $this->_messages[$key][$message]!=='')
        return 
$this->_messages[$key][$message];
    elseif(
$this->hasEventHandler('onMissingTranslation'))
    {
        
$event=new CMissingTranslationEvent($this,$category,$message,$language);
        
$this->onMissingTranslation($event);
        return 
$event->message;
    }
    else
        return 
$message;
}

Translates the specified message. If the message is not found, an onMissingTranslation event will be raised.