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?