Erik Lott
Erik Lott

Reputation: 697

Rails Cannot Parameterize Cyrillic Characters (Russian)

Our application automatically generates page URLs using a parameterized version of the page name. For example:

http://www.domainname.com/this-is-the-page-name

Simple. Works fine, except when the page name uses cyrillic characters, the parameterize method returns a blank string:

"Пробна галерия".parameterize

I've been digging online for how to deal with this, and the suggestions that I have found point towards transliteration techniques. Wondering if there is a simple straightforward way of dealing with this.

Upvotes: 2

Views: 1517

Answers (2)

prograils
prograils

Reputation: 2376

Try this:

def to_slug      
  # http://stackoverflow.com/questions/1302022/best-way-to-generate-slugs-human-readable-ids-in-rails

  #strip the string
   ret = self.strip

   #replace all whitespaces and underscores to dashes
   ret.gsub!(/[\s_]+/, '-')

   #replace all non alphanumeric, non dashes with empty string
   ret.gsub! /\s*[^A-Za-z0-9А-Яа-я\-]\s*/, ''

   #convert double dashes to single
   ret.gsub! /-+/,"-"

   #strip off leading/trailing dashes
   ret.gsub! /\A[-]+|[-]+\z/,""

   ret
end

Upvotes: 0

sfate
sfate

Reputation: 145

Try to use gsub:

irb> "Пробна галерия".gsub!(/\s/,'-')
  => "Пробна-галерия" 

Upvotes: 1

Related Questions