root/trunk/patForms/AttributeFilter/Gettext.php

Revision 349, 3.0 kB (checked in by gerd, 3 years ago)

Draft of new feature: attribute filter

  • Property svn:keywords set to "Id"
Line 
1 <?php
2 /**
3  * patForms test attribute filter
4  *
5  * $Id$
6  *
7  * @package     patForms
8  * @subpackage  AttributeFilter
9  */
10
11 /**
12  * patForms test attribute filter
13  *
14  *
15  * @package     patForms
16  * @subpackage  AttributeFilter
17  * @author      gERD Schaufelberger <gerd@php-tools.net>
18  * @license     LGPL, see license.txt for details
19  * @link        http://www.php-tools.net
20  * @version     1.0
21  */
22 class patForms_AttributeFilter_Gettext extends patForms_AttributeFilter
23 {
24    /**
25     * use gettext marker
26     *
27     * defines whether to use a check for a "gettext:" token
28     * @access   private
29     */
30     var $_useMarker =   true;
31
32    /**
33     * attribute list
34     *
35     * list of attributes to handle
36     * @access   private
37     */
38     var $_attributes = array( 'label', 'title', 'description' );
39
40    /**
41     * define the attibute names to be translated with gettext
42     *
43     * By default the attributes 'label', 'title' and 'description'
44     * will be translated. As those are the only attributes that contain
45     * messages visible by human readers, this should be suffices.
46     * Still, this list by be altered.
47     *
48     * @access   public
49     * @param array list of attributes to handle
50     * @return boolean true on success
51     */
52     function setAttributes($attributes)
53     {
54         $this->_attributes   =   $attributes;
55         return true;
56     }
57
58    /**
59     * Filter attribute value on "set"-event
60     *
61     * @access   public
62     * @param    string  element's type
63     * @param    string  attribute's names
64     * @param    mixed   attribute's value
65     * @return   mixed   true on success, patError-object on error
66     */
67     function onSet($type, $attribute, &$value)
68     {
69         return $this->_translate($type, $attribute, $value);
70     }
71
72    /**
73     * Filter attribute value on "apply"-event
74     *
75     * @access   public
76     * @param    string  element's type
77     * @param    string  attribute's names
78     * @param    mixed   attribute's value
79     * @return   mixed   true on success, patError-object on error
80     */
81     function onApply($type, $attribute, &$value)
82     {
83         return $this->_translate($type, $attribute, $value);
84     }
85
86    /**
87     * Translate string
88     *
89     * @access   private
90     * @param    string  element's type
91     * @param    string  attribute's names
92     * @param    mixed   attribute's value
93     * @return   mixed   true on success, patError-object on error
94     */
95     function _translate($type, $attribute, &$value)
96     {
97         // must be a known attribute
98         if (!in_array( $attribute, $this->_attributes)) {
99             return true;
100         }
101
102         // only string are translateable
103         if (!is_string($value)) {
104             return true;
105         }
106
107         // translate blind
108         if (!$this->_useMarker) {
109             $value  =   gettext($value);
110             return true;
111         }
112
113         // translate only if there is a marker
114         if (preg_match('/^gettext:\'(.*)\'/', $value, $match)) {
115             $value  =    gettext($match[1]);
116             return true;
117         }
118     }
119 }
120 ?>
Note: See TracBrowser for help on using the browser.