📜 ⬆️ ⬇️

My Smarty plugins. Part 2

Given the community’s interest in this topic, I’m continuing to introduce my Smarty plugins. The first part can be found here .


1.2. Declination of words

Urgent problem: to bring the user how many comments to the blog entry is. The most terrible option: "2 comments". Slightly less terrible - "Comments: 2". But you want to see inscriptions in normal Russian: “1 comments”, “22 comments”, “38 comments”!

The declension modifier (declination) serves as a purpose for this very purpose.
')
Format: {$ count | declension: 'word forms': 'language'} , where:
$ count - the number of elements recalculated (not displayed)
'word forms' - a semicolon-based listing of the required word forms for the current language. Those. for Russian it is a stump; stump; stumps' (forms for 1, 2 and 5 subjects), for English - 'stump, stumps' (forms for 1 and several objects). It is allowed (and in my projects this is the main possibility) of setting the term from the localization, in the form: 'group / term'.
'language' is the two-letter language code, if it really hurts. Not required.

Examples of using:
{$ messages} {$ messages | declension: 'message; messages; messages'}
or
{$ messages} {$ messages | declension: 'Forum / messages'} - when multilingualism is required

File smarty / plugins / modifier.declension.php ( download )
<? 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 );
}
}
?>

For the modifier to work correctly, in addition to the glossary of terms of the current language ($ Language) already mentioned in past plugins, the $ currentLanguage variable indicating the current language is also required. (I already mentioned in the previous article that the presence of these global variables is for the article, in my original source code there are references to the framework functions)

1.3. File size

Another trivial task: correctly display file sizes. “384 b”, “613.8 Kb”, “14.1 TB”. Someone must translate these thousands and billions of bytes into a user-friendly form. I do this modifier filesize .

Format: {$ filesize | filesize: 'accuracy'} , where:
'accuracy' is an optional number of decimal places for rounding results.

File smarty / plugins / modifier.filesize.php ( download )
<? 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 ;
}
?>


As in the case of the date modifier (described in the first part of the article ), I use localization tables not only for words and expressions of the interface, but also for storing technical information (here the fraction separator character for the current language).

For English, the translations look like this (for Russian, they are not much different, I will not duplicate):
$ 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' ;


I do not want to inflate the article to indecent size, so for now I’ll stop. To be continued.

Source: https://habr.com/ru/post/38416/


All Articles