Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 3fa9fdd93de1eef83653b02cd4ac8201654543c5 Mon Sep 17 00:00:00 2001
- From: =?utf-8?q?Tarmo=20T=C3=A4nav?= <tarmo@itech.ee>
- Date: Tue, 7 Oct 2008 14:23:05 +0300
- Subject: [PATCH] Made i18n simple backend able to store false values (and not confuse them with nil or lack of value)
- Implemented support.array.skip_last_comma i18n key for
- Array#to_sentence, this also tests the ability to store false.
- ---
- .../active_support/core_ext/array/conversions.rb | 3 ++-
- activesupport/lib/active_support/locale/en-US.yml | 1 +
- .../vendor/i18n-0.0.1/i18n/backend/simple.rb | 16 ++++++++++++++--
- activesupport/test/i18n_test.rb | 16 ++++++++++++++++
- 4 files changed, 33 insertions(+), 3 deletions(-)
- diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
- index e67b719..11c128d 100644
- --- a/activesupport/lib/active_support/core_ext/array/conversions.rb
- +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
- @@ -11,7 +11,8 @@ module ActiveSupport #:nodoc:
- options.assert_valid_keys(:connector, :skip_last_comma, :locale)
- default = I18n.translate(:'support.array.sentence_connector', :locale => options[:locale])
- - options.reverse_merge! :connector => default, :skip_last_comma => false
- + default_skip_last_comma = I18n.translate(:'support.array.skip_last_comma', :locale => options[:locale])
- + options.reverse_merge! :connector => default, :skip_last_comma => default_skip_last_comma
- options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == ''
- case length
- diff --git a/activesupport/lib/active_support/locale/en-US.yml b/activesupport/lib/active_support/locale/en-US.yml
- index 60ecb1d..c31694b 100644
- --- a/activesupport/lib/active_support/locale/en-US.yml
- +++ b/activesupport/lib/active_support/locale/en-US.yml
- @@ -29,3 +29,4 @@ en-US:
- support:
- array:
- sentence_connector: "and"
- + skip_last_comma: false
- diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
- index 2dbaf8a..30e3655 100644
- --- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
- +++ b/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
- @@ -30,7 +30,13 @@ module I18n
- options.delete(:default)
- values = options.reject{|name, value| reserved.include? name }
- - entry = lookup(locale, key, scope) || default(locale, default, options) || raise(I18n::MissingTranslationData.new(locale, key, options))
- + entry = lookup(locale, key, scope)
- + if entry.nil?
- + entry = default(locale, default, options)
- + if entry.nil?
- + raise(I18n::MissingTranslationData.new(locale, key, options))
- + end
- + end
- entry = pluralize locale, entry, count
- entry = interpolate locale, entry, values
- entry
- @@ -83,7 +89,13 @@ module I18n
- return unless key
- init_translations unless initialized?
- keys = I18n.send :normalize_translation_keys, locale, key, scope
- - keys.inject(translations){|result, k| result[k.to_sym] or return nil }
- + keys.inject(translations) do |result, k|
- + if (x = result[k.to_sym]).nil?
- + return nil
- + else
- + x
- + end
- + end
- end
- # Evaluates a default translation.
- diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb
- index 4b17e3c..db5bd5e 100644
- --- a/activesupport/test/i18n_test.rb
- +++ b/activesupport/test/i18n_test.rb
- @@ -72,4 +72,20 @@ class I18nTest < Test::Unit::TestCase
- def test_time_pm
- assert_equal 'pm', I18n.translate(:'time.pm')
- end
- +
- + def test_sentence_connector
- + assert_equal 'and', I18n.translate(:'support.array.sentence_connector')
- + end
- +
- + def test_skip_last_comma
- + assert_equal false, I18n.translate(:'support.array.skip_last_comma')
- + end
- +
- + def test_to_sentence
- + assert_equal 'a, b, and c', %w[a b c].to_sentence
- + I18n.backend.store_translations 'en-US', :support => { :array => { :skip_last_comma => true } }
- + assert_equal 'a, b and c', %w[a b c].to_sentence
- + ensure
- + I18n.backend.store_translations 'en-US', :support => { :array => { :skip_last_comma => false } }
- + end
- end
- --
- 1.6.0.2
Add Comment
Please, Sign In to add comment