sbs/yii2-transliterator-helper

The transliterator helper for the Yii 2 framework

0.3.3 2019-05-23 16:07 UTC

This package is auto-updated.

Last update: 2024-03-24 02:44:42 UTC


README

Build Status

Transliterator Helper transliterates UTF-8 encoded text to US-ASCII.

Installation

The preferred way to install this extension is through composer.

Either run

composer require sbs/yii2-transliterator-helper

or add

"sbs/yii2-transliterator-helper": "*"

to the require section of your application's composer.json file.

Usage

Pass to the method process() the UTF-8 encoded string you wish to transliterate:

use sbs\helpers\TransliteratorHelper;

// will echo AAAAAAAECEEEEIIIIDNOOOOOUUUUYssaaaaaaaeceeeeiiiidnooooouuuuyy
TransliteratorHelper::process('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ', 'en'));

You can use it as application behavior:

use sbs\behaviors\SlugBehavior;

//...
public function behaviors()
{
    return [
        //...
        [
            'class' => SlugBehavior::class,
            'attribute' => 'title',
            'slugAttribute' => 'slug',
        ],
    ];
}

Since version 0.3 you can use SlugInput widget:

Configurations:

You need a registration controller in your main config file in section controllerMap:

use sbs\controllers\TransliterationController;

//...
'controllerMap' => [
    'transliteration' => [
        'class' => TransliterationController::class,
        'lowercase' => false //provides transliteration to lower case, true by default.
    ]
],
//...

Like a widget:

use sbs\widgets\SlugInput;

echo SlugInput::widget([
    'name' => 'News[slug]',
    'sourceName' => 'News[title]'
]);

Like an ActiveForm widget:

use sbs\widgets\SlugInput;

echo $form->field($model, 'slug')->widget(SlugInput::class, [
    'sourceAttribute' => 'title'
]);

That's all. Enjoy.