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
|
<?php
/*
** Copyright (C) 1995-2008 by the ITools Authors.
** This file is part of ITools - the Internet Tools Library
**
** ITools is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** ITools is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class it_text
{
var $actlanguage; # Selected language
var $defaultlanguage; # Browser language
var $languages = array(); # Active languages
var $languages_available = array(); # Available languages
var $statictext = array(); # Text array, read from php file on init
var $label_to_service = array(); # which label belongs to which service - only used for debug parameter texts
/**
* Constructor
* Loads all texts.php in include path for translated labels. Singleton; if instanciated mutiple times, texts are merged
* Example texts.php: <?php return array('_' => array('en'=>"English", 'de'=>"Deutsch"), 'edit'=>array('en'=>"Edit", 'de'=>("Editieren")));
* @param $p['fallbacklanguage'] optional language to use for undefined texts (useful for partially translated projects)
* @param $p['forcelanguage'] optional language to use instead of user's preferred language
* @param $p['global'] store text object in global it_text for global functions (default: true)
* @param $p['phpfile'] optional texts file(s), default: all texts.php in include path
* @param $p['phpfiles'] text files to load in addition to $p['phpfile'] (Note: $p['phpfile'] defaults to all texts.php files in include path)
*/
function it_text($p = array())
{
if (!$p['phpfile'])
{
# Find all texts.php in path (abs path in case we need to save)
foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $dir)
if (file_exists($phpfile = "$dir/texts.php"))
$p['phpfiles'][] = $phpfile;
}
$this->p = ($p += array(
'global' => true,
'phpfiles' => array_unique(array_merge((array)$p['phpfiles'], (array)$p['phpfile'])),
));
# Read and merge texts from php files if none defined yet
foreach ($p['phpfiles'] as $phpfile)
{
$oldtext = $this->statictext;
if (is_array($ret = include($phpfile)))
{
$this->statictext += $ret;
if ($GLOBALS['debug_texts'])
{
$service = strpos($phpfile, $GLOBALS['ULTRAHOME']) !== false ? '' : it::match('/www/([^/.]+)', $phpfile);
$this->label_to_service += array_combine(array_keys($ret), array_fill(0, count($ret), $service));
}
}
else
$this->statictext = $oldtext + $this->statictext; # FIXME: compatibility mode
}
# Get array of supported languages and their names
$this->languages_available = (array)$this->statictext['_'];
foreach ($this->languages_available as $code => $languagename)
{
# Only use a language in browser detection below if it's not disabled by a leading '-'
if (substr($languagename, 0, 1) != '-')
{
$this->languages[$code] = $languagename;
if (!$this->actlanguage)
$this->initlang($code, "setting failsafe language");
}
}
# Set our default language according to browser preference
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $code)
if ($this->initlang($code, "setting language from browser") || $this->initlang(substr($code, 0, 2), "setting language family from browser"))
break;
}
$this->defaultlanguage = $this->actlanguage;
$this->initlang(it::match('\.([a-z]{2})\.[^./]+$', $_SERVER['PHP_SELF']), "setting language from url override");
$this->initlang($p['forcelanguage'], "setting language from programmer override");
# Create empty array to activate sampling; dont kill any existing one
if (!it::is_live() || rand(1, $_POST ? 10 : 100) == 1)
$GLOBALS['it_text_sampling'] = (array)$GLOBALS['it_text_sampling'];
# Make this object available under $GLOBALS['it_text'], or add my texts to $GLOBALS['it_text'] if it exists
if ($p['global'])
{
if (!$GLOBALS['it_text'])
$GLOBALS['it_text'] =& $this;
else
$GLOBALS['it_text']->statictext += $this->statictext;
}
}
# internal: overwrite language setting if code is valid, return success
function initlang($code)
{
if ($this->languages[$code])
$this->actlanguage = $code;
return $this->languages[$code];
}
/**
* Instanciate singleton if necessary
*/
static function init()
{
if (!$GLOBALS['it_text'])
new it_text;
}
/**
* INTERNAL function for T(): : Return translated text in the selected language
*/
function text($label, $language = null)
{
if (!$language)
$language = $this->actlanguage;
$text = $this->statictext[$label][$language];
if (!isset($text))
{
$text = $this->statictext[$label][$this->p['fallbacklanguage']];
if (!isset($text))
{
$text = "<span style='background:#F88' title='" . Q("$label (" . it_debug::backtrace(array('levels'=>1, 'skipfiles'=>"text|auto_prepend")) . ")") . "'>" . Q($label) . "</span><a href='/admin/text?edit=$label'>.</a>";
it::error(array('title'=>"unknown label $label language $language - see /tmp/alertdata/alert.log", 'backtraceskip'=>2, 'blockmail'=>21600));
}
}
if ($GLOBALS['debug_texts'] && !preg_match('/submit|button|servicedomain/i', $label) && (!$_GET['it_texts_mark'] || $label == $_GET['it_texts_mark']))
{
$host = 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . preg_replace('/\.texts/', '', $_SERVER['HTTP_HOST']);
if ($service = $this->label_to_service[$label])
$host = preg_replace('#//(admin\.)?[^.]*\.#', "//$service.", $host);
$text = "<span style='background:#8F8' title='$label (" . it_debug::backtrace(array('levels'=>1, 'skipfiles'=>"text|auto_prepend")) . ")'>" . ($text ? $text : $label) . "</span><a href='$host/admin/text?edit=$label'>.</a>";
}
if (isset($GLOBALS['it_text_sampling']))
$GLOBALS['it_text_sampling'][$label] = true;
return $text;
}
/**
* INTERNAL function for ET(): Return translated text with values replaced
*/
function etext($label, $values = null, $language = null)
{
return $this->transmogrify($this->text($label, $language), $values, $label);
}
/**
* INTERNAL function for T_set_language()
*/
function set_language($language)
{
$this->actlanguage = $this->languages_available[$language] ? $language : $this->defaultlanguage;
}
/**
* INTERNAL function for T_lang(): Get active language
*/
function get_language()
{
return $this->actlanguage;
}
/**
* INTERNAL function for T_lang(): Get active language
*/
function get_defaultlanguage()
{
return $this->defaultlanguage;
}
/**
* INTERNAL function for T_exists(): Check if a text entry for a specific label exists
*/
function text_exists($label, $language = null)
{
return isset($this->statictext[$label][isset($language) ? $language : $this->actlanguage]) || $this->p['fallbacklanguage'] && isset($this->statictext[$label][$this->p['fallbacklanguage']]);
}
/**
* Create / overwrite a text in the selected language. Call dump_php() to make the change permanent.
* @param $label Label of text to change
* @param $text New text to set
* @param $language Optional language that is to be manipulated
*/
function set($label, $text = null, $language = null)
{
if (!isset($language))
$language = $this->actlanguage;
$this->statictext[$label][$language] = $text;
}
/**
* Replaces variables of the form {obj.var} with value, e.g. {user.name}
* NOTE: Invalid object names or non-existing variables are simply deleted.
*/
function transmogrify($text, $values = null, $label = null)
{
foreach (preg_split('/{([\w.]+)}/', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $i => $part)
{
if ($i % 2) # odd offsets are delimiters, i.e. braces to be replaced
{
$value = $values ? $values : $GLOBALS;
foreach (explode(".", $part) as $key)
{
$value = is_object($value) ? $value->$key : $value[$key];
if ($value === null && $values && $label) # do not test in $GLOBALS mode
it::error(array('title' => "No value given for text variable {" . $key ."} in label $label", 'backtraceskip' => 3));
}
$result .= $GLOBALS['debug_texts'] ? "</span>$value<span style='background:#8F8'>" : $value;
}
else
$result .= $part;
}
return $result;
}
/**
* Re-create php text file from $this->statictext
* @return true if successful, false if not (usually if file is not writeable by user www)
*/
function dump_php()
{
$result = false;
# Special sorting: natural, but _ is the first entry
uksort($this->statictext, "strnatcmp");
$this->statictext = array_merge(array('_' => $this->statictext['_']), $this->statictext);
$oldmask = umask(002);
if ((count($this->p['phpfiles']) == 1) && ($f = fopen($this->p['phpfiles'][0], 'w')))
{
$result = (fputs($f, '<?php return ' . strtr(var_export($this->statictext, true), array("=> \n array (" => "=> array(", "array (\n '_'" => "array(\n'_'", "\n ),\n " => "\n),\n", "\n ),\n" => "\n),\n", "\r" => "")) . ";\n?>\n") !== false);
fclose($f);
}
umask($oldmask);
return $result;
}
} /* End class it_text */
?>
|