Inflection extension for Yii2. Support for English / Russian languages to inflect words / names / numbers / money / date&time.
Requirements ¶
Requires only mbstring extension to proper work with multibyte encodings.
Features ¶
Pluralize word with number:
en
:Yii::$app->inflection->pluralize(2, 'item')
=>2 items
ru
:Yii::$app->inflection->pluralize(2, 'элемент')
=>2 элемента
Inflect names to relational cases (applicable in Russian only):
ru
:Yii::$app->inflection->inflectName('Иванов Петр', wapmorgan\yii2inflector\Inflector::DATIVE)
=>Иванову Петру
Inflect geographical names to relational cases (applicable in Russian only):
ru
:Yii::$app->inflection->inflectGeoName('Санкт-Петербург', , wapmorgan\yii2inflector\Inflector::GENITIVE)
=>Санкт-Петербурга
Generate cardinal numerals:
- (WIP)
en
:Yii::$app->inflection->cardinalize(2)
=> 'two' ru
:Yii::$app->inflection->cardinalize(2)
=> 'два'
- (WIP)
Generate ordinal numerals:
en
:Yii::$app->inflection->cardinalize(2)
=> '2nd'ru
:Yii::$app->inflection->cardinalize(2)
=> '2-й'
And more, and more ... Full list is in actual project readme
Usage ¶
- Install it with composer
composer require wapmorgan/yii2-inflection
- Add
wapmorgan\yii2inflection\Inflection
as a serviceinflection
in config (web.php or console.php):$config = [ // ... // ... 'components' => [ // ... 'inflection' => [ 'class' => 'wapmorgan\yii2inflection\Inflection' ] ], // ... ];
- Optional service parameters:
-
language
- default language for inflection. Currently supported langs isru
(Russian) anden
(English). If you passed unsupported or unknown language, an Exception will be throwed during service initializion. By default, it uses language parameter of current application.
- Call any methods described above in a controller / command / view.
$word = 'новость'; echo Yii::$app->inflection->pluralize(rand(2, 139), $word).PHP_EOL; echo Yii::$app->inflection->pluralize(rand(9, 69), $word).PHP_EOL;
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.