aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2008-11-18 02:16:08 +0900
committerJeremy Kemper <jeremy@bitsweat.net>2008-12-08 15:53:20 -0800
commit273c770011ab43d4b523caacb808bce070922d77 (patch)
treed10dd1a4af6127ad824eac36e5786cda32f4e431 /activesupport/lib/active_support
parent091e6f791aa3324b2c7f8c8c4cd2fce12b689cc8 (diff)
downloadrails-273c770011ab43d4b523caacb808bce070922d77.tar.gz
rails-273c770011ab43d4b523caacb808bce070922d77.tar.bz2
rails-273c770011ab43d4b523caacb808bce070922d77.zip
Change Array#to_sentence I18n options to pass comma and space character from outside.
[#1397 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb19
-rw-r--r--activesupport/lib/active_support/locale/en.yml5
2 files changed, 13 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
index f0d6591135..69d35dafd3 100644
--- a/activesupport/lib/active_support/core_ext/array/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -3,15 +3,16 @@ module ActiveSupport #:nodoc:
module Array #:nodoc:
module Conversions
# Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options:
- # * <tt>:connector</tt> - The word used to join the last element in arrays with two or more elements (default: "and")
- # * <tt>:skip_last_comma</tt> - Set to true to return "a, b and c" instead of "a, b, and c".
+ # * <tt>:words_connector</tt> - The sign or word used to join the elements in arrays with two or more elements (default: ", ")
+ # * <tt>:two_words_connector</tt> - The sign or word used to join the elements in arrays with two elements (default: " and ")
+ # * <tt>:last_word_connector</tt> - The sign or word used to join the last element in arrays with three or more elements (default: ", and ")
def to_sentence(options = {})
- options.assert_valid_keys(:connector, :skip_last_comma, :locale)
+ options.assert_valid_keys(:words_connector, :two_words_connector, :last_word_connector, :locale)
- default = I18n.translate(:'support.array.sentence_connector', :locale => options[:locale])
- 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 == ''
+ default_words_connector = I18n.translate(:'support.array.words_connector', :locale => options[:locale])
+ default_two_words_connector = I18n.translate(:'support.array.two_words_connector', :locale => options[:locale])
+ default_last_word_connector = I18n.translate(:'support.array.last_word_connector', :locale => options[:locale])
+ options.reverse_merge! :words_connector => default_words_connector, :two_words_connector => default_two_words_connector, :last_word_connector => default_last_word_connector
case length
when 0
@@ -19,9 +20,9 @@ module ActiveSupport #:nodoc:
when 1
self[0].to_s
when 2
- "#{self[0]} #{options[:connector]}#{self[1]}"
+ "#{self[0]}#{options[:two_words_connector]}#{self[1]}"
else
- "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}"
+ "#{self[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{self[-1]}"
end
end
diff --git a/activesupport/lib/active_support/locale/en.yml b/activesupport/lib/active_support/locale/en.yml
index 92132cacd5..e604c9ee8c 100644
--- a/activesupport/lib/active_support/locale/en.yml
+++ b/activesupport/lib/active_support/locale/en.yml
@@ -28,5 +28,6 @@ en:
# Used in array.to_sentence.
support:
array:
- sentence_connector: "and"
- skip_last_comma: false
+ words_connector: ", "
+ two_words_connector: " and "
+ last_word_connector: ", and "