Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to use I18n.t call in an initializer file in Rails 3.1.1
    primarykey
    data
    text
    <p>I would like to use I18n.t in an initializer file in Rails 3.1.1.</p> <p>This question already exists for Rails 2 (http://stackoverflow.com/questions/3514536/unable-to-use-i18n-t-call-in-an-initializer-file). Supposedly, it was answered by a subsequent release. It appears to be an issue again in Rails 3.1.1.</p> <p><strong>The problem:</strong></p> <p>I have a custom time format called :dxl. I would like to format my Email.sent_at datetime as :dxl. </p> <p><strong>The solution:</strong></p> <p>In can add the format to en.yml (time.formats.dxl) and use it with:</p> <pre><code>I18n.localize(email.sent_at, :format =&gt; :dxl) </code></pre> <p>I also want to show the utc time. I do not see a way to use I18n for this, so I am using:</p> <pre><code>email.sent_at.utc.to_s(:dxl) </code></pre> <p><strong>The rub:</strong></p> <p>To support <code>to_s(format)</code>, I believe I need to assign <code>Time::DATE_FORMATS[:dxl]</code> in an initializer (e.g. config/initializers/time_formats.rb).</p> <p>I would rather not duplicate the strftime format string in both en.yml and the initializer.</p> <p>Unfortunately, it looks like I18n is not usable within an initializer. When I add this to config/initializers/time_formats.rb:</p> <pre><code>Time::DATE_FORMATS[:dxl] = I18n.translate('time.formats.dxl') </code></pre> <p>I get:</p> <pre><code>&gt; Time.now.utc.to_s(:dxl) =&gt; "translation missing: en.time.formats.dxl" </code></pre> <p>Is there a way to ensure I18n is ready (has read the en.yml file) in an initializer?</p> <p>I did verify that the translation does work correctly if I copy/paste the strftime format into the initializer.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload