<?php include_once "pgettext.php"; $domain = 'messages'; $directory = dirname(__FILE__); putenv("LANGUAGE="); $locale = "sv_SE.utf8"; setlocale(LC_MESSAGES, $locale); bindtextdomain($domain, $directory); textdomain($domain); bind_textdomain_codeset($domain, 'UTF-8'); foreach (array(1, 2) as $nbr) { printf(npgettext("body", "One heart\n", "%d hearts\n", $nbr), $nbr); printf(npgettext("place", "One heart\n", "%d hearts\n", $nbr), $nbr); } printf(pgettext("door", "Open\n")); printf(pgettext("book", "Open\n"));
function T_npgettext($context, $singular, $plural, $number) { if (_check_locale_and_function('npgettext')) { return npgettext($context, $singular, $plural, $number); } else { return _npgettext($context, $singular, $plural, $number); } }
/** * Translates the string with respect to the given context and plural forms, also replaces placeholders with supplied arguments. * If no translation is found, the original string will be used. Unlimited number of parameters supplied. * Parameter placeholders must be defined as %1$s, %2$s etc. * * Example: _xn('%1$s message for arg1 "%2$s"', '%1$s messages for arg1 "%2$s"', 3, 'context', 'arg1Value'); * returns: '3 messages for arg1 "arg1Value"' * * @param string $message string to translate * @param string $messagePlural string to translate for plural form * @param int $num number to determine usage of plural form, also is used as first replace argument * @param string $context context of the string * @param string $param parameter to be replace the first placeholder * @param string $param,... unlimited number of optional parameters * * @return string */ function _xn($message, $messagePlural, $num, $context) { $arguments = array_slice(func_get_args(), 4); array_unshift($arguments, $num); return $context == '' ? _params(ngettext($message, $messagePlural, $num), $arguments) : _params(npgettext($context, $message, $messagePlural, $num), $arguments); }
/** * Translates the string with respect to the given context and plural forms, also replaces placeholders with supplied arguments. * If no translation is found, the original string will be used. Unlimited number of parameters supplied. * * Example: _xn('%1$s message for arg1 "%2$s"', '%1$s messages for arg1 "%2$s"', 3, 'context', 'arg1Value'); * returns: '3 messagges for arg1 "arg1Value"' * * @param string $message string to translate * @param string $message_plural string to translate for plural form * @param int $num number to determine usage of plural form, also is used as first replace argument * @param string $context context of the string * * @return string */ function _xn($message, $message_plural, $num, $context) { $arguments = array_slice(func_get_args(), 4); array_unshift($arguments, $num); if ($context == '') { return vsprintf(ngettext($message, $message_plural, $num), $arguments); } else { return vsprintf(npgettext($context, $message, $message_plural, $num), $arguments); } }
public function testNpgettext() { if (self::$functionExists['npgettext']) { $this->markTestSkipped('Function npgettext already defined'); } $translation = npgettext('firstContext', 'singular with context', '%d plurals with context', 1); $this->assertEquals('singular with context', $translation); $translation = npgettext('firstContext', 'singular with context', '%d plurals with context', 2); $this->assertEquals('%d plurals with context', $translation); $this->setlocaleCs(); $translation = npgettext('firstContext', 'singular with context', '%d plurals with context', 1); $this->assertEquals('jednotné číslo s kontextem', $translation); $translation = npgettext('firstContext', 'singular with context', '%d plurals with context', 2); $this->assertEquals('%d množná čísla s kontextem', $translation); $translation = npgettext('firstContext', 'singular with context', '%d plurals with context', 5); $this->assertEquals('%d množných čísel s kontextem', $translation); }
/** * Smarty block function, provides gettext support for smarty. * * The block content is the text that should be translated. * * Any parameter that is sent to the function will be represented as %n in the translation text, * where n is 1 for the first parameter. The following parameters are reserved: * - escape - sets escape mode: * - 'html' for HTML escaping, this is the default. * - 'js' for javascript escaping. * - 'url' for url escaping. * - 'no'/'off'/0 - turns off escaping * - plural - The plural version of the text (2nd parameter of ngettext()) * - count - The item count for plural mode (3rd parameter of ngettext()) * - domain - Textdomain to be used, default if skipped (dgettext() instead of gettext()) * - context - gettext context. reserved for future use. * * @param array $params * @param string $text * @link http://www.smarty.net/docs/en/plugins.block.functions.tpl * @return string */ function smarty_block_t($params, $text) { if (!isset($text)) { return $text; } // set escape mode, default html escape if (isset($params['escape'])) { $escape = $params['escape']; unset($params['escape']); } else { $escape = 'html'; } // set plural parameters 'plural' and 'count'. if (isset($params['plural'])) { $plural = $params['plural']; unset($params['plural']); // set count if (isset($params['count'])) { $count = $params['count']; unset($params['count']); } } // get domain param if (isset($params['domain'])) { $domain = $params['domain']; unset($params['domain']); } else { $domain = null; } // get context param if (isset($params['context'])) { $context = $params['context']; unset($params['context']); } else { $context = null; } // use plural if required parameters are set if (isset($count) && isset($plural)) { // use specified textdomain if available if (isset($domain) && isset($context)) { $text = dnpgettext($domain, $context, $text, $plural, $count); } elseif (isset($domain)) { $text = dngettext($domain, $text, $plural, $count); } elseif (isset($context)) { $text = npgettext($context, $text, $plural, $count); } else { $text = ngettext($text, $plural, $count); } } else { // use specified textdomain if available if (isset($domain) && isset($context)) { $text = dpgettext($domain, $context, $text); } elseif (isset($domain)) { $text = dgettext($domain, $text); } elseif (isset($context)) { $text = pgettext($context, $text); } else { $text = gettext($text); } } // run strarg if there are parameters if (count($params)) { $text = smarty_gettext_strarg($text, $params); } switch ($escape) { case 'html': $text = nl2br(htmlspecialchars($text)); break; case 'javascript': case 'js': // javascript escape $text = strtr($text, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\\/')); break; case 'url': // url escape $text = urlencode($text); break; } return $text; }
<?php echo _('Welcome to first domain'); echo _('And second domain'); echo pgettext('yearn', 'miss'); echo pgettext('mishit', 'miss'); echo npgettext('device', 'mouse', 'mouses', 1); echo npgettext('animal', 'mouse', 'mice', 1);