Doctrine Sluggable and translit

Doctrine Sluggable behaviour doesn’t work with non-ASCII characters – it just removes them from the slug. For some languages, e.g. Russian with Cyrillic alphabet, it becomes unusable. The obvious way is to use transliteration.
Luckily Doctrine has easy way to do that, you can pass method for building slug:

      fields: [name]
      builder: [oxtSluggableTranslit, urlize]

The easiest way I found to transliterate non-ASCII text is to use PECL translit package. For Debian-based systems you can install it with dh-make-php.
Here’s the code:

class oxtSluggableTranslit
   * Convert any passed string to a url friendly string. Converts 'My first blog post' to 'my-first-blog-post'
   * @param  string $text  Text to urlize
   * @return string $text  Urlized text
  static public function urlize($text)
    return Doctrine_Inflector::urlize(transliterate($text, array('cyrillic_transliterate', 'remove_punctuation'), 'utf-8', 'utf-8'));

You can view all available transliterate filters by running

php -r 'print_r(transliterate_filters_get());'

Monday, February 1st, 2010