Skip to content

Commit

Permalink
Fixes issue #251: changed the signature of t() to be the same as v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed May 15, 2013
1 parent fd7dbba commit fb5993a
Show file tree
Hide file tree
Showing 43 changed files with 89 additions and 103 deletions.
4 changes: 0 additions & 4 deletions docs/guide/upgrade-from-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,6 @@ Message translation is still supported, but managed via the "i18n" application c
The component manages a set of message sources, which allows you to use different message
sources based on message categories. For more information, see the class documentation for `I18N`.

The message translation method is changed by merging the message category into the message being
translated. For example, `Yii::t('yii|message to be translated')`.



Action Filters
--------------
Expand Down
15 changes: 6 additions & 9 deletions yii/YiiBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,9 @@ public static function powered()
/**
* Translates a message to the specified language.
*
* The translation will be conducted according to the message category and the target language.
* To specify the category of the message, prefix the message with the category name and separate it
* with "|". For example, "app|hello world". If the category is not specified, the default category "app"
* will be used. The actual message translation is done by a [[\yii\i18n\MessageSource|message source]].
* This is a shortcut method of [[\yii\i18n\I18N::translate()]].
*
* The translation will be conducted according to the message category and the target language will be used.
*
* In case when a translated message has different plural forms (separated by "|"), this method
* will also attempt to choose an appropriate one according to a given numeric value which is
Expand All @@ -595,20 +594,18 @@ public static function powered()
* For more details on how plural rules are applied, please refer to:
* [[http://www.unicode.org/cldr/charts/supplemental/language_plural_rules.html]]
*
* @param string $category the message category.
* @param string $message the message to be translated.
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
* @param string $language the language code (e.g. `en_US`, `en`). If this is null, the current
* [[\yii\base\Application::language|application language]] will be used.
* @return string the translated message.
*/
public static function t($message, $params = array(), $language = null)
public static function t($category, $message, $params = array(), $language = null)
{
if (self::$app !== null) {
return self::$app->getI18N()->translate($message, $params, $language);
return self::$app->getI18N()->translate($category, $message, $params, $language);
} else {
if (strpos($message, '|') !== false && preg_match('/^([\w\-\\/\.\\\\]+)\|(.*)/', $message, $matches)) {
$message = $matches[2];
}
return is_array($params) ? strtr($message, $params) : $message;
}
}
Expand Down
2 changes: 1 addition & 1 deletion yii/base/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function bindActionParams($action, $params)
}

if (!empty($missing)) {
throw new InvalidRequestException(Yii::t('yii|Missing required parameters: {params}', array(
throw new InvalidRequestException(Yii::t('yii', 'Missing required parameters: {params}', array(
'{params}' => implode(', ', $missing),
)));
}
Expand Down
28 changes: 14 additions & 14 deletions yii/base/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ public static function isFatalError($error)
public function getName()
{
$names = array(
E_ERROR => Yii::t('yii|Fatal Error'),
E_PARSE => Yii::t('yii|Parse Error'),
E_CORE_ERROR => Yii::t('yii|Core Error'),
E_COMPILE_ERROR => Yii::t('yii|Compile Error'),
E_USER_ERROR => Yii::t('yii|User Error'),
E_WARNING => Yii::t('yii|Warning'),
E_CORE_WARNING => Yii::t('yii|Core Warning'),
E_COMPILE_WARNING => Yii::t('yii|Compile Warning'),
E_USER_WARNING => Yii::t('yii|User Warning'),
E_STRICT => Yii::t('yii|Strict'),
E_NOTICE => Yii::t('yii|Notice'),
E_RECOVERABLE_ERROR => Yii::t('yii|Recoverable Error'),
E_DEPRECATED => Yii::t('yii|Deprecated'),
E_ERROR => Yii::t('yii', 'Fatal Error'),
E_PARSE => Yii::t('yii', 'Parse Error'),
E_CORE_ERROR => Yii::t('yii', 'Core Error'),
E_COMPILE_ERROR => Yii::t('yii', 'Compile Error'),
E_USER_ERROR => Yii::t('yii', 'User Error'),
E_WARNING => Yii::t('yii', 'Warning'),
E_CORE_WARNING => Yii::t('yii', 'Core Warning'),
E_COMPILE_WARNING => Yii::t('yii', 'Compile Warning'),
E_USER_WARNING => Yii::t('yii', 'User Warning'),
E_STRICT => Yii::t('yii', 'Strict'),
E_NOTICE => Yii::t('yii', 'Notice'),
E_RECOVERABLE_ERROR => Yii::t('yii', 'Recoverable Error'),
E_DEPRECATED => Yii::t('yii', 'Deprecated'),
);
return isset($names[$this->getCode()]) ? $names[$this->getCode()] : Yii::t('yii|Error');
return isset($names[$this->getCode()]) ? $names[$this->getCode()] : Yii::t('yii', 'Error');
}
}
2 changes: 1 addition & 1 deletion yii/base/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Exception extends \Exception
*/
public function getName()
{
return \Yii::t('yii|Exception');
return \Yii::t('yii', 'Exception');
}
}
2 changes: 1 addition & 1 deletion yii/base/HttpException.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function getName()
if (isset($httpCodes[$this->statusCode])) {
return $httpCodes[$this->statusCode];
} else {
return \Yii::t('yii|Error');
return \Yii::t('yii', 'Error');
}
}
}
2 changes: 1 addition & 1 deletion yii/base/InvalidCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InvalidCallException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Invalid Call');
return \Yii::t('yii', 'Invalid Call');
}
}

2 changes: 1 addition & 1 deletion yii/base/InvalidConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InvalidConfigException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Invalid Configuration');
return \Yii::t('yii', 'Invalid Configuration');
}
}

2 changes: 1 addition & 1 deletion yii/base/InvalidParamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InvalidParamException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Invalid Parameter');
return \Yii::t('yii', 'Invalid Parameter');
}
}

2 changes: 1 addition & 1 deletion yii/base/InvalidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InvalidRequestException extends UserException
*/
public function getName()
{
return \Yii::t('yii|Invalid Request');
return \Yii::t('yii', 'Invalid Request');
}
}

2 changes: 1 addition & 1 deletion yii/base/InvalidRouteException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InvalidRouteException extends UserException
*/
public function getName()
{
return \Yii::t('yii|Invalid Route');
return \Yii::t('yii', 'Invalid Route');
}
}

2 changes: 1 addition & 1 deletion yii/base/NotSupportedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class NotSupportedException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Not Supported');
return \Yii::t('yii', 'Not Supported');
}
}

2 changes: 1 addition & 1 deletion yii/base/UnknownClassException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnknownClassException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Unknown Class');
return \Yii::t('yii', 'Unknown Class');
}
}

2 changes: 1 addition & 1 deletion yii/base/UnknownMethodException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnknownMethodException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Unknown Method');
return \Yii::t('yii', 'Unknown Method');
}
}

2 changes: 1 addition & 1 deletion yii/base/UnknownPropertyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UnknownPropertyException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Unknown Property');
return \Yii::t('yii', 'Unknown Property');
}
}

4 changes: 2 additions & 2 deletions yii/console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function processRequest()
list ($route, $params) = $request->resolve();
return $this->runAction($route, $params);
} else {
throw new Exception(\Yii::t('yii|This script must be run from the command line.'));
throw new Exception(\Yii::t('yii', 'This script must be run from the command line.'));
}
}

Expand All @@ -113,7 +113,7 @@ public function runAction($route, $params = array())
try {
return parent::runAction($route, $params);
} catch (InvalidRouteException $e) {
throw new Exception(\Yii::t('yii|Unknown command "{command}".', array('{command}' => $route)));
throw new Exception(\Yii::t('yii', 'Unknown command "{command}".', array('{command}' => $route)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions yii/console/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function bindActionParams($action, $params)
$args = isset($params[Request::ANONYMOUS_PARAMS]) ? $params[Request::ANONYMOUS_PARAMS] : array();
unset($params[Request::ANONYMOUS_PARAMS]);
if (!empty($params)) {
throw new Exception(Yii::t('yii|Unknown options: {params}', array(
throw new Exception(Yii::t('yii', 'Unknown options: {params}', array(
'{params}' => implode(', ', array_keys($params)),
)));
}
Expand All @@ -115,7 +115,7 @@ public function bindActionParams($action, $params)
}

if (!empty($missing)) {
throw new Exception(Yii::t('yii|Missing required arguments: {params}', array(
throw new Exception(Yii::t('yii', 'Missing required arguments: {params}', array(
'{params}' => implode(', ', $missing),
)));
}
Expand Down
2 changes: 1 addition & 1 deletion yii/console/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Exception extends UserException
*/
public function getName()
{
return \Yii::t('yii|Error');
return \Yii::t('yii', 'Error');
}
}

4 changes: 2 additions & 2 deletions yii/console/controllers/HelpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function actionIndex($command = null)
if ($command !== null) {
$result = Yii::$app->createController($command);
if ($result === false) {
throw new Exception(Yii::t('yii|No help for unknown command "{command}".', array(
throw new Exception(Yii::t('yii', 'No help for unknown command "{command}".', array(
'{command}' => $command,
)));
}
Expand Down Expand Up @@ -239,7 +239,7 @@ protected function getActionHelp($controller, $actionID)
{
$action = $controller->createAction($actionID);
if ($action === null) {
throw new Exception(Yii::t('yii|No help for unknown sub-command "{command}".', array(
throw new Exception(Yii::t('yii', 'No help for unknown sub-command "{command}".', array(
'{command}' => rtrim($controller->getUniqueId() . '/' . $actionID, '/'),
)));
}
Expand Down
2 changes: 1 addition & 1 deletion yii/db/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public function __construct($message, $errorInfo = null, $code = 0, \Exception $
*/
public function getName()
{
return \Yii::t('yii|Database Exception');
return \Yii::t('yii', 'Database Exception');
}
}
2 changes: 1 addition & 1 deletion yii/db/StaleObjectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ class StaleObjectException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Stale Object Exception');
return \Yii::t('yii', 'Stale Object Exception');
}
}
11 changes: 2 additions & 9 deletions yii/i18n/I18N.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,19 @@ public function init()
* Translates a message to the specified language.
* If the first parameter in `$params` is a number and it is indexed by 0, appropriate plural rules
* will be applied to the translated message.
* @param string $category the message category.
* @param string $message the message to be translated.
* @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
* @param string $language the language code (e.g. `en_US`, `en`). If this is null, the current
* [[\yii\base\Application::language|application language]] will be used.
* @return string the translated message.
*/
public function translate($message, $params = array(), $language = null)
public function translate($category, $message, $params = array(), $language = null)
{
if ($language === null) {
$language = Yii::$app->language;
}

// allow chars for category: word chars, ".", "-", "/", "\"
if (strpos($message, '|') !== false && preg_match('/^([\w\-\\/\.\\\\]+)\|(.*)/', $message, $matches)) {
$category = $matches[1];
$message = $matches[2];
} else {
$category = 'app';
}

$message = $this->getMessageSource($category)->translate($category, $message, $language);

if (!is_array($params)) {
Expand Down
2 changes: 1 addition & 1 deletion yii/logging/EmailTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function export($messages)
$body .= $this->formatMessage($message);
}
$body = wordwrap($body, 70);
$subject = $this->subject === null ? \Yii::t('yii|Application Log') : $this->subject;
$subject = $this->subject === null ? \Yii::t('yii', 'Application Log') : $this->subject;
foreach ($this->emails as $email) {
$this->sendEmail($subject, $body, $email, $this->sentFrom, $this->headers);
}
Expand Down
6 changes: 3 additions & 3 deletions yii/logging/ProfileTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function setReport($value)
if ($value === 'summary' || $value === 'callstack')
$this->_report = $value;
else
throw new CException(Yii::t('yii|CProfileLogRoute.report "{report}" is invalid. Valid values include "summary" and "callstack".',
throw new CException(Yii::t('yii', 'CProfileLogRoute.report "{report}" is invalid. Valid values include "summary" and "callstack".',
array('{report}' => $value)));
}

Expand Down Expand Up @@ -106,7 +106,7 @@ protected function displayCallstack($logs)
$results[$last[4]] = array($token, $delta, count($stack));
} else
{
throw new CException(Yii::t('yii|CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
throw new CException(Yii::t('yii', 'CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
array('{token}' => $token)));
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ protected function displaySummary($logs)
else
$results[$token] = array($token, 1, $delta, $delta, $delta);
} else
throw new CException(Yii::t('yii|CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
throw new CException(Yii::t('yii', 'CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
array('{token}' => $token)));
}
}
Expand Down
2 changes: 1 addition & 1 deletion yii/validators/BooleanValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} must be either "{true}" or "{false}".');
$this->message = Yii::t('yii', '{attribute} must be either "{true}" or "{false}".');
}
}

Expand Down
2 changes: 1 addition & 1 deletion yii/validators/CaptchaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|The verification code is incorrect.');
$this->message = Yii::t('yii', 'The verification code is incorrect.');
}
}

Expand Down
18 changes: 9 additions & 9 deletions yii/validators/CompareValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,28 @@ public function init()
if ($this->message === null) {
switch ($this->operator) {
case '==':
$this->message = Yii::t('yii|{attribute} must be repeated exactly.');
$this->message = Yii::t('yii', '{attribute} must be repeated exactly.');
break;
case '===':
$this->message = Yii::t('yii|{attribute} must be repeated exactly.');
$this->message = Yii::t('yii', '{attribute} must be repeated exactly.');
break;
case '!=':
$this->message = Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
$this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
break;
case '!==':
$this->message = Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
$this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".');
break;
case '>':
$this->message = Yii::t('yii|{attribute} must be greater than "{compareValue}".');
$this->message = Yii::t('yii', '{attribute} must be greater than "{compareValue}".');
break;
case '>=':
$this->message = Yii::t('yii|{attribute} must be greater than or equal to "{compareValue}".');
$this->message = Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".');
break;
case '<':
$this->message = Yii::t('yii|{attribute} must be less than "{compareValue}".');
$this->message = Yii::t('yii', '{attribute} must be less than "{compareValue}".');
break;
case '<=':
$this->message = Yii::t('yii|{attribute} must be less than or equal to "{compareValue}".');
$this->message = Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".');
break;
default:
throw new InvalidConfigException("Unknown operator: {$this->operator}");
Expand All @@ -119,7 +119,7 @@ public function validateAttribute($object, $attribute)
{
$value = $object->$attribute;
if (is_array($value)) {
$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.'));
$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
return;
}
if ($this->compareValue !== null) {
Expand Down
Loading

1 comment on commit fb5993a

@lucianobaraglia
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice move! 👍

Please sign in to comment.