<? php
/ **
* Smarty plugin - declension modifier
* @package Smarty
* @subpackage plugins
* /
/ **
* Declension modifier: the declension of nouns by the rules of the English language
*
* @param array $ forms (eg: 0 => article, 1 => articles)
* @param int $ count
* @return string
* /
function smarty_modifier_declension_en ( $ forms , $ count )
{
if ( $ count == 1 )
return $ forms [ 0 ];
else
return $ forms [ 1 ];
}
/ **
* Declension modifier: the declension of nouns according to the rules of the Russian language
*
* @param array $ forms (eg: 0 => stump, 1 => stump, 2 => stumps)
* @param int $ count
* @return string
* /
function smarty_modifier_declension_ru ( $ forms , $ count )
{
$ mod100 = $ count % 100 ;
switch ( $ count % 10 ) {
case 1 :
if ( $ mod100 == 11 )
return $ forms [ 2 ];
else
return $ forms [ 0 ];
case 2 :
case 3 :
case 4 :
if (( $ mod100 > 10 ) && ( $ mod100 < 20 ))
return $ forms [ 2 ];
else
return $ forms [ 1 ];
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 0 :
return $ forms [ 2 ];
}
}
/ **
* Declension modifier: declension of nouns
*
* @param int $ count
* @param string $ forms
* @param string $ language
* @return string
* /
function smarty_modifier_declension ( $ count , $ forms , $ language = '' )
{
global $ currentLanguage , $ Language ;
if (! $ language )
$ language = $ currentLanguage ;
$ count = abs ( $ count );
// Trying to catch the term from the dictionary
if ( preg_match ( '/^(.*)\/(.*)$/' , $ forms , $ termine ))
{
if ( $ termine [ 1 ] && $ termine [ 2 ])
{
$ forms = $ Language [ $ termine [ 1 ]] [ $ termine [ 2 ]];
}
}
// Select individual word forms
$ forms = explode ( ';' , $ forms );
$ fn = 'smarty_modifier_declension_' . $ language ;
if ( function_exists ( $ fn ))
{
// There is a personal function for the current language
return $ fn ( $ forms , $ count );
} else {
// We act in the image of the English language
return smarty_modifier_declension_en ( $ forms , $ count );
}
}
?>
<? php
/ **
* Smarty plugin - filesize modifier
*
* @author Vasily Melenchuk
* @package Smarty
* @subpackage plugins
* /
/ **
* Modifier filesize: output sizes for files
*
* @param int / float $ size
* @param int $ precision - required accuracy (decimal places)
* @return string
* /
function smarty_modifier_filesize ( $ size , $ precision = 2 )
{
global $ Language ;
if (! is_numeric ( $ precision ) ||! is_numeric ( $ size ))
{
// Something is clearly wrong
trigger_error ( 'Modifier filesize: Invalid input params' );
return '' ;
}
// Find the appropriate value
$ result = '?' ;
$ multiplier = 1 ;
while ( $ multiplier <= 1099511627776 ) // 1 TB in bytes. Enough for now
{
if ( $ size / $ multiplier < 1 )
{
break;
} else {
$ result = round ( $ size / $ multiplier , $ precision ). '& nbsp;' . $ Language [ 'global' ] [ 'filesize_' . $ multiplier ];
}
$ multiplier * = 1024 ;
}
// Final Touch: Fractional Separator
$ result = str_replace ( '.' , $ Language [ 'global' ] [ 'decimal_separator' ], $ result );
return $ result ;
}
?>
$ Language [ 'global' ] [ 'decimal_separator' ] = '.' ;
$ Language [ 'global' ] [ 'filesize_1' ] = 'b' ;
$ Language [ 'global' ] [ 'filesize_1024' ] = 'Kb' ;
$ Language [ 'global' ] [ 'filesize_1048576' ] = 'Mb' ;
$ Language [ 'global' ] [ 'filesize_1073741824' ] = 'Gb' ;
$ Language [ 'global' ] [ 'filesize_1099511627776' ] = 'Tb' ;
Source: https://habr.com/ru/post/38416/
All Articles