2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
4 // Copyright (c) 2003 Laurent Bedubourg
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
23 //require_once "PEAR.php";
25 define('GETTEXT_NATIVE', 1);
26 define('GETTEXT_PHP', 2);
28 function get_text_init($managerType = GETTEXT_NATIVE) {
30 if (!isset($_SESSION['get_text'])) {
32 if ($managerType == GETTEXT_NATIVE)
34 if (function_exists('gettext'))
36 $_SESSION['get_text'] = new gettext_native_support();
40 // fail back to php support
41 $_SESSION['get_text'] = new gettext_php_support();
45 function raise_error($str) {
50 function is_error($err) {
55 * Interface to gettext native support.
57 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
60 class gettext_native_support
62 var $_interpolation_vars = array();
65 * Set gettext language code.
66 * @throws GetText_Error
68 function set_language($lang_code, $encoding)
70 putenv("LANG=$lang_code");
71 putenv("LC_ALL=$lang_code");
72 putenv("LANGUAGE=$lang_code");
74 //$set = setlocale(LC_ALL, "$lang_code");
75 //$set = setlocale(LC_ALL, "$encoding");
77 // cover a couple of country/encoding variants
78 $up = strtoupper($encoding);
79 $low = strtolower($encoding);
80 $lshort = strtr($up, '-','');
81 $ushort = strtr($low, '-','');
83 $set = setlocale(LC_ALL, $lang_code.".".$encoding,
84 $lang_code.".".$up, $lang_code.".".$low,
85 $lang_code.".".$ushort, $lang_code.".".$lshort);
87 setlocale(LC_NUMERIC, 'C'); // important for numeric presentation etc.
90 $str = sprintf('language code "%s", encoding "%s" not supported by your system',
91 $lang_code, $encoding);
92 //$err = new GetText_Error($str);
93 //return PEAR::raise_error($err);
94 return raise_error("1 " . $str);
99 * Check system support for given language nedded for gettext.
101 function check_support($lang_code, $encoding)
103 $old = setlocale(LC_MESSAGES, '0');
104 $up = strtoupper($encoding);
105 $low = strtolower($encoding);
106 $lshort = strtr($up, '-','');
107 $ushort = strtr($low, '-','');
109 $test = setlocale(LC_MESSAGES,
110 $lang_code.".".$encoding,
113 $lang_code.".".$ushort,
114 $lang_code.".".$lshort) !== false;
116 setlocale(LC_MESSAGES, $old);
120 * Add a translation domain.
122 function add_domain($domain, $path=false)
126 bindtextdomain($domain, "./locale/");
130 bindtextdomain($domain, $path);
132 //bind_textdomain_codeset($domain, $encoding);
137 * Retrieve translation for specified key.
141 function _get_translation($key)
143 return gettext($key);
148 * Reset interpolation variables.
152 $this->_interpolation_vars = array();
156 * Set an interpolation variable.
158 function set_var($key, $value)
160 $this->_interpolation_vars[$key] = $value;
164 * Set an associative array of interpolation variables.
166 function set_vars($hash)
168 $this->_interpolation_vars = array_merge($this->_interpolation_vars,
173 * Retrieve translation for specified key.
175 * @param string $key -- gettext msgid
176 * @throws GetText_Error
178 function gettext($key)
180 $value = $this->_get_translation($key);
181 if ($value === false) {
182 $str = sprintf('Unable to locate gettext key "%s"', $key);
183 //$err = new GetText_Error($str);
184 //return PEAR::raise_error($err);
185 return raise_error("2 " . $str);
188 while (preg_match('/\$\{(.*?)\}/sm', $value, $m)) {
189 list($src, $var) = $m;
191 // retrieve variable to interpolate in context, throw an exception
193 $var2 = $this->_get_var($var);
194 if ($var2 === false) {
195 $str = sprintf('Interpolation error, var "%s" not set', $var);
196 //$err = new GetText_Error($str);
197 //return PEAR::raise_error($err);
198 return raise_error("3 " . $str);
200 $value = str_replace($src, $var2, $value);
206 * Retrieve an interpolation variable value.
211 function _get_var($name)
213 if (!array_key_exists($name, $this->_interpolation_vars)) {
216 return $this->_interpolation_vars[$name];
222 * Implementation of get_text support for PHP.
224 * This implementation is abble to cache .po files into php files returning the
225 * domain translation hashtable.
228 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
230 class gettext_php_support extends gettext_native_support
232 var $_path = 'locale/';
233 var $_lang_code = false;
234 var $_domains = array();
236 var $_jobs = array();
239 * Set the translation domain.
241 * @param string $lang_code -- language code
242 * @throws GetText_Error
244 function set_language($lang_code, $encoding)
246 // if language already set, try to reload domains
247 if ($this->_lang_code !== false and $this->_lang_code != $lang_code)
249 foreach ($this->_domains as $domain)
251 $this->_jobs[] = array($domain->name, $domain->path);
253 $this->_domains = array();
257 $this->_lang_code = $lang_code;
259 // this allow us to set the language code after
261 while (count($this->_jobs) > 0)
263 list($domain, $path) = array_shift($this->_jobs);
264 $err = $this->add_domain($domain, $path);
265 // error raised, break jobs
266 /*if (PEAR::is_error($err)) {
276 * Check system support for given language (dummy).
278 function check_support($lang_code, $encoding)
283 * Add a translation domain.
285 * @param string $domain -- Domain name
286 * @param string $path optional -- Repository path
287 * @throws GetText_Error
289 function add_domain($domain, $path = "./locale/")
291 if (array_key_exists($domain, $this->_domains))
296 if (!$this->_lang_code)
298 $this->_jobs[] = array($domain, $path);
302 $err = $this->_load_domain($domain, $path);
312 * Load a translation domain file.
314 * This method cache the translation hash into a php file unless
315 * GETTEXT_NO_CACHE is defined.
317 * @param string $domain -- Domain name
318 * @param string $path optional -- Repository
319 * @throws GetText_Error
322 function _load_domain($domain, $path = "./locale")
324 $src_domain = $path . "/$this->_lang_code/LC_MESSAGES/$domain.po";
325 $php_domain = $path . "/$this->_lang_code/LC_MESSAGES/$domain.php";
327 if (!file_exists($src_domain))
329 $str = sprintf('Domain file "%s" not found.', $src_domain);
330 //$err = new GetText_Error($str);
331 //return PEAR::raise_error($err);
332 return raise_error("4 " . $str);
335 $d = new gettext_domain();
339 if (!file_exists($php_domain) || (filemtime($php_domain) < filemtime($src_domain)))
342 // parse and compile translation table
343 $parser = new gettext_php_support_parser();
344 $hash = $parser->parse($src_domain);
345 if (!defined('GETTEXT_NO_CACHE'))
347 $comp = new gettext_php_support_compiler();
348 $err = $comp->compile($hash, $src_domain);
349 /*if (PEAR::is_error($err)) {
361 $d->_keys = include $php_domain;
363 $this->_domains[] = &$d;
367 * Implementation of gettext message retrieval.
369 function _get_translation($key)
371 for ($i = $this->_end; $i >= 0; $i--)
373 if ($this->_domains[$i]->has_key($key))
375 return $this->_domains[$i]->get($key);
383 * Class representing a domain file for a specified language.
386 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
393 var $_keys = array();
395 function has_key($key)
397 return array_key_exists($key, $this->_keys);
402 return $this->_keys[$key];
407 * This class is used to parse gettext '.po' files into php associative arrays.
410 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
412 class gettext_php_support_parser
414 var $_hash = array();
419 * Parse specified .po file.
422 * @throws GetText_Error
424 function parse($file)
426 $this->_hash = array();
427 $this->_current_key = false;
428 $this->_current_value = "";
430 if (!file_exists($file))
432 $str = sprintf('Unable to locate file "%s"', $file);
433 //$err = new GetText_Error($str);
434 //return PEAR::raise_error($err);
435 return raise_error($str);
438 $lines = file($file);
439 foreach ($lines as $line)
441 $this->_parse_line($line, ++$i);
453 function _parse_line($line, $nbr)
455 if (preg_match('/^\s*?#/', $line)) { return; }
456 if (preg_match('/^\s*?msgid \"(.*?)(?!<\\\)\"/', $line, $m)) {
458 $this->_current_key = $m[1];
461 if (preg_match('/^\s*?msgstr \"(.*?)(?!<\\\)\"/', $line, $m)) {
462 $this->_current_value .= $m[1];
465 if (preg_match('/^\s*?\"(.*?)(?!<\\\)\"/', $line, $m)) {
466 $this->_current_value .= $m[1];
472 * Store last key/value pair into building hashtable.
476 function _store_key()
478 if ($this->_current_key === false) return;
479 $this->_current_value = str_replace('\\n', "\n", $this->_current_value);
480 $this->_hash[$this->_current_key] = $this->_current_value;
481 $this->_current_key = false;
482 $this->_current_value = "";
488 * This class write a php file from a gettext hashtable.
490 * The produced file return the translation hashtable on include.
492 * @throws GetText_Error
494 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
496 class gettext_php_support_compiler
499 * Write hash in an includable php file.
501 function compile(&$hash, $source_path)
503 $dest_path = preg_replace('/\.po$/', '.php', $source_path);
504 $fp = @fopen($dest_path, "w");
507 $str = sprintf('Unable to open "%s" in write mode.', $dest_path);
508 //$err = new GetText_Error($str);
509 //return PEAR::raise_error($err);
510 return raise_error($str);
512 fwrite($fp, '<?php' . "\n");
513 fwrite($fp, 'return array(' . "\n");
514 foreach ($hash as $key => $value)
516 $key = str_replace("'", "\\'", $key);
517 $value = str_replace("'", "\\'", $value);
518 fwrite($fp, ' \'' . $key . '\' => \'' . $value . "',\n");
520 fwrite($fp, ');' . "\n");
527 * get_text related error.
529 //class GetText_Error extends PEAR_Error {}