Commit fb5993af by Qiang Xue

Fixes issue #251: changed the signature of t() to be the same as v1.1

parent fd7dbba7
......@@ -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
--------------
......
......@@ -579,10 +579,9 @@ class YiiBase
/**
* 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
......@@ -595,20 +594,18 @@ class YiiBase
* 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;
}
}
......
......@@ -183,7 +183,7 @@ class Controller extends Component
}
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),
)));
}
......
......@@ -90,20 +90,20 @@ class ErrorException extends Exception
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');
}
}
......@@ -20,6 +20,6 @@ class Exception extends \Exception
*/
public function getName()
{
return \Yii::t('yii|Exception');
return \Yii::t('yii', 'Exception');
}
}
......@@ -103,7 +103,7 @@ class HttpException extends UserException
if (isset($httpCodes[$this->statusCode])) {
return $httpCodes[$this->statusCode];
} else {
return \Yii::t('yii|Error');
return \Yii::t('yii', 'Error');
}
}
}
......@@ -20,7 +20,7 @@ class InvalidCallException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Invalid Call');
return \Yii::t('yii', 'Invalid Call');
}
}
......@@ -20,7 +20,7 @@ class InvalidConfigException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Invalid Configuration');
return \Yii::t('yii', 'Invalid Configuration');
}
}
......@@ -20,7 +20,7 @@ class InvalidParamException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Invalid Parameter');
return \Yii::t('yii', 'Invalid Parameter');
}
}
......@@ -20,7 +20,7 @@ class InvalidRequestException extends UserException
*/
public function getName()
{
return \Yii::t('yii|Invalid Request');
return \Yii::t('yii', 'Invalid Request');
}
}
......@@ -20,7 +20,7 @@ class InvalidRouteException extends UserException
*/
public function getName()
{
return \Yii::t('yii|Invalid Route');
return \Yii::t('yii', 'Invalid Route');
}
}
......@@ -20,7 +20,7 @@ class NotSupportedException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Not Supported');
return \Yii::t('yii', 'Not Supported');
}
}
......@@ -20,7 +20,7 @@ class UnknownClassException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Unknown Class');
return \Yii::t('yii', 'Unknown Class');
}
}
......@@ -20,7 +20,7 @@ class UnknownMethodException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Unknown Method');
return \Yii::t('yii', 'Unknown Method');
}
}
......@@ -20,7 +20,7 @@ class UnknownPropertyException extends Exception
*/
public function getName()
{
return \Yii::t('yii|Unknown Property');
return \Yii::t('yii', 'Unknown Property');
}
}
......@@ -94,7 +94,7 @@ class Application extends \yii\base\Application
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.'));
}
}
......@@ -113,7 +113,7 @@ class Application extends \yii\base\Application
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)));
}
}
......
......@@ -91,7 +91,7 @@ class Controller extends \yii\base\Controller
$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)),
)));
}
......@@ -115,7 +115,7 @@ class Controller extends \yii\base\Controller
}
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),
)));
}
......
......@@ -22,7 +22,7 @@ class Exception extends UserException
*/
public function getName()
{
return \Yii::t('yii|Error');
return \Yii::t('yii', 'Error');
}
}
......@@ -55,7 +55,7 @@ class HelpController extends Controller
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,
)));
}
......@@ -239,7 +239,7 @@ class HelpController extends Controller
{
$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, '/'),
)));
}
......
......@@ -39,6 +39,6 @@ class Exception extends \yii\base\Exception
*/
public function getName()
{
return \Yii::t('yii|Database Exception');
return \Yii::t('yii', 'Database Exception');
}
}
......@@ -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');
}
}
......@@ -75,26 +75,19 @@ class I18N extends Component
* 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)) {
......
......@@ -48,7 +48,7 @@ class EmailTarget extends Target
$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);
}
......
......@@ -59,7 +59,7 @@ class CProfileLogRoute extends CWebLogRoute
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)));
}
......@@ -106,7 +106,7 @@ class CProfileLogRoute extends CWebLogRoute
$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)));
}
}
......@@ -149,7 +149,7 @@ class CProfileLogRoute extends CWebLogRoute
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)));
}
}
......
......@@ -43,7 +43,7 @@ class BooleanValidator extends Validator
{
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}".');
}
}
......
......@@ -39,7 +39,7 @@ class CaptchaValidator extends Validator
{
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.');
}
}
......
......@@ -79,28 +79,28 @@ class CompareValidator extends Validator
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}");
......@@ -119,7 +119,7 @@ class CompareValidator extends Validator
{
$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) {
......
......@@ -38,7 +38,7 @@ class DateValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|The format of {attribute} is invalid.');
$this->message = Yii::t('yii', 'The format of {attribute} is invalid.');
}
}
......
......@@ -55,7 +55,7 @@ class EmailValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is not a valid email address.');
$this->message = Yii::t('yii', '{attribute} is not a valid email address.');
}
}
......
......@@ -45,7 +45,7 @@ class ExistValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is invalid.');
$this->message = Yii::t('yii', '{attribute} is invalid.');
}
}
......
......@@ -97,22 +97,22 @@ class FileValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|File upload failed.');
$this->message = Yii::t('yii', 'File upload failed.');
}
if ($this->uploadRequired === null) {
$this->uploadRequired = Yii::t('yii|Please upload a file.');
$this->uploadRequired = Yii::t('yii', 'Please upload a file.');
}
if ($this->tooMany === null) {
$this->tooMany = Yii::t('yii|You can upload at most {limit} files.');
$this->tooMany = Yii::t('yii', 'You can upload at most {limit} files.');
}
if ($this->wrongType === null) {
$this->wrongType = Yii::t('yii|Only files with these extensions are allowed: {extensions}.');
$this->wrongType = Yii::t('yii', 'Only files with these extensions are allowed: {extensions}.');
}
if ($this->tooBig === null) {
$this->tooBig = Yii::t('yii|The file "{file}" is too big. Its size cannot exceed {limit} bytes.');
$this->tooBig = Yii::t('yii', 'The file "{file}" is too big. Its size cannot exceed {limit} bytes.');
}
if ($this->tooSmall === null) {
$this->tooSmall = Yii::t('yii|The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.');
$this->tooSmall = Yii::t('yii', 'The file "{file}" is too small. Its size cannot be smaller than {limit} bytes.');
}
if (!is_array($this->types)) {
$this->types = preg_split('/[\s,]+/', strtolower($this->types), -1, PREG_SPLIT_NO_EMPTY);
......
......@@ -62,14 +62,14 @@ class NumberValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = $this->integerOnly ? Yii::t('yii|{attribute} must be an integer.')
: Yii::t('yii|{attribute} must be a number.');
$this->message = $this->integerOnly ? Yii::t('yii', '{attribute} must be an integer.')
: Yii::t('yii', '{attribute} must be a number.');
}
if ($this->min !== null && $this->tooSmall === null) {
$this->tooSmall = Yii::t('yii|{attribute} must be no less than {min}.');
$this->tooSmall = Yii::t('yii', '{attribute} must be no less than {min}.');
}
if ($this->max !== null && $this->tooBig === null) {
$this->tooBig = Yii::t('yii|{attribute} must be no greater than {max}.');
$this->tooBig = Yii::t('yii', '{attribute} must be no greater than {max}.');
}
}
......@@ -83,7 +83,7 @@ class NumberValidator extends Validator
{
$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;
}
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
......
......@@ -48,7 +48,7 @@ class RangeValidator extends Validator
throw new InvalidConfigException('The "range" property must be set.');
}
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is invalid.');
$this->message = Yii::t('yii', '{attribute} is invalid.');
}
}
......
......@@ -45,7 +45,7 @@ class RegularExpressionValidator extends Validator
throw new InvalidConfigException('The "pattern" property must be set.');
}
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is invalid.');
$this->message = Yii::t('yii', '{attribute} is invalid.');
}
}
......
......@@ -57,8 +57,8 @@ class RequiredValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} cannot be blank.')
: Yii::t('yii|{attribute} must be "{requiredValue}".');
$this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.')
: Yii::t('yii', '{attribute} must be "{requiredValue}".');
}
}
......
......@@ -65,16 +65,16 @@ class StringValidator extends Validator
$this->encoding = Yii::$app->charset;
}
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} must be a string.');
$this->message = Yii::t('yii', '{attribute} must be a string.');
}
if ($this->min !== null && $this->tooShort === null) {
$this->tooShort = Yii::t('yii|{attribute} should contain at least {min} characters.');
$this->tooShort = Yii::t('yii', '{attribute} should contain at least {min} characters.');
}
if ($this->max !== null && $this->tooLong === null) {
$this->tooLong = Yii::t('yii|{attribute} should contain at most {max} characters.');
$this->tooLong = Yii::t('yii', '{attribute} should contain at most {max} characters.');
}
if ($this->is !== null && $this->notEqual === null) {
$this->notEqual = Yii::t('yii|{attribute} should contain {length} characters.');
$this->notEqual = Yii::t('yii', '{attribute} should contain {length} characters.');
}
}
......
......@@ -39,7 +39,7 @@ class UniqueValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} "{value}" has already been taken.');
$this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
}
}
......@@ -55,7 +55,7 @@ class UniqueValidator extends Validator
$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;
}
......
......@@ -46,7 +46,7 @@ class UrlValidator extends Validator
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is not a valid URL.');
$this->message = Yii::t('yii', '{attribute} is not a valid URL.');
}
}
......
......@@ -100,7 +100,7 @@ class AccessControl extends ActionFilter
if ($user->getIsGuest()) {
$user->loginRequired();
} else {
throw new HttpException(403, Yii::t('yii|You are not allowed to perform this action.'));
throw new HttpException(403, Yii::t('yii', 'You are not allowed to perform this action.'));
}
}
}
......@@ -72,7 +72,7 @@ class Request extends \yii\base\Request
$_GET = array_merge($_GET, $params);
return array($route, $_GET);
} else {
throw new HttpException(404, Yii::t('yii|Page not found.'));
throw new HttpException(404, Yii::t('yii', 'Page not found.'));
}
}
......@@ -786,7 +786,7 @@ class Request extends \yii\base\Request
}
if (empty($token) || $cookies->getValue($this->csrfTokenName) !== $token) {
throw new HttpException(400, Yii::t('yii|Unable to verify your data submission.'));
throw new HttpException(400, Yii::t('yii', 'Unable to verify your data submission.'));
}
}
}
......
......@@ -286,7 +286,7 @@ class User extends Component
if ($this->loginUrl !== null) {
Yii::$app->getResponse()->redirect($this->loginUrl);
} else {
throw new HttpException(403, Yii::t('yii|Login Required'));
throw new HttpException(403, Yii::t('yii', 'Login Required'));
}
}
......
......@@ -187,7 +187,7 @@ class ActiveForm extends Widget
}
}
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii|Please fix the following errors:') . '</p>';
$header = isset($options['header']) ? $options['header'] : '<p>' . Yii::t('yii', 'Please fix the following errors:') . '</p>';
$footer = isset($options['footer']) ? $options['footer'] : '';
unset($options['header'], $options['footer']);
......
......@@ -103,7 +103,7 @@ class Breadcrumbs extends Widget
$links = array();
if ($this->homeLink === null) {
$links[] = $this->renderItem(array(
'label' => Yii::t('yii|Home'),
'label' => Yii::t('yii', 'Home'),
'url' => Yii::$app->homeUrl,
), $this->itemTemplate);
} elseif ($this->homeLink !== false) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment