Extends ruby String
class with urlize
method which converts string to friendly url.
It removes all characters that couldn't be used in url and replaces spaces/underscores with dashes.
In addition it transliterate string using I18n library.
Installation
gem install string-urlize
In Rails 3, add this to your Gemfile and run the bundle command.
gem "string-urlize"
Usage
'my cool string'.urlize # => my-cool-string
:foo_symbol.to_s.urlize # => foo-symbol
Examples:
string | url |
---|---|
Lorem ipsum dolor sit amet | lorem-ipsum-dolor-sit-amet |
CamelCase | camel-case |
a lot of spaces | a-lot-of-spaces |
special !@#$%^&*()<>,./?\ \| symbols | special-symbols |
underscored_string | underscored-string |
string with-dashes | string-with-dashes |
ÈÉÊË | eeee |
òóôõöø | oooooo |
Transliteration
Transliteration powered by I18n library and enabled by default. So you could provide locale to the urlize method:
'Jürgen'.urlize # => "jurgen"
'Jürgen'.urlize(:locale => :de) # => "juergen"
or disable transliteration at all:
'Jürgen'.urlize(:transliterate => false) # => 'jürgen' in ruby 1.9 and 'jrden' in ruby 1.8.7