Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

titleize missing in ActiveSupport::Multibyte::Chars

#2794

Hi!

I'm missing the method titleize in ActiveSupport::Multibyte::Chars.

Example:
"VarfÖr gick åke bakÅt".mb_chars.titleize.to_s Gives me:
"VarfÖr Gick åke BakÅt" When I expect:
"Varför Gick Åke Bakåt"

I solved it in my project with a simple monkey patch:

class ActiveSupport::Multibyte::Chars
  def titleize

self.split(/(\s)/).collect { |e| e.mb_chars.capitalize.to_s }.join($1)



end end

Reported by johan.rejeep (at gmail) · June 12th, 2009 @ 08:56 AM

State: resolved
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: none

Activity

  1. Manfred Stienstra
    Manfred Stienstra
    • State changed from new to open

    That's correct, none of the inflection methods are multibyte safe. Inflection methods were primarily written to juggle table and constant names and are used quite a lot in core code. I'm afraid Rails would slow down a lot if we would make them multibyte safe.

    However, I wouldn't mind accepting a patch with a ActiveSupport::Multibyte::Inflections module which can be included into Chars to add these methods.

    August 11th, 2009 @ 02:24 PM

  2. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  3. Norman Clarke
    Norman Clarke
    • Tag changed from encoding, string, utf to encoding, patch, string, utf

    The attached patch resolves the reported issue.

    Rather than create an Inflector module, however, I just added a #titleize method to Multibyte::Chars.

    This was for two reasons. One, there was already a capitalize method there, and this is conceptually similar.

    Two, none of the other methods in Inflector seem particularly useful in a multibyte context: what's the point of a separate classify or underscore method, when you can just do something like:

    Inflector.transliterate(string).tableize
    

    if you really need to get a table name from a string with multibyte chars. If we provide a method for this, it's not entirely clear to me how we should handle non-Roman strings that can't easily be approximated to ASCII, since for example, Ruby doesn't allow UTF-8 characters in constants. Also, pluralizing non-English strings will generally just lead to hilarity:

    "Männer".mb_chars.tableize # => "manners"
    

    That looks completely absurd to a German speaker. :-)

    May 28th, 2010 @ 08:52 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • Milestone cleared.
    • Assigned user set to José Valim

    June 15th, 2010 @ 10:58 PM

  5. Santiago Pastorino
  6. Repository
  7. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to

    October 15th, 2010 @ 11:01 PM