mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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>
This commit is contained in:
parent
091e6f791a
commit
273c770011
4 changed files with 38 additions and 26 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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 "
|
||||
|
|
|
@ -55,21 +55,22 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase
|
|||
assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence
|
||||
end
|
||||
|
||||
def test_to_sentence_with_connector
|
||||
assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:connector => 'and also')
|
||||
assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
|
||||
assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => nil)
|
||||
assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => ' ')
|
||||
assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
|
||||
def test_to_sentence_with_words_connector
|
||||
assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
|
||||
assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
|
||||
assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
|
||||
end
|
||||
|
||||
def test_to_sentence_with_skip_last_comma
|
||||
assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
|
||||
def test_to_sentence_with_last_word_connector
|
||||
assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
|
||||
assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
|
||||
assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
|
||||
assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' and ')
|
||||
end
|
||||
|
||||
def test_two_elements
|
||||
assert_equal "one and two", ['one', 'two'].to_sentence
|
||||
assert_equal "one two", ['one', 'two'].to_sentence(:connector => '')
|
||||
assert_equal "one two", ['one', 'two'].to_sentence(:two_words_connector => ' ')
|
||||
end
|
||||
|
||||
def test_one_element
|
||||
|
|
|
@ -71,19 +71,28 @@ class I18nTest < Test::Unit::TestCase
|
|||
assert_equal 'pm', I18n.translate(:'time.pm')
|
||||
end
|
||||
|
||||
def test_sentence_connector
|
||||
assert_equal 'and', I18n.translate(:'support.array.sentence_connector')
|
||||
def test_words_connector
|
||||
assert_equal ', ', I18n.translate(:'support.array.words_connector')
|
||||
end
|
||||
|
||||
def test_skip_last_comma
|
||||
assert_equal false, I18n.translate(:'support.array.skip_last_comma')
|
||||
def test_two_words_connector
|
||||
assert_equal ' and ', I18n.translate(:'support.array.two_words_connector')
|
||||
end
|
||||
|
||||
def test_last_word_connector
|
||||
assert_equal ', and ', I18n.translate(:'support.array.last_word_connector')
|
||||
end
|
||||
|
||||
def test_to_sentence
|
||||
default_two_words_connector = I18n.translate(:'support.array.two_words_connector')
|
||||
default_last_word_connector = I18n.translate(:'support.array.last_word_connector')
|
||||
assert_equal 'a, b, and c', %w[a b c].to_sentence
|
||||
I18n.backend.store_translations 'en', :support => { :array => { :skip_last_comma => true } }
|
||||
I18n.backend.store_translations 'en', :support => { :array => { :two_words_connector => ' & ' } }
|
||||
assert_equal 'a & b', %w[a b].to_sentence
|
||||
I18n.backend.store_translations 'en', :support => { :array => { :last_word_connector => ' and ' } }
|
||||
assert_equal 'a, b and c', %w[a b c].to_sentence
|
||||
ensure
|
||||
I18n.backend.store_translations 'en', :support => { :array => { :skip_last_comma => false } }
|
||||
I18n.backend.store_translations 'en', :support => { :array => { :two_words_connector => default_two_words_connector } }
|
||||
I18n.backend.store_translations 'en', :support => { :array => { :last_word_connector => default_last_word_connector } }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue