define('GETTEXT_NATIVE', 1);
define('GETTEXT_PHP', 2);
-/**
-* Generic gettext static class.
-*
-* This class allows gettext usage with php even if the gettext support is
-* not compiled in php.
-*
-* The developper can choose between the GETTEXT_NATIVE support and the
-* GETTEXT_PHP support on initialisation. If native is not supported, the
-* system will fall back to PHP support.
-*
-* On both systems, this package add a variable interpolation system so you can
-* translate entire dynamic sentences in stead of peace of sentences.
-*
-* Small example without pear error lookup :
-*
-* <?php
-* require_once "get_text.php";
-*
-* get_text::init();
-* get_text::set_language('fr_Fr'); // may throw GetText_Error
-* get_text::add_domain('myAppDomain'); // may throw GetText_Error
-* get_text::set_var('login', $login);
-* get_text::set_var('name', $name);
-*
-* // may throw GetText_Error
-* echo get_text::gettext('Welcome ${name}, you\'re connected with login ${login}');
-*
-* // should echo something like :
-* //
-* // "Bienvenue Jean-Claude, vous ĂȘtes connectĂ© en tant qu'utilisateur jcaccount"
-* //
-* // or if fr_FR translation does not exists
-* //
-* // "Welcome Jean-Claude, you're connected with login jcaccount"
-*
-* ?>
-*
-* A gettext mini-howto should be provided with this package, if you're new
-* to gettext usage, please read it to learn how to build a gettext
-* translation directory (locale).
-*
-* @todo Tools to manage gettext files in php.
-*
-* - non traducted domains / keys
-* - modification of keys
-* - domain creation, preparation, delete, ...
-* - tool to extract required messages from TOF templates
-*
-* @version 0.5
-* @author Laurent Bedubourg <laurent.bedubourg@free.fr>
-*/
-class get_text
-{
- /**
- * This method returns current gettext support class.
- *
- * @return GetText_Support
- * @static 1
- * @access private
- */
- function &_support($set=false)
- {
- static $support_obj;
- if ($set !== false)
- {
- $support_obj = $set;
- }
- elseif (!isset($support_obj))
- {
- trigger_error("get_text not initialized !". endl.
- "Please call get_text::init() before calling ".
- "any get_text function !" . endl , E_USER_ERROR);
- }
- return $support_obj;
- }
-
- /**
- * Initialize gettext package.
- *
- * This method instantiate the gettext support depending on managerType
- * value.
- *
- * GETTEXT_NATIVE try to use gettext php support and fall back to PHP
- * support if not installed.
- *
- * GETTEXT_PHP explicitely request the usage of PHP support.
- *
- * @param int $managerType
- * Gettext support type.
- *
- * @access public
- * @static 1
- */
- function init($managerType = GETTEXT_NATIVE)
- {
+function get_text_init($managerType = GETTEXT_NATIVE) {
+ global $GetText;
+ if (!isset($GetText)) {
+
if ($managerType == GETTEXT_NATIVE)
{
if (function_exists('gettext'))
{
- return get_text::_support(new gettext_native_support());
+ $GetText = new gettext_native_support();
+ return;
}
}
// fail back to php support
- return get_text::_support(new gettext_php_support());
- }
-
- /**
- * Set the language to use for traduction.
- *
- * @param string $lang_code
- * The language code usually defined as ll_CC, ll is the two letter
- * language code and CC is the two letter country code.
- *
- * @throws GetText_Error if language is not supported by your system.
- */
- function set_language($lang_code, $encoding)
- {
- $support = &get_text::_support();
- return $support->set_language($lang_code, $encoding);
- }
-
- /**
- * Add a translation domain.
- *
- * The domain name is usually the name of the .po file you wish to use.
- * For example, if you created a file 'lang/ll_CC/LC_MESSAGES/myapp.po',
- * you'll use 'myapp' as the domain name.
- *
- * @param string $domain
- * The domain name.
- *
- * @param string $path optional
- * The path to the locale directory (ie: /path/to/locale/) which
- * contains ll_CC directories.
- */
- function add_domain($domain, $path=false)
- {
- $support =& get_text::_support();
- return $support->add_domain($domain, $path);
- }
-
- /**
- * Retrieve the translation for specified key.
- *
- * @param string $key
- * String to translate using gettext support.
- */
- function gettext($key)
- {
- $support = &get_text::_support();
- return $support->gettext($key);
- }
-
- /**
- * Add a variable to gettext interpolation system.
- *
- * @param string $key
- * The variable name.
- *
- * @param string $value
- * The variable value.
- */
- function set_var($key, $value)
- {
- $support =& get_text::_support();
- return $support->set_var($key, $value);
- }
-
- /**
- * Add an hashtable of variables.
- *
- * @param hashtable $hash
- * PHP associative array of variables.
- */
- function set_vars($hash)
- {
- $support =& get_text::_support();
- return $support->set_vars($hash);
- }
-
- /**
- * Reset interpolation variables.
- */
- function reset()
- {
- $support =& get_text::_support();
- return $support->reset();
- }
+ $GetText = new gettext_php_support();
+ }
}
function raise_error($str) {
- //echo "$str";
+ error_log($str);
return 1;
}
class gettext_native_support
{
var $_interpolation_vars = array();
+ var $domain_path;
/**
* Set gettext language code.
putenv("LANG=$lang_code");
putenv("LC_ALL=$lang_code");
putenv("LANGUAGE=$lang_code");
-
+
//$set = setlocale(LC_ALL, "$lang_code");
//$set = setlocale(LC_ALL, "$encoding");
- $set = setlocale(LC_ALL, $lang_code.".".$encoding);
+
+ // cover a couple of country/encoding variants
+ $up = strtoupper($encoding);
+ $low = strtolower($encoding);
+ $lshort = strtr($up, '-','');
+ $ushort = strtr($low, '-','');
+
+ if ($lang_code == 'C')
+ $set = setlocale(LC_ALL,'C');
+ else
+ $set = setlocale(LC_ALL, $lang_code.".".$encoding,
+ $lang_code.".".$up, $lang_code.".".$low,
+ $lang_code.".".$ushort, $lang_code.".".$lshort);
+
setlocale(LC_NUMERIC, 'C'); // important for numeric presentation etc.
if ($set === false)
{
+ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') // don't do this test if server is WIN
+ return 0;
$str = sprintf('language code "%s", encoding "%s" not supported by your system',
$lang_code, $encoding);
//$err = new GetText_Error($str);
}
//return 0;
}
-
+ /**
+ * Check system support for given language nedded for gettext.
+ */
+ function check_support($lang_code, $encoding)
+ {
+ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') // don't do this test if server is WIN
+ return true;
+ $old = setlocale(LC_CTYPE, '0'); // LC_MESSAGES does not exist on Win
+ $up = strtoupper($encoding);
+ $low = strtolower($encoding);
+ $lshort = strtr($up, '-','');
+ $ushort = strtr($low, '-','');
+
+ $test = setlocale(LC_ALL,
+ $lang_code.".".$encoding,
+ $lang_code.".".$up,
+ $lang_code.".".$low,
+ $lang_code.".".$ushort,
+ $lang_code.".".$lshort) !== false;
+ setlocale(LC_ALL, $old);
+ setlocale(LC_NUMERIC, 'C');
+ return $test;
+ }
/**
* Add a translation domain.
*/
- function add_domain($domain, $path=false)
+ function add_domain($domain, $path=false, $version='')
{
if ($path === false)
- {
- bindtextdomain($domain, "./locale/");
- }
- else
- {
- bindtextdomain($domain, $path);
- }
+ $path = $this->domain_path;
+ if ($path === false)
+ $path = "./locale";
+ if ($domain == "")
+ $domain = "?";
+ if ($version) {
+ // To avoid need for apache server restart after change of *.mo file
+ // we have to include file version as part of filename.
+ // This is alternative naming convention: $domain = $version.'/'.$domain;
+ $domain .= '-'.$version;
+ }
+ bindtextdomain($domain, $path);
//bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
}
-
+
/**
* Retrieve translation for specified key.
*
*/
function gettext($key)
{
- $value = $this->_get_translation($key);
+ $value = $this->_get_translation($key);
if ($value === false) {
$str = sprintf('Unable to locate gettext key "%s"', $key);
//$err = new GetText_Error($str);
}
}
}
-
+ /**
+ * Check system support for given language (dummy).
+ */
+ function check_support($lang_code, $encoding)
+ {
+ return true;
+ }
/**
* Add a translation domain.
*
* @param string $path optional -- Repository path
* @throws GetText_Error
*/
- function add_domain($domain, $path = "./locale/")
+ function add_domain($domain, $path = false, $version ='')
{
+ if ($path === false)
+ $path = $this->domain_path;
+ if ($path === false)
+ $path = "./locale";
+
+ if ($version) {
+ $domain .= '-'.$version;
+ }
+
if (array_key_exists($domain, $this->_domains))
{
return;
$this->_jobs[] = array($domain, $path);
return;
}
-
+ // Don't fill the domains with false data, it increased the error.log
+ if (strpos($domain, $this->_lang_code) === false)
+ return;
+
$err = $this->_load_domain($domain, $path);
if ($err != 0)
{
$d = new gettext_domain();
$d->name = $domain;
$d->path = $path;
-
if (!file_exists($php_domain) || (filemtime($php_domain) < filemtime($src_domain)))
{
*/
function _parse_line($line, $nbr)
{
+ $line = str_replace("\\\"", "'", $line); // Should be inside preg_match, but I couldn't find the solution. This works.
if (preg_match('/^\s*?#/', $line)) { return; }
if (preg_match('/^\s*?msgid \"(.*?)(?!<\\\)\"/', $line, $m)) {
$this->_store_key();
}
}
-/**
-* get_text related error.
+/*
+ Set current gettext domain path
*/
-//class GetText_Error extends PEAR_Error {}
-
+function set_ext_domain($path='') {
+ global $path_to_root, $GetText;
+ static $domain_stack = array('');
+
+ if ($path) // save path on domain stack
+ array_unshift($domain_stack, $path);
+ else
+ {
+ array_shift($domain_stack);
+ $path = $domain_stack[0];
+ }
+
+ $lang_path = $path_to_root . ($path ? '/' : '') .$path.'/lang';
+ // ignore change when extension does not provide translation structure and test for valid gettext.
+ if (file_exists($lang_path) && isset($GetText))
+ $GetText->add_domain($_SESSION['language']->code,
+ $lang_path, $path ? '' : $_SESSION['language']->version);
+}
?>