Yii 2.0: yii2-inflection

Inflection extension for Yii2. Support for English / Russian languages to inflect words / names / numbers / money / date&time.
2 followers

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

  1. Pluralize word with number:

    • en: Yii::$app->inflection->pluralize(2, 'item') => 2 items
    • ru: Yii::$app->inflection->pluralize(2, 'элемент') => 2 элемента
  2. Inflect names to relational cases (applicable in Russian only):

    • ru: Yii::$app->inflection->inflectName('Иванов Петр', wapmorgan\yii2inflector\Inflector::DATIVE) => Иванову Петру
  3. Inflect geographical names to relational cases (applicable in Russian only):

    • ru: Yii::$app->inflection->inflectGeoName('Санкт-Петербург', , wapmorgan\yii2inflector\Inflector::GENITIVE) => Санкт-Петербурга
  4. Generate cardinal numerals:

    • (WIP) en: Yii::$app->inflection->cardinalize(2) => 'two'
    • ru: Yii::$app->inflection->cardinalize(2) => 'два'
  5. Generate ordinal numerals:

    • en: Yii::$app->inflection->cardinalize(2) => '2nd'
    • ru: Yii::$app->inflection->cardinalize(2) => '2-й'
  6. And more, and more ... Full list is in actual project readme

Usage

  1. Install it with composer

    composer require wapmorgan/yii2-inflection
    
  2. Add wapmorgan\yii2inflection\Inflection as a service inflection 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 is ru (Russian) and en (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.
  3. 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;

Resources

Be the first person to leave a comment

Please to leave your comment.

Create extension