aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/inflector.rb
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2008-09-29 17:36:09 +0200
committerMichael Koziarski <michael@koziarski.com>2008-10-07 21:01:38 +0200
commita4629e707d80a5769f7a71ca6ed9471015e14dc9 (patch)
tree2f8596d17599d67aeb30e5a90040e929bd1c80dc /activesupport/lib/active_support/inflector.rb
parentf2c10f27565318b5efbdc57fb608b2afe9ae445d (diff)
downloadrails-a4629e707d80a5769f7a71ca6ed9471015e14dc9.tar.gz
rails-a4629e707d80a5769f7a71ca6ed9471015e14dc9.tar.bz2
rails-a4629e707d80a5769f7a71ca6ed9471015e14dc9.zip
Extract transliteration code to a seperate method.
Use iconv by default, but only when the transliteration is well behaved. When it isn't, fallback to mb_chars
Diffstat (limited to 'activesupport/lib/active_support/inflector.rb')
-rw-r--r--activesupport/lib/active_support/inflector.rb19
1 files changed, 17 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index 89a93f4a5f..f1e7abf4aa 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,14 +259,28 @@ module ActiveSupport
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
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
+
+ # Replaces accented characters with their ascii equivalents.
+ def transliterate(string)
+ Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s
+ end
+
+ # The iconv transliteration code doesn't function correctly
+ # on some platforms, but it's very fast where it does function.
+ if "foo" != Inflector.transliterate("föö")
+ def transliterate(string)
+ string.mb_chars.normalize(:kd). # Decompose accented characters
+ gsub(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
+ end
+ 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.
#