~oh-dev/openhealth/phit-tools

« back to all changes in this revision

Viewing changes to ihris-suite/lib/i2ce/modules/Internationalization/iHRIS_Module_Internationalization.php

  • Committer: litlfred at ibiblio
  • Date: 2009-10-23 12:59:28 UTC
  • Revision ID: litlfred@ibiblio.org-20091023125928-u5lkafz0urm9t8eq
updated ihris-suite to 4.0.1-prerelease -- not debugged

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * © Copyright 2007, 2008 IntraHealth International, Inc.
4
 
 * 
5
 
 * This File is part of iHRIS
6
 
 * 
7
 
 * iHRIS is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 3 of the License, or
10
 
 * (at your option) any later version.
11
 
 * 
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 * 
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 */
20
 
/**
21
 
 ** The module that adds in an image data type
22
 
 * @package I2CE
23
 
 * @access public
24
 
 * @author Carl Leitner <litlfred@ibiblio.org>
25
 
 * @copyright Copyright &copy; 2007, 2008 IntraHealth International, Inc. 
26
 
 * @since Demo-v1.a
27
 
 * @version Demo-v2.a
28
 
 */
29
 
 
30
 
/*
31
 
 * Handles internationalization of the page
32
 
Module Name: Internationalization
33
 
Module Version: 0.1
34
 
Module URI: http://www.capacityproject.org
35
 
Description: Handles internationalization of the page. Not really functional -- just moved the old stuff here.
36
 
Author: Carl Leitner
37
 
Author Email:litlfred@ibiblio.org
38
 
*/
39
 
 
40
 
 
41
 
/** 
42
 
 * Include internationalization (I18N) files 
43
 
 * 
44
 
 * These files include functions for managing internationalization.
45
 
 * 
46
 
 * It's a standard PEAR project and more information can be found
47
 
 * at the {@link http://pear.php.net/package/I18Nv2 I18N site}.
48
 
 * 
49
 
 * @link http://pear.php.net/package/I18Nv2/docs I18Nv2 Docs
50
 
 */
51
 
require_once 'I18Nv2.php';
52
 
/**
53
 
 * Include internationalization (I18N) class for negotiating locale.
54
 
 * 
55
 
 * These files include functions for handling how to determine what locale to
56
 
 * use for translating the content of the site into multiple languages.
57
 
 * 
58
 
 * It's a standard PEAR project and more information can be found
59
 
 * at the {@link http://pear.php.net/package/I18Nv2 I18N site}.
60
 
 * 
61
 
 * @link http://pear.php.net/package/I18Nv2/docs I18Nv2 Docs
62
 
 */
63
 
require_once 'I18Nv2/Negotiator.php';
64
 
 
65
 
/**
66
 
 * I2CE_Template class for managing the import and display of HTML template files.
67
 
 * 
68
 
 * This class is used to load HTML template files and combine them as 
69
 
 * specified in the template files.  It also handles any internationalization
70
 
 * as needed.
71
 
 * 
72
 
 * All interactions with the page object use the PHP DOM classes to find and replace
73
 
 * elements of the page.
74
 
 * 
75
 
 * @package I2CE
76
 
 * @access public
77
 
 * @link http://pear.php.net/package/I18Nv2 I18Nv2 Package Site
78
 
 * @link http://www.php.net/manual/en/ref.dom.php PHP DOM Reference
79
 
 */
80
 
 
81
 
 
82
 
class iHRIS_Module_Internationalization extends I2CE_Module {
83
 
    /**
84
 
     * Holds the file descriptor for saving the display text to a file
85
 
     * to be processed by gettext to make translations for I18N.
86
 
     * @var integer
87
 
     */
88
 
    protected $gtFile;
89
 
 
90
 
 
91
 
    /** 
92
 
     * Recursively cycles through all text nodes to perform internationalization.
93
 
     * 
94
 
     * This method takes a node and processes it with gettext if it's a text node.
95
 
     * If it's a div element with the attribute "lang" set to "en" then it will 
96
 
     * process the whole node as is to translate it.  Otherwise it will recursively
97
 
     * process any child nodes.
98
 
     * 
99
 
     * @param DOMNode $node
100
 
     */
101
 
    protected function processGetText( $node ) {
102
 
        if ( $node->nodeName == "div" && $node->getAttribute("lang") == "en" ) {                
103
 
            $simple_text = $node->textContent;
104
 
            $simple_text = gettext( $simple_text );
105
 
            $this->addText( $simple_text, "div" );          
106
 
        } elseif ( $node->hasChildNodes() ) {
107
 
            foreach( $node->childNodes as $child ) {
108
 
                $this->processGetText( $child );
109
 
            }
110
 
        } elseif ( $node instanceof DOMText ) {
111
 
            if ( trim($node->nodeValue) != "" ) {
112
 
                $node->nodeValue = gettext( $node->nodeValue );
113
 
            }
114
 
        }
115
 
    }
116
 
 
117
 
    public static function getHooks() {
118
 
        return array(
119
 
            'post_template_process_display'=> 'translate'
120
 
            );
121
 
                
122
 
    }
123
 
 
124
 
    /**
125
 
     * Checks to see if gettext is available before allowing the module to be enabled.
126
 
     */
127
 
    public function action_enable() {
128
 
        $enable = function_exists('gettext');
129
 
        if (!$enable) {
130
 
            I2CE::raiseError("Unable to use internationalization.  Please make the gettext extension available http://php.net/gettext", E_USER_WARNING);
131
 
        }
132
 
        return $enable; 
133
 
    }
134
 
 
135
 
 
136
 
    public function translate($args) {
137
 
        //We need to move this out of $i2ce_config to a configuration module and store all this in the DB
138
 
        global $i2ce_config; 
139
 
        $neg = new I18Nv2_Negotiator;
140
 
        $locale = I18Nv2::setLocale( 
141
 
            $neg->getLocaleMatch( $i2ce_config['template']['langs'],
142
 
                                  $i2ce_config['template']['countries'] ), 
143
 
            LC_ALL );
144
 
        bindtextdomain( $i2ce_config['template']['domain'], 
145
 
                        $i2ce_config['template']['locale'] );
146
 
        textdomain( $i2ce_config['template']['domain'] );
147
 
        if ( substr( $this->locale, 0, 2 ) != "en" ) {
148
 
            $top = $args['template']->getDoc()->documentElement;
149
 
            $this->processGetText( $top );
150
 
        }
151
 
    }
152
 
        
153
 
 
154
 
    /** 
155
 
     * Save only the text nodes to a file to be processed by gettext.
156
 
     * 
157
 
     * This will open the given filename and write only the text nodes to it
158
 
     * so that it can be processed by gettext to create the translation files.
159
 
     * 
160
 
     * TODO: The way this works needs to be redone completely.
161
 
     * 
162
 
     * @param string $filename
163
 
     */
164
 
    public function saveGetText($filename) {
165
 
        $this->gtFile = fopen($filename, 'w');
166
 
        if ( !$this->gtFile ) {
167
 
            echo "Can't open $filename\n";
168
 
            die;
169
 
        }
170
 
        fwrite( $this->gtFile, "<?php\n" );
171
 
        $this->writeGetTextNodes( $this->doc->documentElement );
172
 
        fwrite( $this->gtFile, "\n?>\n" );
173
 
        fclose( $this->gtFile );
174
 
    }
175
 
    /**
176
 
     * Recursively cycle through all text nodes for gettext display.
177
 
     * 
178
 
     * This will cycle through all nodes until it finds a text element and write that 
179
 
     * to the {@link gtFile} file descriptor.  The finished file can be parsed by
180
 
     * gettext to create the files to be used by a translator for translating the entire site.
181
 
     * 
182
 
     * @param DOMNode $node
183
 
     */
184
 
    protected function writeGetTextNodes( $node ) {
185
 
        if ( $node->nodeName == "div" && $node->getAttribute("lang") == "en" ) {
186
 
            //$simple_text = simplexml_import_dom( $node )->asXML();
187
 
            $simple_text = $node->textContent;
188
 
            fwrite( $this->gtFile, 'gettext("' . str_replace( '"', '\"', $simple_text ) . '");' . "\n" );
189
 
        } elseif ( $node->hasChildNodes() ) {
190
 
            foreach( $node->childNodes as $child ) {
191
 
                $this->writeGetTextNodes( $child );
192
 
            }
193
 
        } elseif ( $node instanceof DOMText ) {
194
 
            if ( trim($node->nodeValue) != "" ) {
195
 
                fwrite( $this->gtFile, 'gettext("' . $node->nodeValue . '");' . "\n" );
196
 
            }
197
 
        }
198
 
    }
199
 
        
200
 
 
201
 
}
202
 
 
203
 
# Local Variables:
204
 
# mode: php
205
 
# c-default-style: "bsd"
206
 
# indent-tabs-mode: nil
207
 
# c-basic-offset: 4
208
 
# End: