| Ratibor |
|
|---|---|
|
В общем вот что получилось:
/**
* Makes correct plural forms of words
*
* @global string $lang Current language
* @param int $digit Numeric value
* @param string $expr Word or expression
* @param bool $onlyword Return only words, without numbers
* @param bool $canfrac - Numeric value can be Decimal Fraction
* @return string
*/
function sed_declension($digit, $expr, $onlyword = false, $canfrac = false)
{
if (!is_array($expr))
{
return trim(($onlyword ? '' : "$digit ") . $expr);
}
global $lang;
if ($canfrac)
{
$is_frac = floor($digit) != $digit;
$i = $digit;
}
else
{
$i = preg_replace('#\D+#', '', $digit);
}
$plural = sed_get_plural($i, $lang, $is_frac);
$cnt = count($expr);
return trim(($onlyword ? '' : "$digit ") . (($cnt > 0 && $plural < $cnt) ? $expr[$plural] : ''));
}
/**
* Used in sed_declension to get rules for concrete languages
*
* @param int $plural Numeric value
* @param string $lang Target language code
* @return int
*/
function sed_get_plural($plural, $lang, $is_frac = false)
{
switch ($lang)
{
case 'en':
case 'de':
case 'nl':
case 'se':
return ($plural == 1) ? 1 : 0;
case 'fr':
case 'uk':
return ($plural > 1) ? 0 : 1;
case 'ru':
case 'ua':
if ($is_frac)
{
return 1;
}
$plural %= 100;
return (5 <= $plural && $plural <= 20) ? 2 : ((1 == ($plural %= 10)) ? 0 : ((2 <= $plural && $plural <= 4) ? 1 : 2));
default:
return 0;
}
}Не задавай глупых вопросов, не услышишь вранья.
This post was edited by Ratibor (2009-02-15 17:16, 16 years ago)
|