This is a Ruby gem that builds slugs from Strings.
It is similar to StringEx' String#to_url
, but more lightweight.
These are the main things that SexySlug does differently:
- depends on activesupport
- leaves core classes untouched
- makes a few more transformations (see specs)
- does not transliterate non-latin scripts (why?)
- provides no ActiveRecord mixin
- is less customizable
- is about five times faster (see benchmark)
gem install sexy_slug
or add it to your Gemfile.
SexySlug.from('Hi there!') # => 'hi-there'
SexySlug.from('Mambo #5') # => 'mambo-number-five'
I18n.with_locale(:de) { SexySlug.from('Mambo #5') } # => 'mambo-nummer-fuenf'
Simply change the contents of the SexySlug::PROCESSORS
Array.
SexySlug::PROCESSORS.delete(SexySlug::UsuallyTransliterableChar)
SexySlug::PROCESSORS.unshift(MyCustomProcessor)
Sluggification is always a fuzzy business, but transliteration in particular is almost guaranteed to produce incorrect results.
In many languages, codepoints don't map one-to-one to pronunciations, so their correct transliteration is context-dependent. Some relevant issues are e.g. allophones, crasis, digraphs, sandhi, and shared scripts.
Despite some restraint, sexy_slug
isn't completely "i18n-proof", e.g.:
- it might mistranslate money amounts, as the dollar sign is also used for non-dollar currencies
- it always transliterates umlauts, which isn't appropriate for languages such as Turkish
See customization on how to avoid this if needed.
Feel free to send suggestions, point out issues, or submit pull requests.