MessageController
API reference › yii › console › controllers › MessageController
Extracts messages to be translated from source files.
The extracted messages can be saved the following depending on format
setting in config file:
- PHP message source files.
- “.po” files.
- Database.
Usage:
- Create a configuration file using the ‘message/config’ command: yii message/config /path/to/myapp/messages/config.php
- Edit the created config file, adjusting it for your web application needs.
- Run the ‘message/extract’ command, using created config: yii message /path/to/myapp/messages/config.php
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$action | yii\<static>| | The action that is currently being executed. Default: null. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$catalog | string | Name of the file that will be used for translations for “po” format. Default: 'messages'. | MessageController |
$color | bool| | Whether to enable ANSI color in the output. Default: null. | yii\ |
$db | string | Connection component ID for “db” format. Default: 'db'. | MessageController |
$defaultAction | string | Controller default action ID. Default: 'extract'. | MessageController |
$except | array| | List of patterns that specify which files/directories should NOT be processed. Default: ['.*',. | MessageController |
$format | string | Generated file format. Default: 'php'. | MessageController |
$help | bool | Whether to display help information about current command. Default: false. Available since 2.0.10. | yii\ |
$helpSummary read-only | string | The one-line short summary describing this controller. | yii\ |
$id | string | The ID of this controller. Default: null. | yii\ |
$ignoreCategories | array | Message categories to ignore. Default: []. | MessageController |
$interactive | bool | Whether to run the command interactively. Default: true. | yii\ |
$languages | array | Required, list of language codes that the extracted messages should be translated to. Default: []. | MessageController |
$layout | string| | The name of the layout to be applied to this controller’s views. Default: null. | yii\ |
$markUnused | bool | Whether to mark messages that no longer appear in the source code. Default: true. | MessageController |
$messagePath | string | Required, root directory containing message translations. Default: '@yii/messages'. | MessageController |
$messageTable | string | Custom name for translation message table for “db” format. Default: '{{%message}}'. | MessageController |
$module | yii\ | The module that this controller belongs to. Default: null. | yii\ |
$modules read-only | yii\[] | All ancestor modules that this controller is located within. | yii\ |
$only | array| | List of patterns that specify which files (not directories) should be processed. Default: ['*.php']. | MessageController |
$overwrite | bool | Whether the message file should be overwritten with the merged messages Default: true. | MessageController |
$passedOptions read-only | array | The names of the options passed during execution. | yii\ |
$passedOptionValues read-only | array | The properties corresponding to the passed options. | yii\ |
$phpDocBlock | string| | DocBlock used for messages array in generated PHP file. Default: null. Available since 2.0.13. | MessageController |
$phpFileHeader | string | File header in generated PHP file with messages. Default: ''. Available since 2.0.13. | MessageController |
$removeUnused | bool | Whether to remove messages that no longer appear in the source code. Default: false. | MessageController |
$request | yii\ | The request object. | yii\ |
$response | yii\ | The response object. | yii\ |
$route read-only | string | The route (module ID, controller ID and action ID) of the current request. | yii\ |
$silentExitOnException | bool| | If true - script finish with ExitCode::OK in case of exception. false - ExitCode::UNSPECIFIED_ERROR. Default: null. Available since 2.0.36. | yii\ |
$sort | bool | Whether to sort messages by keys when merging new messages with the existing ones. Default: false. | MessageController |
$sourceMessageTable | string | Custom name for source message table for “db” format. Default: '{{%source_message}}'. | MessageController |
$sourcePath | string | Required, root directory of all source files. Default: '@yii'. | MessageController |
$translator | string| | The name of the function for translating messages. Default: ['Yii::t',. | MessageController |
$uniqueId read-only | string | The controller ID that is prefixed with the module ID (if any). | yii\ |
$view | yii\|yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath | string | The directory containing the view files for this controller. | yii\ |
$config protected | array | Config for messages extraction. Default: null. Available since 2.0.13. | MessageController |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | 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\ |
actionConfig() | Creates a configuration file for the “extract” command using command line options specified. | MessageController |
actionConfigTemplate() | Creates a configuration file template for the “extract” command. | MessageController |
actionExtract() | Extracts messages to be translated from source code. | MessageController |
actions() | Declares external actions for the controller. | yii\ |
afterAction() | This method is invoked right after an action is executed. | yii\ |
ansiFormat() | Formats a string with ANSI codes. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeAction() | This method is invoked right before an action is executed. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
bindActionParams() | Binds the parameters to the action. | 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\ |
confirm() | Asks user to confirm by typing y or n. | yii\ |
createAction() | Creates an action based on the given action ID. | 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\ |
findLayoutFile() | Finds the applicable layout file. | yii\ |
getActionArgsHelp() | Returns the help information for the anonymous arguments for the action. | yii\ |
getActionHelp() | Returns the detailed help information for the specified action. | yii\ |
getActionHelpSummary() | Returns a one-line short summary describing the specified action. | yii\ |
getActionOptionsHelp() | Returns the help information for the options for the action. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getHelp() | Returns help information for this controller. | yii\ |
getHelpSummary() | Returns one-line short summary describing this controller. | yii\ |
getModules() | Returns all ancestor modules of this controller. | yii\ |
getOptionValues() | Returns properties corresponding to the options for the action id Child classes may override this method to specify possible properties. | yii\ |
getPassedOptions() | Returns the names of valid options passed during execution. | yii\ |
getPassedOptionValues() | Returns the properties corresponding to the passed options. | yii\ |
getRoute() | Returns the route of the current request. | yii\ |
getUniqueId() | Returns the unique ID of the controller. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing view files for this controller. | 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 the object. | yii\ |
isColorEnabled() | Returns a value indicating whether ANSI color is enabled. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
optionAliases() | Returns option alias names. | MessageController |
options() | Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name. | MessageController |
prompt() | Prompts the user for input and validates it. | yii\ |
render() | Renders a view and applies layout if available. | yii\ |
renderContent() | Renders a static string by applying a layout. | yii\ |
renderFile() | Renders a view file. | yii\ |
renderPartial() | Renders a view without applying layout. | yii\ |
run() | Runs a request specified in terms of a route. | yii\ |
runAction() | Runs an action with the specified action ID and parameters. | yii\ |
select() | Gives the user an option to choose from. Giving ’?’ as an input will show a list of options to choose from and their explanations. | yii\ |
setView() | Sets the view object to be used by this controller. | yii\ |
setViewPath() | Sets the directory that contains the view files. | yii\ |
stderr() | Prints a string to STDERR. | yii\ |
stdout() | Prints a string to STDOUT. | yii\ |
trigger() | Triggers an event. | yii\ |
bindInjectedParams() protected final | Fills parameters based on types and names in action method signature. | yii\ |
extractMessages() protected | Extracts messages from a file. | MessageController |
extractMessagesFromTokens() protected | Extracts messages from a parsed PHP tokens list. | MessageController |
getActionMethodReflection() protected | yii\ | |
getLine() protected | Finds out a line of the first non-char PHP token found. | MessageController |
initConfig() protected | MessageController | |
isCategoryIgnored() protected | The method checks, whether the $category is ignored according to $ignoreCategories array. | MessageController |
parseDocCommentDetail() protected | Returns full description from the docblock. | yii\ |
parseDocCommentSummary() protected | Returns the first line of docblock. | yii\ |
parseDocCommentTags() protected | Parses the comment block into tags. | yii\ |
saveMessagesCategoryToPHP() protected | Writes category messages into PHP file. | MessageController |
saveMessagesToDb() protected | Saves messages to database. | MessageController |
saveMessagesToPHP() protected | Writes messages into PHP files. | MessageController |
saveMessagesToPO() protected | Writes messages into PO file. | MessageController |
saveMessagesToPOT() protected | Writes messages into POT file. | MessageController |
tokensEqual() protected | Finds out if two PHP tokens are equal. | MessageController |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEFORE_ACTION | yii\ | An event raised right before executing a controller action. Event name: 'beforeAction'. | yii\ |
EVENT_AFTER_ACTION | yii\ | An event raised right after executing a controller action. Event name: 'afterAction'. | yii\ |
Property details
Section titled “Property details”$except
Section titled “$except ”public array|null $except = ['.*', '/.*', '/messages', '/tests', '/runtime', '/vendor', '/BaseYii.php']List of patterns that specify which files/directories should NOT be processed.
If empty or not set, all files/directories will be processed. See helpers/FileHelper::findFiles() description for pattern matching rules. If a file/directory matches both a pattern in “only” and “except”, it will NOT be processed.
$format
Section titled “$format ”public string $format = 'php'Generated file format.
Can be “php”, “db”, “po” or “pot”.
$ignoreCategories
Section titled “$ignoreCategories ”public array $ignoreCategories = []Message categories to ignore.
For example, ‘yii’, ‘app*’, ‘widgets/menu’, etc.
$languages
Section titled “$languages ”public array $languages = []Required, list of language codes that the extracted messages should be translated to.
For example, [‘zh-CN’, ‘de’].
$markUnused
Section titled “$markUnused ”public bool $markUnused = trueWhether to mark messages that no longer appear in the source code.
Defaults to true, which means each of these messages will be enclosed with a pair of ’@@’ marks.
$only
Section titled “$only ”public array|null $only = ['*.php']List of patterns that specify which files (not directories) should be processed.
If empty or not set, all files will be processed. See helpers/FileHelper::findFiles() description for pattern matching rules. If a file/directory matches both a pattern in “only” and “except”, it will NOT be processed.
$phpDocBlock
Section titled “$phpDocBlock ”public string|null $phpDocBlock = nullDocBlock used for messages array in generated PHP file.
If null, default DocBlock will be used.
This property is used only if $format is “php”.
Since
2.0.13
$phpFileHeader
Section titled “$phpFileHeader ”public string $phpFileHeader = ''File header in generated PHP file with messages.
This property is used only if $format is “php”.
Since
2.0.13
$removeUnused
Section titled “$removeUnused ”public bool $removeUnused = falseWhether to remove messages that no longer appear in the source code.
Defaults to false, which means these messages will NOT be removed.
$sort
Section titled “$sort ”public bool $sort = falseWhether to sort messages by keys when merging new messages with the existing ones.
Defaults to false, which means the new (untranslated) messages will be separated from the old (translated) ones.
$translator
Section titled “$translator ”public string|string[] $translator = ['Yii::t', '\Yii::t']The name of the function for translating messages.
This is used as a mark to find the messages to be translated. You may use a string for single function name or an array for multiple function names.
Method details
Section titled “Method details”actionConfig()
Section titled “actionConfig() ”public function actionConfig(string $filePath): intCreates a configuration file for the “extract” command using command line options specified.
The generated configuration file contains parameters required for source code messages extraction. You may use this configuration file with the “extract” command.
Parameters
| Parameter | Type | Description |
|---|---|---|
$filePath | string | Output file name or alias. |
Returns
int: CLI exit code
Throws
yii\console\Exception: on failure.
actionConfigTemplate()
Section titled “actionConfigTemplate() ”public function actionConfigTemplate(string $filePath): intCreates a configuration file template for the “extract” command.
The created configuration file contains detailed instructions on how to customize it to fit for your needs. After customization, you may use this configuration file with the “extract” command.
Parameters
| Parameter | Type | Description |
|---|---|---|
$filePath | string | Output file name or alias. |
Returns
int: CLI exit code
Throws
yii\console\Exception: on failure.
actionExtract()
Section titled “actionExtract() ”public function actionExtract(string|null $configFile = null): mixedExtracts messages to be translated from source code.
This command will search through source code files and extract messages that need to be translated in different languages.
Parameters
| Parameter | Type | Description |
|---|---|---|
$configFile | string| | The path or alias of the configuration file. You may use the “yii message/config” command to generate this file and then customize it for your needs. |
Returns
mixed
Throws
yii\console\Exception: on failure.
optionAliases()
Section titled “optionAliases() ”public function optionAliases(): arrayReturns option alias names.
Child classes may override this method to specify alias options.
Returns
array: The options alias names valid for the action where the keys is alias name for option and value is option name.
Since
2.0.8
options()
Section titled “options() ”public function options(mixed $actionID): string[]Returns the names of valid options for the action (id) An option requires the existence of a public member variable whose name is the option name.
Child classes may override this method to specify possible options.
Note that the values setting via options are not available
until beforeAction() is being called.
Parameters
| Parameter | Type | Description |
|---|---|---|
$actionID | mixed | The action id of the current request |
Returns
string[]: The names of the options valid for the action
extractMessages()
Section titled “extractMessages() ”protected function extractMessages(string $fileName, string $translator, array $ignoreCategories = []): arrayExtracts messages from a file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$fileName | string | Name of the file to extract messages from |
$translator | string | Name of the function used to translate messages |
$ignoreCategories | array | Message categories to ignore. This parameter is available since version 2.0.4. |
Returns
array
extractMessagesFromTokens()
Section titled “extractMessagesFromTokens() ”protected function extractMessagesFromTokens(array $tokens, array $translatorTokens, array $ignoreCategories): arrayExtracts messages from a parsed PHP tokens list.
Parameters
| Parameter | Type | Description |
|---|---|---|
$tokens | array | Tokens to be processed. |
$translatorTokens | array | Translator tokens. |
$ignoreCategories | array | Message categories to ignore. |
Returns
array: Messages.
getLine()
Section titled “getLine() ”protected function getLine(array $tokens): int|stringFinds out a line of the first non-char PHP token found.
Parameters
| Parameter | Type | Description |
|---|---|---|
$tokens | array |
Returns
int|string
Since
2.0.1
initConfig()
Section titled “initConfig() ”protected function initConfig(string $configFile): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$configFile | string |
Returns
mixed
Throws
yii\console\Exception: If configuration file does not exists.
Since
2.0.13
isCategoryIgnored()
Section titled “isCategoryIgnored() ”protected function isCategoryIgnored(string $category, array $ignoreCategories): boolThe method checks, whether the $category is ignored according to $ignoreCategories array.
Examples:
myapp- will be ignored onlymyappcategory;myapp*- will be ignored by all categories beginning withmyapp(myapp,myapplication,myapprove,myapp/widgets,myapp.widgets, etc).
Parameters
| Parameter | Type | Description |
|---|---|---|
$category | string | Category that is checked |
$ignoreCategories | array | Message categories to ignore. |
Returns
bool
Since
2.0.7
saveMessagesCategoryToPHP()
Section titled “saveMessagesCategoryToPHP() ”protected function saveMessagesCategoryToPHP(array $messages, string $fileName, bool $overwrite, bool $removeUnused, bool $sort, string $category, bool $markUnused): intWrites category messages into PHP file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array | |
$fileName | string | Name of the file to write to |
$overwrite | bool | If existing file should be overwritten without backup |
$removeUnused | bool | If obsolete translations should be removed |
$sort | bool | If translations should be sorted |
$category | string | Message category |
$markUnused | bool | If obsolete translations should be marked |
Returns
int: Exit code
saveMessagesToDb()
Section titled “saveMessagesToDb() ”protected function saveMessagesToDb(array $messages, yii\db\Connection $db, string $sourceMessageTable, string $messageTable, bool $removeUnused, array $languages, bool $markUnused): mixedSaves messages to database.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array | |
$db | yii\ | |
$sourceMessageTable | string | |
$messageTable | string | |
$removeUnused | bool | |
$languages | array | |
$markUnused | bool |
Returns
mixed
saveMessagesToPHP()
Section titled “saveMessagesToPHP() ”protected function saveMessagesToPHP(array $messages, string $dirName, bool $overwrite, bool $removeUnused, bool $sort, bool $markUnused): mixedWrites messages into PHP files.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array | |
$dirName | string | Name of the directory to write to |
$overwrite | bool | If existing file should be overwritten without backup |
$removeUnused | bool | If obsolete translations should be removed |
$sort | bool | If translations should be sorted |
$markUnused | bool | If obsolete translations should be marked |
Returns
mixed
saveMessagesToPO()
Section titled “saveMessagesToPO() ”protected function saveMessagesToPO(array $messages, string $dirName, bool $overwrite, bool $removeUnused, bool $sort, string $catalog, bool $markUnused): mixedWrites messages into PO file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array | |
$dirName | string | Name of the directory to write to |
$overwrite | bool | If existing file should be overwritten without backup |
$removeUnused | bool | If obsolete translations should be removed |
$sort | bool | If translations should be sorted |
$catalog | string | Message catalog |
$markUnused | bool | If obsolete translations should be marked |
Returns
mixed
saveMessagesToPOT()
Section titled “saveMessagesToPOT() ”protected function saveMessagesToPOT(array $messages, string $dirName, string $catalog): mixedWrites messages into POT file.
Parameters
| Parameter | Type | Description |
|---|---|---|
$messages | array | |
$dirName | string | Name of the directory to write to |
$catalog | string | Message catalog |
Returns
mixed
Since
2.0.6
tokensEqual()
Section titled “tokensEqual() ”protected function tokensEqual(array|string $a, array|string $b): boolFinds out if two PHP tokens are equal.
Parameters
| Parameter | Type | Description |
|---|---|---|
$a | array| | |
$b | array| |
Returns
bool
Since
2.0.1