CMessageSource
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.
Public Properties
Property |
Type |
Description |
Defined By |
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 |
Protected Methods
Method |
Description |
Defined By |
loadMessages() | Loads the message translation for the specified language and category. | CMessageSource |
translateMessage() | Translates the specified message. | CMessageSource |
Events
Event |
Description |
Defined By |
onMissingTranslation | Raised when a message cannot be translated. | CMessageSource |
Property Details
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.
public string getLanguage()
public void setLanguage(string $language)
the language that the source messages are written in. Defaults to application language.
Method Details
public string getLanguage() |
{return} | string | the language that the source messages are written in. Defaults to application language. |
abstract protected array loadMessages(string $category, string $language) |
$category | string | the message category |
$language | string | the target language |
{return} | array | the loaded messages |
Loads the message translation for the specified language and category.
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.
public void setLanguage(string $language) |
$language | string | the language that the source messages are written in. |
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) |
Source Code: framework/i18n/CMessageSource.php#80 (
show)
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.
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 |
Source Code: framework/i18n/CMessageSource.php#99 (
show)
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.