3
* © Copyright 2007, 2008 IntraHealth International, Inc.
5
* This File is part of iHRIS
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.
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.
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/>.
21
** The module that adds in an image data type
24
* @author Carl Leitner <litlfred@ibiblio.org>
25
* @copyright Copyright © 2007, 2008 IntraHealth International, Inc.
31
* Handles internationalization of the page
32
Module Name: Internationalization
34
Module URI: http://www.capacityproject.org
35
Description: Handles internationalization of the page. Not really functional -- just moved the old stuff here.
37
Author Email:litlfred@ibiblio.org
42
* Include internationalization (I18N) files
44
* These files include functions for managing internationalization.
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}.
49
* @link http://pear.php.net/package/I18Nv2/docs I18Nv2 Docs
51
require_once 'I18Nv2.php';
53
* Include internationalization (I18N) class for negotiating locale.
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.
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}.
61
* @link http://pear.php.net/package/I18Nv2/docs I18Nv2 Docs
63
require_once 'I18Nv2/Negotiator.php';
66
* I2CE_Template class for managing the import and display of HTML template files.
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
72
* All interactions with the page object use the PHP DOM classes to find and replace
73
* elements of the page.
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
82
class iHRIS_Module_Internationalization extends I2CE_Module {
84
* Holds the file descriptor for saving the display text to a file
85
* to be processed by gettext to make translations for I18N.
92
* Recursively cycles through all text nodes to perform internationalization.
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.
99
* @param DOMNode $node
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 );
110
} elseif ( $node instanceof DOMText ) {
111
if ( trim($node->nodeValue) != "" ) {
112
$node->nodeValue = gettext( $node->nodeValue );
117
public static function getHooks() {
119
'post_template_process_display'=> 'translate'
125
* Checks to see if gettext is available before allowing the module to be enabled.
127
public function action_enable() {
128
$enable = function_exists('gettext');
130
I2CE::raiseError("Unable to use internationalization. Please make the gettext extension available http://php.net/gettext", E_USER_WARNING);
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
139
$neg = new I18Nv2_Negotiator;
140
$locale = I18Nv2::setLocale(
141
$neg->getLocaleMatch( $i2ce_config['template']['langs'],
142
$i2ce_config['template']['countries'] ),
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 );
155
* Save only the text nodes to a file to be processed by gettext.
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.
160
* TODO: The way this works needs to be redone completely.
162
* @param string $filename
164
public function saveGetText($filename) {
165
$this->gtFile = fopen($filename, 'w');
166
if ( !$this->gtFile ) {
167
echo "Can't open $filename\n";
170
fwrite( $this->gtFile, "<?php\n" );
171
$this->writeGetTextNodes( $this->doc->documentElement );
172
fwrite( $this->gtFile, "\n?>\n" );
173
fclose( $this->gtFile );
176
* Recursively cycle through all text nodes for gettext display.
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.
182
* @param DOMNode $node
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 );
193
} elseif ( $node instanceof DOMText ) {
194
if ( trim($node->nodeValue) != "" ) {
195
fwrite( $this->gtFile, 'gettext("' . $node->nodeValue . '");' . "\n" );
205
# c-default-style: "bsd"
206
# indent-tabs-mode: nil