Add translation support to horde/Token
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 30 Nov 2010 23:31:45 +0000 (16:31 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 30 Nov 2010 23:32:11 +0000 (16:32 -0700)
framework/Token/lib/Horde/Token/Base.php
framework/Token/lib/Horde/Token/Translation.php [new file with mode: 0644]
framework/Token/package.xml

index aa2aa84..a636a37 100644 (file)
@@ -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 (file)
index 0000000..98f701c
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Horde
+ * @package  Translation
+ */
+
+/**
+ * Horde_Token_Translation is the translation wrapper class for horde/Token.
+ *
+ * @author  Jan Schneider <jan@horde.org>
+ * @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);
+    }
+
+}
index 364ce55..2ade6ea 100644 (file)
@@ -23,7 +23,7 @@
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>
+ <notes>* Add translation support.
  * Add support for timestamped tokens.
  * Add Horde_Token_Exception::.
  * Move driver code into sub-classes.
@@ -45,6 +45,9 @@
       <file name="File.php" role="php" />
       <file name="Null.php" role="php" />
       <file name="Sql.php" role="php" />
+      <file name="Translation.php" role="php">
+       <tasks:replace from="@data_dir@" to="data_dir" type="pear-config" />
+      </file>
      </dir> <!-- /lib/Horde/Token -->
      <file name="Token.php" role="php" />
     </dir> <!-- /lib/Horde -->
     <channel>pear.horde.org</channel>
    </package>
    <package>
+    <name>Translation</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
     <name>Url</name>
     <channel>pear.horde.org</channel>
    </package>
    <install as="Horde/Token/File.php" name="lib/Horde/Token/File.php" />
    <install as="Horde/Token/Null.php" name="lib/Horde/Token/Null.php" />
    <install as="Horde/Token/Sql.php" name="lib/Horde/Token/Sql.php" />
+   <install as="Horde/Token/Translation.php" name="lib/Horde/Token/Translation.php" />
    <install as="Horde/Token/Exception/Expired.php" name="lib/Horde/Token/Exception/Expired.php" />
    <install as="Horde/Token/Exception/Invalid.php" name="lib/Horde/Token/Exception/Invalid.php" />
    <install as="Horde/Token/Exception/Used.php" name="lib/Horde/Token/Exception/Used.php" />