From a5ae9dc1906e5fa08d56dc9585fd8ccbcffb5c90 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 30 Nov 2010 16:31:45 -0700 Subject: [PATCH] Add translation support to horde/Token --- framework/Token/lib/Horde/Token/Base.php | 7 ++-- framework/Token/lib/Horde/Token/Translation.php | 52 +++++++++++++++++++++++++ framework/Token/package.xml | 10 ++++- 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 framework/Token/lib/Horde/Token/Translation.php diff --git a/framework/Token/lib/Horde/Token/Base.php b/framework/Token/lib/Horde/Token/Base.php index aa2aa84f8..a636a37dd 100644 --- a/framework/Token/lib/Horde/Token/Base.php +++ b/framework/Token/lib/Horde/Token/Base.php @@ -49,6 +49,7 @@ abstract class Horde_Token_Base if (!isset($params['secret'])) { throw new Horde_Token_Exception('Missing secret parameter.'); } + if (!isset($params['token_lifetime'])) { $params['token_lifetime'] = -1; } @@ -168,10 +169,10 @@ nce? { list($nonce, $hash) = $this->_decode($token); if ($hash != $this->_hash($nonce . $seed)) { - throw new Horde_Token_Exception_Invalid('We cannot verify that this request was really sent by you. It could be a malicious request. If you intended to perform this action, you can retry it now.'); + throw new Horde_Token_Exception_Invalid(Horde_Token_Translation::t('We cannot verify that this request was really sent by you. It could be a malicious request. If you intended to perform this action, you can retry it now.')); } if ($this->_isExpired($nonce, $this->_params['token_lifetime'])) { - throw new Horde_Token_Exception_Expired(sprintf("This request cannot be completed because the link you followed or the form you submitted was only valid for %s minutes. Please try again now.", floor($this->_params['token_lifetime'] / 60))); + throw new Horde_Token_Exception_Expired(Horde_Token_Translation::t(sprintf("This request cannot be completed because the link you followed or the form you submitted was only valid for %s minutes. Please try again now.", floor($this->_params['token_lifetime'] / 60)))); } return array($nonce, $hash); } @@ -192,7 +193,7 @@ nce? { list($nonce, $hash) = $this->isValid($token, $seed); if (!$this->verify($nonce)) { - throw new Horde_Token_Exception_Used('This token has been used before!'); + throw new Horde_Token_Exception_Used(Horde_Token_Translation::t('This token has been used before!')); } } diff --git a/framework/Token/lib/Horde/Token/Translation.php b/framework/Token/lib/Horde/Token/Translation.php new file mode 100644 index 000000000..98f701c37 --- /dev/null +++ b/framework/Token/lib/Horde/Token/Translation.php @@ -0,0 +1,52 @@ + + * @package Token + */ +class Horde_Token_Translation extends Horde_Translation +{ + /** + * Returns the translation of a message. + * + * @var string $message The string to translate. + * + * @return string The string translation, or the original string if no + * translation exists. + */ + static public function t($message) + { + self::$_domain = 'Horde_Token'; + self::$_directory = '@data_dir@' == '@'.'data_dir'.'@' ? dirname(__FILE__) . '/../../../locale' : '@data_dir@/Token/locale'; + return parent::t($message); + } + + /** + * Returns the plural translation of a message. + * + * @param string $singular The singular version to translate. + * @param string $plural The plural version to translate. + * @param integer $number The number that determines singular vs. plural. + * + * @return string The string translation, or the original string if no + * translation exists. + */ + static public function ngettext($singular, $plural, $number) + { + self::$_domain = 'Horde_Token'; + self::$_directory = '@data_dir@' == '@'.'data_dir'.'@' ? dirname(__FILE__) . '/../../../locale' : '@data_dir@/Token/locale'; + return parent::ngettext($singular, $plural, $number); + } + +} diff --git a/framework/Token/package.xml b/framework/Token/package.xml index 364ce5575..2ade6ead5 100644 --- a/framework/Token/package.xml +++ b/framework/Token/package.xml @@ -23,7 +23,7 @@ beta LGPL - + * Add translation support. * Add support for timestamped tokens. * Add Horde_Token_Exception::. * Move driver code into sub-classes. @@ -45,6 +45,9 @@ + + + @@ -76,6 +79,10 @@ pear.horde.org + Translation + pear.horde.org + + Url pear.horde.org @@ -103,6 +110,7 @@ + -- 2.11.0