View of /mambo/branches/4.6/includes/phpgettext/phpgettext.class.php
Parent Directory
|
Revision Log
Revision 39 -
(download)
(annotate)
Mon Dec 19 21:03:57 2005 UTC (7 years, 4 months ago) by csouza
File size: 9020 byte(s)
Mon Dec 19 21:03:57 2005 UTC (7 years, 4 months ago) by csouza
File size: 9020 byte(s)
<?php /** * @version 0.9 * @author Carlos Souza * @copyright Copyright (c) 2005 Carlos Souza <csouza@web-sense.net> * @package PHPGettext * @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @link http://phpgettext.web-sense.net * * * class PHPGettext * php replacement for gettext where it is not available * * not all gettext functions are implemented yet * * * */ class PHPGettext { /** * Enter description here... * * @var bool */ var $has_gettext; /** * The current locale. eg: en-GB * * @var string */ var $locale; /** * The current domain * * @var string */ var $domain; /** * The current character set * * @var unknown_type */ var $charset; /** * Container for the loaded domains * * @var array */ var $text_domains; /** * The asssociative array of messages for the current domain * * @var unknown_type */ var $messages; /** * Enter description here... * * @return PHPGettext */ function PHPGettext() {} /** * Enter description here... * * Lookup the locale from the environment variables. * Priority order for gettext is: * 1. LANGUAGE * 2. LC_ALL * 3. LC_MESSAGE * 4. LANG * * @return unknown */ function getlocale() { if (empty($this->locale)) { $langs = array( getenv('LANGUAGE'), getenv('LC_ALL'), getenv('LC_MESSAGE'), getenv('LANG') ); foreach ($langs as $lang) { if ($lang){ $this->locale = $lang; break; } } } return $this->locale; } /** * Set locale environment variables */ function setlocale($lang) { $this->locale = $lang; putenv("LANGUAGE=$lang"); putenv("LC_ALL=$lang"); putenv("LC_MESSAGE=$lang"); putenv("LANG=$lang"); $ret = setlocale(LC_ALL, ''); return $ret; } /** * Alias for gettext * will also output the result if $output = true */ function _($message, $output = false){ $ret = $this->gettext($message); if ($output) { echo $ret; return true; } return $ret; } /** * Lookup a message in the current domain * returns translation if it exists or original message */ function gettext($message){ if ($this->has_gettext){ return gettext($message); } if (isset( $this->messages[$this->domain][$message])) { $translation = $this->messages[$this->domain][$message]; return $translation; } return $message; } /** * Specify the character encoding in which the messages * from the DOMAIN message catalog will be returned * */ function bind_textdomain_codeset($domain, $charset){ if ($this->has_gettext){ bind_textdomain_codeset($domain, $charset); } return $this->test_domains[$domain]["charset"] = $charset; } /** * Sets the path for a domain * if gettext is unavailable, translation files will be loaded here * */ function bindtextdomain($domain, $path){ if ($this->has_gettext){ bindtextdomain($domain, $path); } else { $this->load($domain, $path); } return $this->test_domains[$domain]["path"] = $path; } /** * Sets the default domain textdomain */ function textdomain($domain = null){ if ($this->has_gettext){ textdomain($domain); } if (is_null($domain)) return $this->domain; else return $this->domain = $domain; } /** * Override the current domain * The dgettext() function allows you to override the current domain for a single message lookup. */ function dgettext($domain, $message){ if (array_key_exists($domain, $this->messages)) $translation = $this->messages[$domain][$message]; if ($translation) return $translation; return $message; } /** * Plural version of gettext * * not yet implemented */ function ngettext($msg1, $msg2, $count){ if ($this->has_gettext){ return ngettext($msg1, $msg2, $count); } dump($this->domain); if (isset( $this->messages[$this->domain][$msg1])) { $translation = $this->messages[$this->domain][$msg1]; return $translation; } return $translation; } /** * Plural version of dgettext . * not yet implemented */ function dngettext($domain, $msg1, $msg2, $count){ return $msg1; } function load($domain, $path) { require_once('phpgettext.catalog.php'); $catalog = new PHPGettext_catalog($domain, $path); $catalog->setproperty('mode', _MODE_MO_); $catalog->setproperty('lang', $this->getlocale()); $catalog->load(); foreach ($catalog->strings as $string) $this->messages[$domain][$string->msgid] = $string->msgstr; } /** * Overrides the domain for a single lookup string * This function allows you to override the current domain for a single message lookup. * It also allows you to specify a category. * Categories are folders within the languages directory . * currently, only LC_MESSAGES is implemented * * The values for categories are: * LC_CTYPE 0 * LC_NUMERIC 1 * LC_TIME 2 * LC_COLLATE 3 * LC_MONETARY 4 * LC_MESSAGES 5 * LC_ALL 6 * * not yet implemented */ function dcgettext($domain, $message, $category){ return $message; } /** * Plural version of dcgettext * not yet implemented */ function dcngettext($domain, $msg1, $msg2, $count, $category){ return $msg1; } } class PHPGettextAdmin { function PHPGettextAdmin(){} /** * Enter description here... * * @param unknown_type $domain * @param unknown_type $langdir */ function msgfmt ($domain, $langdir) { $cmd = "msgfmt"; $arg = "-o $langdir/LC_MESSAGES/$domain.mo $langdir/$domain.po"; //$exec = 'msgfmt --help'; return $this->execute($cmd, $arg); } /** * Enter description here... * * @param unknown_type $domain * @param unknown_type $langdir */ function msgmerge ($outdated, $recent, $langdir) { #msgmerge $langdir/$old.po $new.po $cmd = "msgmerge"; $arg = "$langdir/$outdated $recent"; //$exec = 'msgmerge --help'; return $this->execute($cmd, $arg); } /** * Invoke the xgettext utility with $args * the xgettext executable must be in PATH * * @param string the commandline arguments to gettext * @return unknown */ function xgettext($args) { $cmd = substr(strtoupper($_ENV['OS']), 0, 3) == 'WIN' ? "xgettext.exe " : "xgettext "; return $this->execute($cmd.$args); } /** * Enter description here... * * @param unknown_type $domain * @param unknown_type $langdir */ function execute($cmd) { if (substr(strtoupper(php_uname()), 0, 3) == 'WIN'){ $cmd = "cmd /C $cmd"; $shell = new COM("WScript.Shell"); $shell->Run($cmd, 0, false); } else { $cmd = "$cmd"; $lastline = exec($cmd, $output, $ret); } return $ret; } } /** * Enter description here... * * @return unknown */ function &phpgettext(){ static $gettext; if (is_null($gettext)) { require_once('phpgettext.class.php'); $gettext = new PHPGettext(); if (!function_exists("gettext") || !function_exists("_")) { $gettext->has_gettext = false; require_once('phpgettext.compat.php'); } } return $gettext; } function T_($message) { $gettext =& phpgettext(); return $gettext->gettext($message); } function Tn_($msg1, $msg2, $count) { $gettext =& phpgettext(); return $gettext->ngettext($msg1, $msg2, $count); } function Td_($domain, $message) { $gettext =& phpgettext(); return $gettext->dgettext($domain, $message); } function Tdn_($domain, $msg1, $msg2, $count) { $gettext =& phpgettext(); return $gettext->dngettext($domain, $msg1, $msg2, $count); } ?>
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

