dftk
[ class tree: dftk ] [ index: dftk ] [ all elements ]

Source for file dftk_da_language_manager.php

Documentation is available at dftk_da_language_manager.php


1 <?php
2 /*
3
4 Copyright (c)2003 DuckCorp(tm) and RtpNet(tm)
5
6
7
8 This file is part of DFTK.
9
10 DFTK is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 DFTK is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with DFTK if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25
26 /**
27 * @package dftk
28 * @author Duck <duck@DuckCorp.org>
29 * @author Rtp <rtp@rtp-net.org>
30 * @copyright Copyright (c)2003 DuckCorp(tm) and RtpNet(tm)
31 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
32 * @version 0.4.0
33 */
34
35
36 /**
37 * DFTK Language Manager Class
38 *
39 * @package dftk
40 * @subpackage dftk-language
41 * @author Duck <duck@DuckCorp.org>
42 *
43 * @access public
44 */
45 class DftkDaLanguageManager
46 {
47 /**
48 * List of registered directories for translations
49 *
50 * @access private
51 * @var array
52 */
53 var $_dirlist;
54
55 /**
56 * List of translations for the selected language
57 *
58 * @access private
59 * @var array
60 */
61 var $_trans;
62
63 /**
64 * Language to use currently
65 *
66 * @access private
67 * @var string
68 */
69 var $_lang;
70
71 /**
72 * Encoding to use currently
73 *
74 * @access private
75 * @var string
76 */
77 var $_encoding;
78
79 /**
80 * Constructor
81 *
82 * @access public
83 */
84 function DftkDaLanguageManager()
85 {
86 $this->_dirlist = array();
87 $this->_trans = array();
88 $this->_lang = "en";
89 $this->_encoding = "UTF-8";
90 }
91
92 /**
93 * Select Language to use
94 *
95 * @access public
96 * @param string $lang Language name
97 */
98 function set_lang($lang)
99 {
100 $this->_lang = strtolower($lang);
101 $this->reload();
102 }
103
104 /**
105 * Select Encoding to use
106 *
107 * @access public
108 * @param string $encoding Encofing name
109 */
110 function set_encoding($encoding)
111 {
112 $this->_encoding = strtoupper($encoding);
113 }
114
115 /**
116 * Reload translation files
117 *
118 * @access public
119 */
120 function reload()
121 {
122 $this->_trans = array();
123 foreach ($this->_dirlist as $dir)
124 {
125 $this->_load($dir);
126 }
127 }
128
129 /**
130 * Register a directory where to find translation files
131 *
132 * @access public
133 * @param string $dir Directory path
134 */
135 function add_trans($dir)
136 {
137 if (file_exists($dir))
138 {
139 $this->_dirlist[] = $dir;
140 $this->_load($dir);
141 }
142 }
143
144 /**
145 * Load tranlation files in the specified directory
146 *
147 * @access private
148 * @param string $dir Directory path
149 */
150 function _load($dir)
151 {
152 if (file_exists($dir."/".$this->_lang))
153 {
154 $handle = opendir($dir."/".$this->_lang);
155 while ($file = readdir($handle))
156 {
157 if ($file=='.' || $file=='..')
158 continue;
159 if (is_dir($file))
160 continue;
161 $z = parse_ini_file($dir."/".$this->_lang."/".$file);
162 $this->_trans = array_merge($this->_trans, $z);
163 }
164 closedir($handle);
165 }
166 }
167
168 /**
169 * Ask for a translated message
170 *
171 * @access public
172 * @param string $key Id of the message
173 * @param array $params Parameters for the message
174 * @return string $msg
175 */
176 function get_msg($key, $params=array())
177 {
178 $msg = $this->_trans[$key];
179
180 if (!$msg)
181 return "[No Translation Defined for message '".$key."']";
182
183 if (count($params) != 0)
184 for ($i=0; $i<count($params); $i++)
185 {
186 $msg = preg_replace("/<#(\d)>/", "\$params[\\1]", $msg);
187 eval("\$msg = \"$msg\";");
188 }
189
190 if ($this->_encoding == "UTF-8")
191 return $msg;
192
193 $enc_msg = iconv("UTF-8", $this->_encoding."//TRANSLIT", $msg);
194
195 if ($enc_msg === false)
196 return "[Bad encoding selected, falling back to 'UTF-8'] ".$msg;
197 else
198 return $enc_msg;
199 }
200
201 /**
202 * Get the list of existing message Ids
203 *
204 * @access public
205 * @return array $tab Array of existing message Ids
206 */
207 function get_msg_list()
208 {
209 $tab = array();
210 foreach ($this->_trans as $key => $val)
211 $tab[] = $key;
212
213 return $tab;
214 }
215 }
216
217 ?>

Documentation generated on Sat, 6 Dec 2003 13:47:33 +0100 by phpDocumentor 1.2.3