**/ class twIf_i18n { private static $Domains = array (); private static $twIfgetText = array (); // {{{ getLocalePath /** * Retrive the path where to find our locales * * @access public * @return string **/ public static function getLocalePath () { return dirname (__FILE__) . '/locale/'; } // }}} // {{{ setLocale /** * Set the locale to use * * @param string $Locale * * @access public * @return void **/ public static function setLocale ($Locale) { setlocale (LC_ALL, $Locale); putenv ('LC_ALL=' . $Locale); } // }}} public static function loadTextdomain ($Domain, $Path) { if (TWIF_HAVE_GETTEXT) bindtextdomain ($Domain, $Path); else twIf_i18n::loadManual ($Domain, $Path); self::$Domains [$Domain] = $Domain; } // {{{ loadManual /** * Try to load our language-files manually * * @access public * @return void **/ public static function loadManual ($Domain, $Path) { // Define where to find our locales $twIf_Locales = $Path; // Try to detect the locale if ((($Locale = getenv ('LC_MESSAGES')) == '') && (($Locale = getenv ('LC_ALL')) == '')) $Locale = 'en_US'; // Strip anything we do not support if (($p = strpos ($Locale, '.')) !== false) $Locale = substr ($Locale, 0, $p); if (($p = strpos ($Locale, '@')) !== false) $Locale = substr ($Locale, 0, $p); // Try to find the best directory while (($Locale != '') && !is_file ($twIf_Locales . $Locale . '/LC_MESSAGES/' . $Domain . '.po')) $Locale = substr ($Locale, 0, strrpos ($Locale, '_')); if ($Locale == '') return false; // Read the locales $Locales = file ($twIf_Locales . $Locale . '/LC_MESSAGES/' . $Domain . '.po'); // Parse the locales $String = null; if (!isset (self::$twIfgetText [$Domain])) self::$twIfgetText [$Domain] = array (); for ($i = 0; $i < count ($Locales); $i++) if (substr ($Locales [$i], 0, 6) == 'msgid ') { $p = strpos ($Locales [$i], '"') + 1; $String = substr ($Locales [$i], $p, strrpos ($Locales [$i], '"') - $p); } elseif (($String !== null) && (substr ($Locales [$i], 0, 7) == 'msgstr ')) { $p = strpos ($Locales [$i], '"') + 1; self::$twIfgetText [$Domain][$String] = substr ($Locales [$i], $p, strrpos ($Locales [$i], '"') - $p); $String = null; } } // }}} // {{{ getText /** * Translate a given string * * @param string $Value * @param mixed ... (optional) Values for printf * * @access public * @return string **/ public static function getText ($Value) { $Value = self::getText_Read ($Value, 'twIf'); // Check wheter to not printf the Value if (func_num_args () < 2) return $Value; // Retrive arguements for printf $Args = func_get_args (); array_shift ($Args); return vsprintf ($Value, $Args); } // }}} private static function getText_Read ($Value, $Domain) { if (TWIF_HAVE_GETTEXT) { $oValue = dgettext ($Domain, strval ($Value)); if ($oValue == $Value) foreach (self::$Domains as $Dom) if ($Dom != $Domain) { $oValue = dgettext ($Dom, $Value); if ($oValue != $Value) break; } } else { $oValue = $Value; if (isset (self::$twIfgetText [$Domain][$Value])) $oValue = self::$twIfgetText [$Domain][$Value]; } return $oValue; } } // Setup i18n twIf_i18n::setLocale ('de_DE.UTF-8'); if (extension_loaded ('gettext')) define ('TWIF_HAVE_GETTEXT', true); else define ('TWIF_HAVE_GETTEXT', false); twIf_i18n::loadTextdomain ('twIf', twIf_i18n::getLocalePath ()); ?>