Note that there are some explanatory texts on larger screens.

plurals
  1. POMixed locales in Rails i18n
    text
    copied!<p>Rails somehow mixes my locales an I have absolutely no clue why. Most of my translated strings work as expected, but for some it mixes the locales.</p> <p>Interestingly, this only happens on one of our systems. Specifically running Passenger with Apache.</p> <p>When using Webrick, Thin, or Passenger Standalone on my development system everything is alright.</p> <p>This is what I have in my <code>application.rb</code>:</p> <pre><code>config.i18n.default_locale = :de </code></pre> <p>This is in <code>application_controller.rb</code>:</p> <pre><code>before_filter :set_locale def set_locale I18n.locale = @current_client ? @current_client.locale : I18n.default_locale end </code></pre> <p>(I experience the problems on pages where <code>@current_client</code> is <code>nil</code> and the <code>else</code> part gets executed).</p> <p>So, I am basically using the <code>:de</code> locale. When showing a validation error on a form, I experience mixed up translations like this:</p> <blockquote> <p>ist zu kurz (nicht weniger als 6 Zeichen) und translation missing: en.activerecord.errors.custom.password_format</p> </blockquote> <p>As you can see, the error message from the first failing validation is translated as expected, for the second error message tries to access the English translation (which does not exist). </p> <p>I suspect a problem with lazy loading of translated strings even before the <code>before_filter</code> gets executed.</p> <p>Any clues why this might happen?</p> <p>For the record: This is Rails 3</p> <p><strong>EDIT</strong>:</p> <p>I just discovered that this depends on the environment used. When using the development environment, everything is fine. When using the production environment (or a production-like) environment, I experience the behavior described above.</p> <p><strong>EDIT 2</strong>:</p> <p>I found out even more: It specifically depends on <code>config.cache_classes</code>. When set to <code>true</code>, I see the mixed translations. When set to <code>false</code> (as in the typical development environment), i18n works as expected.</p> <p><strong>EDIT 3</strong>:</p> <p>Maybe this is related to the following bug?</p> <p><a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/5522" rel="noreferrer">https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/5522</a> </p> <p><strong>Edit 4</strong>:</p> <p>This IS related to the bug mentioned above, the problem is due to eagerly loaded model classes, which use I18n strings, but eager class loading happens before I18n initialization, hence the translations are not found. There even is another bug about this:</p> <p><a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6353" rel="noreferrer">https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/6353</a></p> <p>Unfortunately, the Rails guys did not manage to include the fix in the recent 3.0.4 release (as far as I can tell). Hence I'm trying to figure out a workaround like this (in my application configuration):</p> <pre><code> config.before_eager_load do I18n.load_path += Dir[Rails.root.join('config', 'locales', 'de.yml').to_s] I18n::Railtie.reloader.paths.concat I18n.load_path I18n::Railtie.reloader.execute_if_updated I18n.reload! end </code></pre> <p>Unlucky, this does not work. Any clues?</p>
 

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