Skip to content

Rails console does NOT auto load all locales from config/locales #408

@rafaelfranca

Description

@rafaelfranca

From @joelpresence on May 13, 2015 18:53

According to the default auto-generated config/application.rb that comes from rails new toy_app in Rails 4.2.1 using Ruby 2.2.2p95 (for Heroku compatibility):

...
module ToyApp
  class Application < Rails::Application
    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
...
  end
end

all locales in config/locales are autoloaded. However, this is clearly not true and here are the repro steps:

1. Create a file config/locales/fr.yml as follows:
fr:
  hello: "Bonjour"
2. Run bundle exec console rails.
3. Execute the statement:
I18n.locale = :fr
4. Get the stack trace:
I18n::InvalidLocale: :fr is not a valid locale
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/i18n-0.7.0/lib/i18n.rb:284:in `enforce_available_locales!'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/i18n-0.7.0/lib/i18n/config.rb:13:in `locale='
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/i18n-0.7.0/lib/i18n.rb:35:in `locale='
    from (irb):1
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/joel/src/hartl/toy_app/bin/rails:8:in `<top (required)>'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/joel/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/joel/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/joel/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

Now, if you go to that default auto-generated application.rb that claims that all locales are auto-loaded and uncomment the line:

    # Uncomment this line
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

   # ... and change it to be
   config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]

then everything works:

shell> bundle exec rails console
rails console> I18n.locale = :fr
 => :fr 
rails console> I18n.t 'hello'
 => "Bonjour" 

So there are two or more bugs here:

  1. Either the autoloading is failing for the rails console.
  2. Or the comments in the auto-generated application.rb about all locales being auto-loaded are wrong and also the Rails Docs at Configuring the i18n module are also wrong.

This buggy behavior is very confusing for somebody first trying to internationalize a Rails app since the console is a great place to test translations and localizations.

Thanks.

Copied from original issue: rails/rails#20144

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions