MessageSource
API reference › yii › i18n › MessageSource
3 subclasses
MessageSource is the base class for message translation repository classes.
A message source stores message translations in some persistent storage.
Child classes should override loadMessages() to provide translated messages.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$forceTranslation | bool | Whether to force message translation when the source and target languages are the same. Default: false. | MessageSource |
$sourceLanguage | string| | The language that the original messages are in. Default: null. | MessageSource |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes this component. | MessageSource |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
translate() | Translates a message to the specified language. | MessageSource |
trigger() | Triggers an event. | yii\ |
loadMessages() protected | Loads the message translation for the specified language and category. | MessageSource |
translateMessage() protected | Translates the specified message. | MessageSource |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_MISSING_TRANSLATION | MissingTranslationEvent | An event that is triggered when a message translation is not found. Event name: 'missingTranslation'. | MessageSource |
Property details
Section titled “Property details”$forceTranslation
Section titled “$forceTranslation ”public bool $forceTranslation = falseWhether 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.
$sourceLanguage
Section titled “$sourceLanguage ”public string|null $sourceLanguage = nullThe language that the original messages are in.
If not set, it will use the value of
Application::$sourceLanguage.
Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): mixedInitializes this component.
Returns
mixed
translate()
Section titled “translate() ”public function translate(string $category, string $message, string $language): string|boolTranslates a message to the specified language.
Note that unless $forceTranslation is true, if the target language
is the same as the source language, the message
will NOT be translated.
If a translation is not found, a missingTranslation event will be triggered.
Parameters
| Parameter | Type | Description |
|---|---|---|
$category | string | The message category |
$message | string | The message to be translated |
$language | string | The target language |
Returns
string|bool: The translated message or false if translation wasn’t found or isn’t required
loadMessages()
Section titled “loadMessages() ”protected function loadMessages(string $category, string $language): arrayLoads the message translation for the specified language and category.
If translation for specific locale code such as en-US isn’t found it
tries more generic en.
Parameters
| Parameter | Type | Description |
|---|---|---|
$category | string | The message category |
$language | string | The target language |
Returns
array: The loaded messages. The keys are original messages, and the values are translated messages.
translateMessage()
Section titled “translateMessage() ”protected function translateMessage(string $category, string $message, string $language): string|boolTranslates the specified message.
If the message is not found, a missingTranslation event will be triggered.
If there is an event handler, it may provide a fallback translation.
If no fallback translation is provided this method will return false.
Parameters
| Parameter | Type | Description |
|---|---|---|
$category | string | The category that the message belongs to. |
$message | string | The message to be translated. |
$language | string | The target language. |
Returns
string|bool: The translated message or false if translation wasn’t found.