to_sentence with I18n doesn't allow enough flexibility

Using OutputSafetyHelper#to_sentence with the locale option doesn’t allow for customizable connectors/conjunctions for a particular instance, the use of “and” is assumed and impossible to customize.

For the most part, “and” is the preferred usage (eg error_messages.to_sentence) but I feel like the method would be a lot more useful with a further option to change the I18n key.

Consider the example, trying to create a string like “Please attach your xls, xlsx or csv file”:

%{xls xlsx csv}.to_sentence => “xls, xlsx and csv”

Using support.array.last_word_connector or support.array.two_words_connector

%{xls xlsx csv}.to_sentence(locale: “en”) => “xls, xlsx and csv”

Not translated

%{xls xlsx csv}.to_sentence(two_words_connector: " or ", last_word_connector: " or ") => “xls, xlsx or csv”

One current “solution”

or_connector = I18n.t(“support.array.or_connecor”)

%{xls xlsx csv}.to_sentence(two_words_connector: " #{or_connector} ", last_word_connector: " #{or_connector} ") => “xls, xlsx or csv”

I’m proposing a slight extension to this could give something like:

%{xls xlsx csv}.to_sentence(locale: “en”, key: “support.array.or_connector”) => “xls, xlsx or csv”

and a minor change to the method as such:

if defined?(I18n)

i18n_key = options.fetch(i18n_key, ‘support.array’).to_sym

i18n_connectors = I18n.translate(i18n_key, locale: options[:locale], default: {})

default_connectors.merge!(i18n_connectors)

end

Thoughts? Opinions?

Hi,

Using OutputSafetyHelper#to_sentence
<https://github.com/rails/rails/blob/master/actionview%2Flib%2Faction_view%2Fhelpers%2Foutput_safety_helper.rb>
with the locale option doesn't allow for customizable
connectors/conjunctions for a particular instance, the use of "and" is
assumed and impossible to customize.

Why yes, just make something like this:

en:
  or_array:
    words_connector: ', '
    two_words_connector: ' or '
    last_word_connector: ', or '

and use it like this:

my_array.to_sentence(I18n.translate('or_array'))