Transliterator Helper for Yii 2
, (*1)
Transliterator Helper transliterates UTF-8 encoded text to US-ASCII., (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
composer require sbs/yii2-transliterator-helper
or add, (*5)
"sbs/yii2-transliterator-helper": "*"
to the require section of your application's composer.json
file., (*6)
Usage
Pass to the method process()
the UTF-8 encoded string you wish to transliterate:, (*7)
use sbs\helpers\TransliteratorHelper;
// will echo AAAAAAAECEEEEIIIIDNOOOOOUUUUYssaaaaaaaeceeeeiiiidnooooouuuuyy
TransliteratorHelper::process('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ', 'en'));
You can use it as application behavior:, (*8)
use sbs\behaviors\SlugBehavior;
//...
public function behaviors()
{
return [
//...
[
'class' => SlugBehavior::class,
'attribute' => 'title',
'slugAttribute' => 'slug',
],
];
}
Configurations:, (*9)
You need a registration controller in your main config file in section controllerMap
:, (*10)
use sbs\controllers\TransliterationController;
//...
'controllerMap' => [
'transliteration' => [
'class' => TransliterationController::class,
'lowercase' => false //provides transliteration to lower case, true by default.
]
],
//...
Like a widget:, (*11)
use sbs\widgets\SlugInput;
echo SlugInput::widget([
'name' => 'News[slug]',
'sourceName' => 'News[title]'
]);
Like an ActiveForm widget:, (*12)
use sbs\widgets\SlugInput;
echo $form->field($model, 'slug')->widget(SlugInput::class, [
'sourceAttribute' => 'title'
]);
That's all. Enjoy., (*13)