From e26c6a2c61763cf1f1b50fa959a2b456d0cb39f4 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 29 Sep 2008 17:36:09 +0200 Subject: [PATCH] Add transliterate options which fallback when the faster one is broken --- activesupport/lib/active_support/inflector.rb | 20 ++++++++++++++++++-- 1 files changed, 18 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 89a93f4..265d6d1 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -1,4 +1,5 @@ require 'singleton' +require 'iconv' module ActiveSupport # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, @@ -258,13 +259,28 @@ module ActiveSupport # # => Donald E. Knuth def parameterize(string, sep = '-') re_sep = Regexp.escape(sep) - string.mb_chars.normalize(:kd). # Decompose accented characters - gsub(/[^\x00-\x7F]+/, ''). # Remove anything non-ASCII entirely (e.g. diacritics). + transliterate(string). gsub(/[^a-z0-9\-_\+]+/i, sep). # Turn unwanted chars into the separator. squeeze(sep). # No more than one of the separator in a row. gsub(/^#{re_sep}|#{re_sep}$/i, ''). # Remove leading/trailing separator. downcase end + + # Iconv is significantly faster than using normalize(:kd) + def iconv_transliterate(string) + Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s + end + + def mb_chars_transliterate(string) + string.mb_chars.normalize(:kd). # Decompose accented characters + gsub(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). + end + + if "foo" == Inflector.iconv_transliterate("föö") + alias_method :transliterate, :iconv_transliterate + else + alias_method :transliterate, :mb_chars_transliterate + end # Create the name of a table like Rails does for models to table names. This method # uses the +pluralize+ method on the last word in the string. -- 1.6.0.1