to_sentence with I18n doesn't allow enough flexibility

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

to_sentence with I18n doesn't allow enough flexibility

Mark Edmondson
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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: to_sentence with I18n doesn't allow enough flexibility

nanaya-2
Hi,

On Mon, May 1, 2017, at 19:06, Mark Edmondson wrote:
> 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'))

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.