Created
December 9, 2012 13:48
-
-
Save vladcosorg/4244940 to your computer and use it in GitHub Desktop.
Symfony2 russian transliterator for Sluggable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stof_doctrine_extensions: | |
class: | |
sluggable: Your\Bundle\Misc\Misc\SluggableListener | |
default_locale: en_US | |
orm: | |
default: | |
tree: true | |
timestampable: true | |
sluggable: true | |
translatable: true | |
loggable: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Your\Bundle\Misc; | |
class SluggableListener extends \Gedmo\Sluggable\SluggableListener | |
{ | |
public function __construct(){ | |
$this->setTransliterator(array('\Your\Bundle\Misc\Transliterator', 'transliterate')); | |
} | |
protected function getNamespace() | |
{ | |
return parent::getNamespace(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Your\Bundle\Misc; | |
class Transliterator extends \Gedmo\Sluggable\Util\Urlizer | |
{ | |
private static $table = array( | |
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Ѓ' => 'G`', | |
'Ґ' => 'G`', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Є' => 'YE', | |
'Ж' => 'ZH', 'З' => 'Z', 'Ѕ' => 'Z', 'И' => 'I', 'Й' => 'Y', | |
'Ј' => 'J', 'І' => 'I', 'Ї' => 'YI', 'К' => 'K', 'Ќ' => 'K', | |
'Л' => 'L', 'Љ' => 'L', 'М' => 'M', 'Н' => 'N', 'Њ' => 'N', | |
'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', | |
'У' => 'U', 'Ў' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'TS', | |
'Ч' => 'CH', 'Џ' => 'DH', 'Ш' => 'SH', 'Щ' => 'SHH', 'Ъ' => '``', | |
'Ы' => 'YI', 'Ь' => '`', 'Э' => 'E`', 'Ю' => 'YU', 'Я' => 'YA', | |
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'ѓ' => 'g', | |
'ґ' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'є' => 'ye', | |
'ж' => 'zh', 'з' => 'z', 'ѕ' => 'z', 'и' => 'i', 'й' => 'y', | |
'ј' => 'j', 'і' => 'i', 'ї' => 'yi', 'к' => 'k', 'ќ' => 'k', | |
'л' => 'l', 'љ' => 'l', 'м' => 'm', 'н' => 'n', 'њ' => 'n', | |
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', | |
'у' => 'u', 'ў' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'ts', | |
'ч' => 'ch', 'џ' => 'dh', 'ш' => 'sh', 'щ' => 'shh', 'ь' => '', | |
'ы' => 'yi', 'ъ' => "'", 'э' => 'e`', 'ю' => 'yu', 'я' => 'ya' | |
); | |
public static function transliterate($text, $separator = '-') | |
{ | |
$text = strtr($text, self::$table); | |
if (preg_match('/[\x80-\xff]/', $text) && self::validUtf8($text)) { | |
$text = self::utf8ToAscii($text); | |
} | |
return self::urlize($text, $separator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a