Translate.php
7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?PHP
/**
* patTemplate function that emulates gettext's behaviour
*
* This can be used to create multi-lingual websites.
* When the template is read, all texts inside the
* Translation tags are extracted and written to a file
* called '$tmplname-default.ini'.
*
* You should copy this file and translate all sentences.
* When the template is used the next time, the sentences
* will be replaced with their respective translations,
* according to the langanuge you set with:
* <code>
* $tmpl->setOption( 'lang', 'de' );
* </code>
*
* $Id: Translate.php,v 1.5.4.1 2004/10/27 11:23:49 schst Exp $
*
* @package patTemplate
* @subpackage Functions
* @author Stephan Schmidt <schst@php.net>
*/
/**
* patTemplate function that emulates gettext's behaviour
*
* This can be used to create multi-lingual websites.
* When the template is read, all texts inside the
* Translation tags are extracted and written to a file
* called '$tmplname-default.ini'.
*
* You should copy this file and translate all sentences.
* When the template is used the next time, the sentences
* will be replaced with their respective translations,
* according to the langanuge you set with:
* <code>
* $tmpl->setOption( 'lang', 'de' );
* </code>
*
* $Id: Translate.php,v 1.5.4.1 2004/10/27 11:23:49 schst Exp $
*
* @package patTemplate
* @subpackage Functions
* @author Stephan Schmidt <schst@php.net>
* @todo add error management
*/
class patTemplate_Function_Translate extends patTemplate_Function
{
/**
* name of the function
* @access private
* @var string
*/
var $_name = 'Translate';
/**
* configuration of all files
*
* @access private
* @var array
*/
var $_config = array();
/**
* global config
*
* @access private
* @var array
*/
var $_globalconfig = array();
/**
* list of all sentences
*
* @access private
* @var array
*/
var $_sentences = array();
/**
* translations of the language files
*
* @access private
* @var array
*/
var $_translation = array();
/**
* reference to the patTemplate object that instantiated the module
*
* @access protected
* @var object
*/
var $_tmpl;
/**
* set a reference to the patTemplate object that instantiated the reader
*
* @access public
* @param object patTemplate object
*/
function setTemplateReference( &$tmpl )
{
$this->_tmpl = &$tmpl;
}
/**
* call the function
*
* @access public
* @param array parameters of the function (= attributes of the tag)
* @param string content of the tag
* @return string content to insert into the template
*/
function call( $params, $content )
{
/**
* nothing to translate
*/
if( empty( $content ) )
{
return;
}
if( empty( $this->_globalconfig ) )
$this->_retrieveGlobalConfig();
$input = $this->_reader->getCurrentInput();
/**
* get config
*/
if( empty( $this->_config[$input] ) )
{
$this->_retrieveConfig( $input );
$this->_loadTranslationFile( $input );
}
/**
* unique key for the sentence to translate
*/
if( isset( $params['key'] ) )
$key = $params['key'];
else
$key = md5( $content );
/**
* does this already exists?
*/
if( !isset( $this->_sentences[$input][$key] ) )
{
$this->_sentences[$input][$key] = $content;
$this->_addToTranslationFile( $input, $key, $content );
}
/**
* has it been translated?
*/
if( isset( $this->_translation[$input][$key] ) )
{
return $this->_translation[$input][$key];
}
/**
* use original sentence
*/
return $this->_sentences[$input][$key];
}
/**
* get the global configuration
*
* This method fetches the selected language and the translation folder
* as it is shared by all templates.
*
* @access private
* @return boolean currently always returns true
*/
function _retrieveGlobalConfig()
{
/**
* get config values from patTemplate
*/
$this->_globalconfig['lang'] = $this->_tmpl->getOption( 'lang' );
if( !is_array( $this->_globalconfig['lang'] ) )
{
if( $this->_globalconfig['lang'] == 'auto' )
$this->_globalconfig['lang'] = $this->_guessLanguage();
else
$this->_globalconfig['lang'] = array( $this->_globalconfig['lang'] );
}
$this->_globalconfig['translationFolder'] = $this->_tmpl->getOption( 'translationFolder' );
return true;
}
/**
* retrieve configuration
*
* This method sets the used files and loads the original sentence file, if it exists.
*
* @access private
* @param string current input that is used by patTemplate
* @return boolean true on success
*/
function _retrieveConfig( $input )
{
if( !is_array( $this->_config ) )
$this->_config = array();
$this->_config[$input] = array();
$this->_sentences[$input] = array();
$this->_config[$input]['sentenceFile'] = $this->_tmpl->getOption( 'translationFolder' ) . '/'.$input.'-default.ini';
$this->_config[$input]['langFile'] = $this->_tmpl->getOption( 'translationFolder' ) . '/'.$input.'-%s.ini';
/**
* get the 'gettext' source file
*/
$this->_sentences[$input] = @parse_ini_file( $this->_config[$input]['sentenceFile'] );
if( !is_array( $this->_sentences[$input] ) )
$this->_sentences[$input] = array();
else
$this->_sentences[$input] = array_map( array( $this, '_unescape' ), $this->_sentences[$input] );
return true;
}
/**
* load the translation file
*
* @access private
* @param string current input that is used by patTemplate
* @return boolean true on success
*/
function _loadTranslationFile( $input )
{
foreach( $this->_globalconfig['lang'] as $lang )
{
$translationFile = sprintf( $this->_config[$input]['langFile'], $lang );
if( !file_exists( $translationFile ) )
continue;
$tmp = @parse_ini_file( $translationFile );
if( is_array( $tmp ) )
{
$tmp = array_map( array( $this, '_unescape' ), $tmp );
$this->_translation[$input] = $tmp;
return true;
}
}
return false;
}
/**
* unsecape the text that has been read from the translation file
*
* @access private
* @param string
* @return string
*/
function _unescape( $text )
{
return str_replace( '"', '"', $text );
}
/**
* add a new sentence to the translation file
*
* @access private
* @param string unique key
* @param string sentence to translate
* @return boolean
*/
function _addToTranslationFile( $input, $key, $content )
{
$fp = @fopen( $this->_config[$input]['sentenceFile'], 'a' );
if( !$fp )
return false;
flock( $fp, LOCK_EX );
fputs( $fp, sprintf( '%s = "%s"'."\n", $key, str_replace( '"', '"', $content ) ) );
flock( $fp, LOCK_UN );
fclose( $fp );
return true;
}
/**
* guess the language
*
* @access private
* @return array array containing all accepted languages
*/
function _guessLanguage()
{
if( !preg_match_all( '/([a-z\-]*)?[,;]/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches) )
{
return array();
}
$langs = array();
foreach( $matches[1] as $lang )
{
if( empty( $lang ) )
continue;
array_push( $langs, $lang );
}
return $langs;
}
}
?>