Andrew Burns
Andrew Burns

Reputation: 14509

Why doesn't rails honor my date formats in en.yml?

I want to have a custom format, :results so I can do a @obj.date.to_s(:results) call in my views.

If I put my custom format in an initializer then it works:

Time::DATE_FORMATS[:results] = "%D"

However if I try and put that in my en.yml file then it does not work (as in it will not format it as expected):

en:
  time:
    # See also config/initializers/time_formats.rb for lambda based configs
    formats:
      results: '%D'
  date:
    formats:
      default: '%m/%d/%Y"'
      results: '%D'

Everything I read suggests that this should work. What am I missing?

EDIT: This is basically a duplicate of Rails not picking up custom date & time formats in en.yml So I guess the question is how can I set the defaults in yml rather than an initialize (it is easier to manage). Is there a built in way, or do I need to manually load the yaml file and set the Time::DATE_FORMAT hash myself?

Upvotes: 2

Views: 514

Answers (1)

Ismael
Ismael

Reputation: 16730

The answer is in Rails not picking up custom date & time formats in en.yml

The second way you mentioned is using I18n (internationalization) which means to use it you hace to call it.

In your views you can do

<%= l @obj.date, format: :result %> 

Upvotes: 1

Related Questions