From 7db0b073fec6bc3e6f213b58c76e7f43fcc2ab97 Mon Sep 17 00:00:00 2001 From: David Celis Date: Sun, 29 Jul 2012 15:31:53 -0700 Subject: Make ActiveSupport::Inflector locale aware and multilingual MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Inflector is currently not very supportive of internationalized websites. If a user wants to singularize and/or pluralize words based on any locale other than English, they must define each case in locale files. Rather than create large locale files with mappings between singular and plural words, why not allow the Inflector to accept a locale? This patch makes ActiveSupport::Inflector locale aware and uses `:en`` unless otherwise specified. Users will still be provided a list of English (:en) inflections, but they may additionally define inflection rules for other locales. Each list is kept separately and permanently. There is no reason to limit users to one list of inflections: ActiveSupport::Inflector.inflections(:es) do |inflect| inflect.plural(/$/, 's') inflect.plural(/([^aeéiou])$/i, '\1es') inflect.plural(/([aeiou]s)$/i, '\1') inflect.plural(/z$/i, 'ces') inflect.plural(/á([sn])$/i, 'a\1es') inflect.plural(/é([sn])$/i, 'e\1es') inflect.plural(/í([sn])$/i, 'i\1es') inflect.plural(/ó([sn])$/i, 'o\1es') inflect.plural(/ú([sn])$/i, 'u\1es') inflect.singular(/s$/, '') inflect.singular(/es$/, '') inflect.irregular('el', 'los') end 'ley'.pluralize(:es) # => "leyes" 'ley'.pluralize(:en) # => "leys" 'avión'.pluralize(:es) # => "aviones" 'avión'.pluralize(:en) # => "avións" A multilingual Inflector should be of use to anybody that is tasked with internationalizing their Rails application. Signed-off-by: David Celis --- activesupport/test/inflector_test.rb | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 1f32e4ff92..cd91002147 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -354,6 +354,35 @@ class InflectorTest < ActiveSupport::TestCase RUBY end + def test_inflector_locality + ActiveSupport::Inflector.inflections(:es) do |inflect| + inflect.plural(/$/, 's') + inflect.plural(/z$/i, 'ces') + + inflect.singular(/s$/, '') + inflect.singular(/es$/, '') + + inflect.irregular('el', 'los') + end + + assert_equal('hijos', 'hijo'.pluralize(:es)) + assert_equal('luces', 'luz'.pluralize(:es)) + assert_equal('luzs', 'luz'.pluralize) + + assert_equal('sociedad', 'sociedades'.singularize(:es)) + assert_equal('sociedade', 'sociedades'.singularize) + + assert_equal('los', 'el'.pluralize(:es)) + assert_equal('els', 'el'.pluralize) + + ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear } + + assert ActiveSupport::Inflector.inflections(:es).plurals.empty? + assert ActiveSupport::Inflector.inflections(:es).singulars.empty? + assert !ActiveSupport::Inflector.inflections.plurals.empty? + assert !ActiveSupport::Inflector.inflections.singulars.empty? + end + def test_clear_all with_dup do ActiveSupport::Inflector.inflections do |inflect| @@ -467,7 +496,7 @@ class InflectorTest < ActiveSupport::TestCase # there are module functions that access ActiveSupport::Inflector.inflections, # so we need to replace the singleton itself. def with_dup - original = ActiveSupport::Inflector.inflections + original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__) ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original.dup) ensure ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original) -- cgit v1.2.3