2

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.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Erik Lott
  • 697
  • 1
  • 9
  • 17

2 Answers2

1

Try to use gsub:

irb> "Пробна галерия".gsub!(/\s/,'-')
  => "Пробна-галерия" 
sfate
  • 155
  • 10
  • 1
    We don't have a great deal of experience with cyrillic characters, but I'd rather display a romanized version of the string if is possible, UNLESS someone can confirm that displaying cyrillic chars in the address bar is standard. Secondly, parameterize does a nice job of cleaning our output string, where gsub solution is too simple. – Erik Lott Jul 25 '11 at 16:54
  • Then try gem [russian](https://github.com/Undev/russian) >Overloaded method parameterize inflector ActiveSupport (String#parameterize). Now it's a romanization of the Russian letters. – sfate Jul 25 '11 at 17:16
0

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
prograils
  • 2,248
  • 1
  • 28
  • 45