Question

I am a bit confused as to how I can specify what datetime format (or specifically date format in this case) should be used to translate dates inside a localized string.

So far, I have this:

# config/locales/en.yml
en:
    date:
        formats:
            month: '%B %Y'
    texts:
        only_showing_between: "Only showing events between %{from} and %{to}."


# app/views/mymodel/index.html.erb
<%= content_tag :p, t('texts.only_showing_between', :format => :month, :from => @listing[:from], :to => @listing[:to]), :class => 'text-muted' %>

But it doesn't seem to apply the custom format. Any helpful pointers would be appreciated :)

Was it helpful?

Solution

I don't know a cleaner way but this should work:

t('texts.only_showing_between', :from => l(@listing[:from], :format => :month), :to => l(@listing[:to], :format => :month))

Take a look the localize method

Remember: you must change the "date" key for "time" (for example) in your .yml file if @listing[:from] and @listing[:to] are not date objects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top