wapmorgan / yii2-inflection
Inflection extension for Yii2. Support for English / Russian languages to inflect words / names / numbers / money / date&time.
Installs: 79 960
Dependents: 0
Suggesters: 0
Security: 0
Stars: 31
Watchers: 2
Forks: 7
Open Issues: 0
Type:yii2-extension
Requires
- wapmorgan/morphos: ~3.2.3
- yiisoft/yii2: ^2.0
Requires (Dev)
- phpunit/phpunit: ~4.8
README
Inflection extension for Yii2. Support for English / Russian languages to inflect words / names / numbers / money / date&time.
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\yii2inflection\Inflector::DATIVE)
=>Иванову Петру
- Description of all supported cases are in Cases section.
- Inflect geographical names to relational cases (applicable in Russian only):
ru
:Yii::$app->inflection->inflectGeoName('Санкт-Петербург', wapmorgan\yii2inflection\Inflector::GENITIVE)
=>Санкт-Петербурга
- Generate cardinal numerals:
en
:Yii::$app->inflection->cardinalize(2)
=> 'two'ru
:Yii::$app->inflection->cardinalize(2)
=> 'два'
- Generate ordinal numerals:
en
:Yii::$app->inflection->ordinalize(2)
=> '2nd'ru
:Yii::$app->inflection->ordinalize(2)
=> '2-й'
And full form:
en
:Yii::$app->inflection->ordinalize(2, wapmorgan\yii2inflection\Inflector::FULL)
=> 'second'ru
:Yii::$app->inflection->ordinalize(2, wapmorgan\yii2inflection\Inflector::FULL)
=> 'второй'
- Money to words:
- (WIP)
en
:Yii::$app->inflection->monetize(wapmorgan\yii2inflection\Inflector::DOLLAR, 122.04)
=> 'one hundred twenty-two dollars four cents' ru
:Yii::$app->inflection->monetize(wapmorgan\yii2inflection\Inflector::DOLLAR, 122.04)
=> 'сто двадцать два доллара четыре цента'- Description of all supported currencies are in Currencies section.
- Data range to words:
en
:Yii::$app->inflection->textizeTimeRange(new DateInterval('P2Y'))
=> '2 years'ru
:Yii::$app->inflection->textizeTimeRange(new DateInterval('P2Y'))
=> '2 года'
WIP means Work-in-progress i.e this feature is not supported now, but planned to be implemented.
Cases
Currencies
How does it work
It uses built-in inflector for English pluralization (yii\helpers\Inflector
) and Morphos for English & Russian on-the-fly inflection (without dictionaries).
Installation
- Install extension
composer require wapmorgan/yii2-inflection
- Add
wapmorgan\yii2inflection\Inflection
as a serviceinflection
in config and specify proper target language of your application (web.php or console.php):
$config = [ // ... 'language' => 'ru_RU', // for example, Russian // ... 'components' => [ // ... 'inflection' => [ 'class' => 'wapmorgan\yii2inflection\Inflection' ] ], // ... ];
- Optional service parameters:
language
- default language for inflection. By default, it uses language parameter of current application, but you can change this behavior. Currently supported langs isru
(Russian) anden
(English) or their's modification (ru_RU
,en_US
). If you pass unsupported or unknown language, an Exception will be thrown during service initializion.defaultCurrency
- default currency when converting money to words. If set, you can callmonetize(float $value)
without currency:Yii::$app->inflection->monetize(123.45)
=>сто двадцать три рубля сорок пять копеек
- 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;