refactored exceptions.

I18N WIP.
This commit is contained in:
Qiang Xue
2013-02-04 16:39:19 -05:00
parent 398ac25456
commit 54ee8c44a0
42 changed files with 561 additions and 212 deletions

View File

@@ -30,10 +30,6 @@ use yii\util\FileHelper;
* persistence method. This application component is dynamically loaded when needed.</li>
* <li>{@link getCache cache}: provides caching feature. This application component is
* disabled by default.</li>
* <li>{@link getMessages messages}: provides the message source for translating
* application messages. This application component is dynamically loaded when needed.</li>
* <li>{@link getCoreMessages coreMessages}: provides the message source for translating
* Yii framework messages. This application component is dynamically loaded when needed.</li>
* </ul>
*
* Application will undergo the following life cycles when processing a user request:
@@ -57,23 +53,28 @@ class Application extends Module
const EVENT_BEFORE_REQUEST = 'beforeRequest';
const EVENT_AFTER_REQUEST = 'afterRequest';
/**
* @var string the application name. Defaults to 'My Application'.
* @var string the application name.
*/
public $name = 'My Application';
/**
* @var string the version of this application. Defaults to '1.0'.
* @var string the version of this application.
*/
public $version = '1.0';
/**
* @var string the charset currently used for the application. Defaults to 'UTF-8'.
* @var string the charset currently used for the application.
*/
public $charset = 'UTF-8';
/**
* @var string the language that is meant to be used for end users.
* @see sourceLanguage
*/
public $language = 'en_US';
/**
* @var string the language that the application is written in. This mainly refers to
* the language that the messages and view files are in. Defaults to 'en_us' (US English).
* the language that the messages and view files are written in.
* @see language
*/
public $sourceLanguage = 'en_us';
public $sourceLanguage = 'en_US';
/**
* @var array IDs of the components that need to be loaded when the application starts.
*/
@@ -212,29 +213,6 @@ class Application extends Module
}
}
/**
* Returns the language that the end user is using.
* @return string the language that the user is using (e.g. 'en_US', 'zh_CN').
* Defaults to the value of [[sourceLanguage]].
*/
public function getLanguage()
{
return $this->_language === null ? $this->sourceLanguage : $this->_language;
}
/**
* Specifies which language the end user is using.
* This is the language that the application should use to display to end users.
* By default, [[language]] and [[sourceLanguage]] are the same.
* Do not set this property unless your application needs to support multiple languages.
* @param string $language the user language (e.g. 'en_US', 'zh_CN').
* If it is null, the [[sourceLanguage]] will be used.
*/
public function setLanguage($language)
{
$this->_language = $language;
}
/**
* Returns the time zone used by this application.
* This is a simple wrapper of PHP function date_default_timezone_get().
@@ -295,23 +273,6 @@ class Application extends Module
// return $this->getLocale()->getDateFormatter();
// }
//
// /**
// * Returns the core message translations component.
// * @return \yii\i18n\MessageSource the core message translations
// */
// public function getCoreMessages()
// {
// return $this->getComponent('coreMessages');
// }
//
// /**
// * Returns the application message translations component.
// * @return \yii\i18n\MessageSource the application message translations
// */
// public function getMessages()
// {
// return $this->getComponent('messages');
// }
/**
* Returns the database connection component.
@@ -367,6 +328,15 @@ class Application extends Module
return $this->getComponent('viewRenderer');
}
/**
* Returns the internationalization (i18n) component
* @return \yii\i18n\I18N the internationalization component
*/
public function getI18N()
{
return $this->getComponent('i18n');
}
/**
* Sets default path aliases.
*/
@@ -387,19 +357,14 @@ class Application extends Module
'errorHandler' => array(
'class' => 'yii\base\ErrorHandler',
),
'coreMessages' => array(
'class' => 'yii\i18n\PhpMessageSource',
'language' => 'en_us',
'basePath' => '@yii/messages',
),
'messages' => array(
'class' => 'yii\i18n\PhpMessageSource',
'i18n' => array(
'class' => 'yii\i18n\I18N',
),
'securityManager' => array(
'class' => 'yii\base\SecurityManager',
),
'urlManager' => array(
'class' => 'yii\web\UrlManager',
'translator' => array(
'class' => 'yii\i18n\Translator',
),
));
}

View File

@@ -177,7 +177,7 @@ class Controller extends Component
}
if ($missing !== array()) {
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),
)));
}

View File

@@ -22,7 +22,7 @@ class Exception extends \Exception
*/
public function getName()
{
return \Yii::t('yii', 'Exception');
return \Yii::t('yii:Exception');
}
}

View File

@@ -104,6 +104,6 @@ class HttpException extends UserException
if(isset($httpCodes[$this->statusCode]))
return $httpCodes[$this->statusCode];
else
return \Yii::t('yii', 'Error');
return \Yii::t('yii:Error');
}
}

View File

@@ -15,14 +15,14 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class InvalidCallException extends \Exception
class InvalidCallException extends Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Invalid Call');
return \Yii::t('yii:Invalid Call');
}
}

View File

@@ -15,14 +15,14 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class InvalidConfigException extends \Exception
class InvalidConfigException extends Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Invalid Configuration');
return \Yii::t('yii:Invalid Configuration');
}
}

View File

@@ -22,7 +22,7 @@ class InvalidRequestException extends UserException
*/
public function getName()
{
return \Yii::t('yii', 'Invalid Request');
return \Yii::t('yii:Invalid Request');
}
}

View File

@@ -22,7 +22,7 @@ class InvalidRouteException extends UserException
*/
public function getName()
{
return \Yii::t('yii', 'Invalid Route');
return \Yii::t('yii:Invalid Route');
}
}

View File

@@ -15,14 +15,14 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class NotSupportedException extends \Exception
class NotSupportedException extends Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Not Supported');
return \Yii::t('yii:Not Supported');
}
}

View File

@@ -79,7 +79,7 @@ class SecurityManager extends Component
if (!empty($value)) {
$this->_validationKey = $value;
} else {
throw new CException(Yii::t('yii', 'SecurityManager.validationKey cannot be empty.'));
throw new CException(Yii::t('yii:SecurityManager.validationKey cannot be empty.'));
}
}
@@ -112,7 +112,7 @@ class SecurityManager extends Component
if (!empty($value)) {
$this->_encryptionKey = $value;
} else {
throw new CException(Yii::t('yii', 'SecurityManager.encryptionKey cannot be empty.'));
throw new CException(Yii::t('yii:SecurityManager.encryptionKey cannot be empty.'));
}
}
@@ -191,12 +191,12 @@ class SecurityManager extends Component
}
if ($module === false) {
throw new CException(Yii::t('yii', 'Failed to initialize the mcrypt module.'));
throw new CException(Yii::t('yii:Failed to initialize the mcrypt module.'));
}
return $module;
} else {
throw new CException(Yii::t('yii', 'SecurityManager requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
throw new CException(Yii::t('yii:SecurityManager requires PHP mcrypt extension to be loaded in order to use data encryption feature.'));
}
}

View File

@@ -15,14 +15,14 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class UnknownMethodException extends \Exception
class UnknownMethodException extends Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Unknown Method');
return \Yii::t('yii:Unknown Method');
}
}

View File

@@ -15,14 +15,14 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class UnknownPropertyException extends \Exception
class UnknownPropertyException extends Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Unknown Property');
return \Yii::t('yii:Unknown Property');
}
}

View File

@@ -366,7 +366,7 @@ class UrlManager extends Component
return isset($_GET[$this->routeVar]) ? $_GET[$this->routeVar] : $r;
}
if($this->useStrictParsing)
throw new HttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
throw new HttpException(404,Yii::t('yii:Unable to resolve the request "{route}".',
array('{route}'=>$pathInfo)));
else
return $pathInfo;
@@ -502,7 +502,7 @@ class UrlManager extends Component
if($value===self::PATH_FORMAT || $value===self::GET_FORMAT)
$this->_urlFormat=$value;
else
throw new CException(Yii::t('yii','CUrlManager.UrlFormat must be either "path" or "get".'));
throw new CException(Yii::t('yii:CUrlManager.UrlFormat must be either "path" or "get".'));
}
}
@@ -685,7 +685,7 @@ class CUrlRule extends CBaseUrlRule
$this->routePattern='/^'.strtr($this->route,$tr2).'$/u';
if(YII_DEBUG && @preg_match($this->pattern,'test')===false)
throw new CException(Yii::t('yii','The URL pattern "{pattern}" for route "{route}" is not a valid regular expression.',
throw new CException(Yii::t('yii:The URL pattern "{pattern}" for route "{route}" is not a valid regular expression.',
array('{route}'=>$route,'{pattern}'=>$pattern)));
}