Class yii\console\controllers\MessageController

继承yii\console\controllers\MessageController » yii\console\Controller » yii\base\Controller » yii\base\Component » yii\base\BaseObject
实现yii\base\Configurable, yii\base\ViewContextInterface
可用自版本2.0
源代码 https://github.com/yiisoft/yii2/blob/master/framework/console/controllers/MessageController.php

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:

  1. Create a configuration file using the 'message/config' command: yii message/config /path/to/myapp/messages/config.php
  2. Edit the created config file, adjusting it for your web application needs.
  3. Run the 'message/extract' command, using created config: yii message /path/to/myapp/messages/config.php

公共属性

隐藏继承的属性

属性类型描述定义在
$action yii\base\Action|null 当前正在执行的动作. yii\base\Controller
$behaviors yii\base\Behavior[] 附加到此组件的行为列表. 该属性是只读的. yii\base\Component
$catalog string Name of the file that will be used for translations for "po" format. yii\console\controllers\MessageController
$color boolean|null 是否在输出中启用 ANSI 颜色. yii\console\Controller
$db string Connection component ID for "db" format. yii\console\controllers\MessageController
$defaultAction string Controller default action ID. yii\console\controllers\MessageController
$except array List of patterns that specify which files/directories should NOT be processed. yii\console\controllers\MessageController
$format string Generated file format. yii\console\controllers\MessageController
$help boolean 是否显示有关当前命令的帮助信息. yii\console\Controller
$helpSummary string 该属性是只读的. yii\console\Controller
$id string 该控制器的ID. yii\base\Controller
$ignoreCategories array Message categories to ignore. yii\console\controllers\MessageController
$interactive boolean 是否以交互方式运行命令. yii\console\Controller
$languages array Required, list of language codes that the extracted messages should be translated to. yii\console\controllers\MessageController
$layout null|string|false 要应用于此控制器的视图的布局的名称. yii\base\Controller
$markUnused boolean Whether to mark messages that no longer appear in the source code. yii\console\controllers\MessageController
$messagePath string Required, root directory containing message translations. yii\console\controllers\MessageController
$messageTable string Custom name for translation message table for "db" format. yii\console\controllers\MessageController
$module yii\base\Module 该控制器所属的模块. yii\base\Controller
$modules yii\base\Module[] 该控制器所在的所有祖先模块. 该属性是只读的. yii\base\Controller
$only array List of patterns that specify which files (not directories) should be processed. yii\console\controllers\MessageController
$overwrite boolean Whether the message file should be overwritten with the merged messages yii\console\controllers\MessageController
$passedOptionValues array 与传递的选项相对应的属性. 该属性是只读的. yii\console\Controller
$passedOptions array 执行期间传递的选项名称. 该属性是只读的. yii\console\Controller
$phpDocBlock string|null DocBlock used for messages array in generated PHP file. yii\console\controllers\MessageController
$phpFileHeader string File header in generated PHP file with messages. yii\console\controllers\MessageController
$removeUnused boolean Whether to remove messages that no longer appear in the source code. yii\console\controllers\MessageController
$request yii\console\Request yii\console\Controller
$response yii\console\Response yii\console\Controller
$route string 当前请求的路由(模块ID, 控制器ID和动作ID). 该属性是只读的. yii\base\Controller
$silentExitOnException boolean|null 如果为true, 脚本在出现异常的情况下以ExitCode::OK结尾; 如果为false, ExitCode::UNSPECIFIED_ERROR. yii\console\Controller
$sort boolean Whether to sort messages by keys when merging new messages with the existing ones. yii\console\controllers\MessageController
$sourceMessageTable string Custom name for source message table for "db" format. yii\console\controllers\MessageController
$sourcePath string Required, root directory of all source files. yii\console\controllers\MessageController
$translator string The name of the function for translating messages. yii\console\controllers\MessageController
$uniqueId string 以模块ID(如果有)为前缀的控制器ID. 该属性是只读的. yii\base\Controller
$view yii\base\View|yii\web\View 可用于渲染视图或视图文件的视图对象. yii\base\Controller
$viewPath string 包含此控制器的视图文件的目录. yii\base\Controller

受保护的属性

隐藏继承的属性

属性类型描述定义在
$config array Config for messages extraction. yii\console\controllers\MessageController

公共方法

隐藏继承的方法

方法描述定义在
__call() 调用不是类方法的指定的方法. yii\base\Component
__clone() 在通过克隆现有对象创建对象之后调用此方法. yii\base\Component
__construct() 构造函数. yii\base\Controller
__get() 返回组件属性的值. yii\base\Component
__isset() 检查属性是否已设置, 即已定义且不为null. yii\base\Component
__set() 设置组件属性的值. yii\base\Component
__unset() 将组件属性设置为null. yii\base\Component
actionConfig() Creates a configuration file for the "extract" command using command line options specified. yii\console\controllers\MessageController
actionConfigTemplate() Creates a configuration file template for the "extract" command. yii\console\controllers\MessageController
actionExtract() Extracts messages to be translated from source code. yii\console\controllers\MessageController
actions() 为控制器声明外部动作. yii\base\Controller
afterAction() 执行动作后立即调用该方法. yii\base\Controller
ansiFormat() 使用 ANSI 代码格式化字符串. yii\console\Controller
attachBehavior() 将行为附加到此组件. yii\base\Component
attachBehaviors() 将行为列表附加到组件. yii\base\Component
beforeAction() 在执行动作之前立即调用该方法. yii\console\Controller
behaviors() 返回此组件应该具有的行为列表. yii\base\Component
bindActionParams() 将参数绑定到动作. yii\console\Controller
canGetProperty() 返回一个值, 指示属性是否可读取. yii\base\Component
canSetProperty() 返回一个值, 指示属性是否可设置. yii\base\Component
className() 返回此类的完全限定名称. yii\base\BaseObject
confirm() 要求用户输入yn进行确认. yii\console\Controller
createAction() 根据给定的动作ID创建一个动作. yii\base\Controller
detachBehavior() 从组件中分离一个行为. yii\base\Component
detachBehaviors() 分离组件中的所有行为. yii\base\Component
ensureBehaviors() 确保在 behaviors() 中声明的行为已附加到此组件. yii\base\Component
findLayoutFile() 查找适用的布局文件. yii\base\Controller
getActionArgsHelp() 返回动作的匿名参数的帮助信息. yii\console\Controller
getActionHelp() 返回指定动作的详细帮助信息. yii\console\Controller
getActionHelpSummary() 返回描述指定动作的单行简单摘要. yii\console\Controller
getActionOptionsHelp() 返回动作选项的帮助信息. yii\console\Controller
getBehavior() 返回指定的行为对象. yii\base\Component
getBehaviors() 返回附加到此组件的所有行为. yii\base\Component
getHelp() 返回此控制器的帮助信息. yii\console\Controller
getHelpSummary() 返回描述此控制器的单行简单摘要. yii\console\Controller
getModules() 返回此控制器的所有祖先模块. yii\base\Controller
getOptionValues() 返回与动作id的选项相对应的属性. yii\console\Controller
getPassedOptionValues() 返回与传递的选项相对应的属性. yii\console\Controller
getPassedOptions() 返回执行期间传递的选项的名称. yii\console\Controller
getRoute() 返回当前请求的路由. yii\base\Controller
getUniqueId() 返回控制器的唯一ID. yii\base\Controller
getView() 返回可用于渲染视图或视图文件的视图对象. yii\base\Controller
getViewPath() 返回包含此控制器的视图文件的目录. yii\base\Controller
hasEventHandlers() 返回一个值, 指示是否有任何处理程序附加到指定的事件. yii\base\Component
hasMethod() 返回一个值, 指示方法是否已定义. yii\base\Component
hasProperty() 返回一个值, 指示属性是否已定义. yii\base\Component
init() 初始化对象. yii\base\Controller
isColorEnabled() 返回一个值, 该值指示是否启用 ANSI 颜色. yii\console\Controller
off() 从该组件分离现有的事件处理程序. yii\base\Component
on() 将事件处理程序附加到事件. yii\base\Component
optionAliases() 返回选项别名. yii\console\controllers\MessageController
options() 返回动作的有效操作选项. yii\console\controllers\MessageController
prompt() 提示用户输入并验证输入. yii\console\Controller
render() 渲染视图并应用布局(如果可用). yii\base\Controller
renderContent() 通过应用布局渲染静态字符串. yii\base\Controller
renderFile() 渲染视图文件. yii\base\Controller
renderPartial() 渲染视图而不使用布局. yii\base\Controller
run() 运行根据路由指定的请求. yii\base\Controller
runAction() 使用指定的动作id和参数运行一个动作. yii\console\Controller
select() 给用户一个选择的选项. yii\console\Controller
setView() 设置此控制器使用的视图对象. yii\base\Controller
setViewPath() 设置包含视图文件的目录. yii\base\Controller
stderr() 将字符串打印到 STDERR. yii\console\Controller
stdout() 将字符串打印到 STDOUT. yii\console\Controller
trigger() 触发事件. yii\base\Component

受保护的方法

隐藏继承的方法

方法描述定义在
bindInjectedParams() 根据操作方法签名中的类型和名称填充参数. yii\base\Controller
extractMessages() Extracts messages from a file. yii\console\controllers\MessageController
extractMessagesFromTokens() Extracts messages from a parsed PHP tokens list. yii\console\controllers\MessageController
getActionMethodReflection() yii\console\Controller
getLine() Finds out a line of the first non-char PHP token found. yii\console\controllers\MessageController
initConfig() yii\console\controllers\MessageController
isCategoryIgnored() The method checks, whether the $category is ignored according to $ignoreCategories array. yii\console\controllers\MessageController
parseDocCommentDetail() 从文档块返回完整描述. yii\console\Controller
parseDocCommentSummary() 返回文档块的第一行. yii\console\Controller
parseDocCommentTags() 将注释块解析为标签. yii\console\Controller
saveMessagesCategoryToPHP() Writes category messages into PHP file. yii\console\controllers\MessageController
saveMessagesToDb() Saves messages to database. yii\console\controllers\MessageController
saveMessagesToPHP() Writes messages into PHP files. yii\console\controllers\MessageController
saveMessagesToPO() Writes messages into PO file. yii\console\controllers\MessageController
saveMessagesToPOT() Writes messages into POT file. yii\console\controllers\MessageController
tokensEqual() Finds out if two PHP tokens are equal. yii\console\controllers\MessageController

事件

隐藏继承的事件

事件类型描述定义在
EVENT_AFTER_ACTION yii\base\ActionEvent 执行控制器动作后立即触发的事件. yii\base\Controller
EVENT_BEFORE_ACTION yii\base\ActionEvent 在执行控制器动作之前触发的事件. yii\base\Controller

常量

隐藏继承的常量

常量描述定义在
EXIT_CODE_ERROR 1 Deprecated since 2.0.13. 改用 yii\console\ExitCode::UNSPECIFIED_ERROR. yii\console\Controller
EXIT_CODE_NORMAL 0 Deprecated since 2.0.13. 改用 yii\console\ExitCode::OK. yii\console\Controller

属性详情

$catalog public 属性

Name of the file that will be used for translations for "po" format.

public string $catalog 'messages'
$config protected 属性 (可用自版本: 2.0.13)

Config for messages extraction.

参见:

protected array $config null
$db public 属性

Connection component ID for "db" format.

public string $db 'db'
$defaultAction public 属性

Controller default action ID.

public string $defaultAction 'extract'
$except public 属性

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.

public array $except = ['.svn''.git''.gitignore''.gitkeep''.hgignore''.hgkeep''/messages''/BaseYii.php']
$format public 属性

Generated file format. Can be "php", "db", "po" or "pot".

public string $format 'php'
$ignoreCategories public 属性

Message categories to ignore. For example, 'yii', 'app*', 'widgets/menu', etc.

参见 isCategoryIgnored().

$languages public 属性

Required, list of language codes that the extracted messages should be translated to. For example, ['zh-CN', 'de'].

public array $languages = []
$markUnused public 属性

Whether 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.

public boolean $markUnused true
$messagePath public 属性

Required, root directory containing message translations.

public string $messagePath '@yii/messages'
$messageTable public 属性

Custom name for translation message table for "db" format.

public string $messageTable '{{%message}}'
$only public 属性

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.

public array $only = ['*.php']
$overwrite public 属性

Whether the message file should be overwritten with the merged messages

public boolean $overwrite true
$phpDocBlock public 属性 (可用自版本: 2.0.13)

DocBlock used for messages array in generated PHP file. If null, default DocBlock will be used. This property is used only if $format is "php".

public string|null $phpDocBlock null
$phpFileHeader public 属性 (可用自版本: 2.0.13)

File header in generated PHP file with messages. This property is used only if $format is "php".

public string $phpFileHeader ''
$removeUnused public 属性

Whether to remove messages that no longer appear in the source code. Defaults to false, which means these messages will NOT be removed.

public boolean $removeUnused false
$sort public 属性

Whether 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.

public boolean $sort false
$sourceMessageTable public 属性

Custom name for source message table for "db" format.

public string $sourceMessageTable '{{%source_message}}'
$sourcePath public 属性

Required, root directory of all source files.

public string $sourcePath '@yii'
$translator public 属性

The name of the function for translating messages. Defaults to 'Yii::t'. 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.

public string $translator 'Yii::t'

方法详情

actionConfig() public 方法

Creates 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.

public integer actionConfig ( $filePath )
$filePath string

Output file name or alias.

return integer

CLI exit code

throws yii\console\Exception

on failure.

actionConfigTemplate() public 方法

Creates 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.

public integer actionConfigTemplate ( $filePath )
$filePath string

Output file name or alias.

return integer

CLI exit code

throws yii\console\Exception

on failure.

actionExtract() public 方法

Extracts 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.

public void actionExtract ( $configFile null )
$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.

throws yii\console\Exception

on failure.

extractMessages() protected 方法

Extracts messages from a file.

protected array extractMessages ( $fileName, $translator, $ignoreCategories = [] )
$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.

extractMessagesFromTokens() protected 方法

Extracts messages from a parsed PHP tokens list.

protected array extractMessagesFromTokens ( array $tokens, array $translatorTokens, array $ignoreCategories )
$tokens array

Tokens to be processed.

$translatorTokens array

Translator tokens.

$ignoreCategories array

Message categories to ignore.

return array

Messages.

getLine() protected 方法 (可用自版本: 2.0.1)

Finds out a line of the first non-char PHP token found.

protected integer|string getLine ( $tokens )
$tokens array
initConfig() protected 方法 (可用自版本: 2.0.13)

protected void initConfig ( $configFile )
$configFile string
throws yii\console\Exception

If configuration file does not exists.

isCategoryIgnored() protected 方法 (可用自版本: 2.0.7)

The method checks, whether the $category is ignored according to $ignoreCategories array.

Examples:

  • myapp - will be ignored only myapp category;
  • myapp* - will be ignored by all categories beginning with myapp (myapp, myapplication, myapprove, myapp/widgets, myapp.widgets, etc).
protected boolean isCategoryIgnored ( $category, array $ignoreCategories )
$category string

Category that is checked

$ignoreCategories array

Message categories to ignore.

optionAliases() public 方法 (可用自版本: 2.0.8)

返回选项别名.

子类可以重写此方法以指定别名选项.

public array optionAliases ( )
return array

对动作有效的选项别名. 其中键是选项的别名, 值是选项的名称.

options() public 方法

返回动作的有效操作选项.

一个选项需要存在一个公共成员变量, 其名称为选项名称. 子类可以重写此方法以指定可能的选项.

Note: 只有在调用 beforeAction() 之后, 才能通过选项设置值.

public string[] options ( $actionID )
$actionID string

当前请求的动作id

return string[]

动作的有效操作选项.

saveMessagesCategoryToPHP() protected 方法

Writes category messages into PHP file.

protected integer saveMessagesCategoryToPHP ( $messages, $fileName, $overwrite, $removeUnused, $sort, $category, $markUnused )
$messages array
$fileName string

Name of the file to write to

$overwrite boolean

If existing file should be overwritten without backup

$removeUnused boolean

If obsolete translations should be removed

$sort boolean

If translations should be sorted

$category string

Message category

$markUnused boolean

If obsolete translations should be marked

return integer

Exit code

saveMessagesToDb() protected 方法

Saves messages to database.

protected void saveMessagesToDb ( $messages, $db, $sourceMessageTable, $messageTable, $removeUnused, $languages, $markUnused )
$messages array
$db yii\db\Connection
$sourceMessageTable string
$messageTable string
$removeUnused boolean
$languages array
$markUnused boolean
saveMessagesToPHP() protected 方法

Writes messages into PHP files.

protected void saveMessagesToPHP ( $messages, $dirName, $overwrite, $removeUnused, $sort, $markUnused )
$messages array
$dirName string

Name of the directory to write to

$overwrite boolean

If existing file should be overwritten without backup

$removeUnused boolean

If obsolete translations should be removed

$sort boolean

If translations should be sorted

$markUnused boolean

If obsolete translations should be marked

saveMessagesToPO() protected 方法

Writes messages into PO file.

protected void saveMessagesToPO ( $messages, $dirName, $overwrite, $removeUnused, $sort, $catalog, $markUnused )
$messages array
$dirName string

Name of the directory to write to

$overwrite boolean

If existing file should be overwritten without backup

$removeUnused boolean

If obsolete translations should be removed

$sort boolean

If translations should be sorted

$catalog string

Message catalog

$markUnused boolean

If obsolete translations should be marked

saveMessagesToPOT() protected 方法 (可用自版本: 2.0.6)

Writes messages into POT file.

protected void saveMessagesToPOT ( $messages, $dirName, $catalog )
$messages array
$dirName string

Name of the directory to write to

$catalog string

Message catalog

tokensEqual() protected 方法 (可用自版本: 2.0.1)

Finds out if two PHP tokens are equal.

protected boolean tokensEqual ( $a, $b )
$a array|string
$b array|string