spring icon indicating copy to clipboard operation
spring copied to clipboard

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

Open rafaelfranca opened this issue 10 years ago • 9 comments
trafficstars

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

rafaelfranca avatar May 28 '15 01:05 rafaelfranca

I could not reproduce this issue:

[2.2.2 Desktop]$ rails --version
Rails 4.2.1
[2.2.2 Desktop]$ rails new toy_app
Using --skip-bundle from /Users/rafaelfranca/.railsrc
      create
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/views/layouts/application.html.erb
      create  app/assets/images/.keep
      create  app/mailers/.keep
      create  app/models/.keep
      create  app/controllers/concerns/.keep
      create  app/models/concerns/.keep
      create  bin
      create  bin/bundle
      create  bin/rails
      create  bin/rake
      create  bin/setup
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/secrets.yml
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/assets.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/cookies_serializer.rb
      create  config/initializers/filter_parameter_logging.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/session_store.rb
      create  config/initializers/wrap_parameters.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  lib
      create  lib/tasks
      create  lib/tasks/.keep
      create  lib/assets
      create  lib/assets/.keep
      create  log
      create  log/.keep
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/robots.txt
      create  test/fixtures
      create  test/fixtures/.keep
      create  test/controllers
      create  test/controllers/.keep
      create  test/mailers
      create  test/mailers/.keep
      create  test/models
      create  test/models/.keep
      create  test/helpers
      create  test/helpers/.keep
      create  test/integration
      create  test/integration/.keep
      create  test/test_helper.rb
      create  tmp/cache
      create  tmp/cache/assets
      create  vendor/assets/javascripts
      create  vendor/assets/javascripts/.keep
      create  vendor/assets/stylesheets
      create  vendor/assets/stylesheets/.keep
[2.2.2 Desktop]$ bundle
Could not locate Gemfile or .bundle/ directory
[2.2.2 Desktop]$ cd toy_app/
/Users/rafaelfranca/Desktop/toy_app
[2.2.2 toy_app]$ bundle
Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.2
Using minitest 5.6.1
Using thread_safe 0.3.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile 0.6.2
Using mime-types 2.5
Using arel 6.0.0
Using debug_inspector 0.0.2
Using bundler 1.9.6
Using columnize 0.9.0
Using coffee-script-source 1.9.1.1
Using execjs 2.5.2
Using thor 0.19.1
Using multi_json 1.11.0
Using rdoc 4.2.0
Using sass 3.4.13
Using tilt 1.4.1
Using sqlite3 1.3.10
Using tzinfo 1.2.2
Using nokogiri 1.6.6.2
Using mail 2.6.3
Using binding_of_caller 0.7.2
Using byebug 4.0.5
Using coffee-script 2.4.1
Using uglifier 2.7.1
Using sdoc 0.4.1
Using activesupport 4.2.1
Installing spring 1.3.6
Using rails-deprecated_sanitizer 1.0.3
Using globalid 0.3.5
Using activemodel 4.2.1
Using jbuilder 2.2.13
Using rails-dom-testing 1.0.6
Using activejob 4.2.1
Using activerecord 4.2.1
Installing loofah 2.0.2
Using rails-html-sanitizer 1.0.2
Using actionview 4.2.1
Installing rack 1.6.1
Using rack-test 0.6.3
Using actionpack 4.2.1
Using actionmailer 4.2.1
Using railties 4.2.1
Using coffee-rails 4.1.0
Using jquery-rails 4.0.3
Using turbolinks 2.5.3
Installing sprockets 3.1.0
Installing sprockets-rails 2.3.1
Using rails 4.2.1
Using sass-rails 5.0.3
Installing web-console 2.1.2
Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
[2.2.2 toy_app]$ echo "fr:
>   hello: "Bonjour"" > config/locales/fr.yml
[2.2.2 toy_app]$ cat config/locales/
en.yml  fr.yml
[2.2.2 toy_app]$ cat config/locales/fr.yml
fr:
  hello: Bonjour
[2.2.2 toy_app]$ rails c
Loading development environment (Rails 4.2.1)
>> I18n.locale = :fr
=> :fr

I think you got this error because you added the locale file after you started the spring process for your environment, so when you add the file it is not loaded inside the spring process. When you change the application.rb the spring process is reloaded and your locale is now working.

rafaelfranca avatar May 28 '15 01:05 rafaelfranca

From @joelpresence on May 13, 2015 19:38

@rafaelfranca What do you mean by "you adde the locale file after you started the spring process"? How did I start the spring process? rails server was not running so what was running the spring process and how do I stop and restart it?

Thanks for your help!

rafaelfranca avatar May 28 '15 01:05 rafaelfranca

Any rails command starts springs. rails console, rails generate, rails server. You can stop the spring process with spring stop.

rafaelfranca avatar May 28 '15 01:05 rafaelfranca

From @joelpresence on May 13, 2015 20:1

@rafaelfranca thanks.

But when creating a new app, a developer will always create the locale files after starting the spring process because creating the app starts spring and people usually run rails server at least once after app generation to ensure that all is well.

So at best it seems like spring should periodically re-scan the locales directory. At worse, we should add a comment to the Rails Guides i18n doc about needing to restart spring every time a new locale file is added. I'd be happy to help.

What do you think?

Thanks again.

rafaelfranca avatar May 28 '15 01:05 rafaelfranca

Agree. I'll move this issue to spring.

On Wed, May 13, 2015, 17:01 joelpresence [email protected] wrote:

@rafaelfranca https://github.com/rafaelfranca thanks.

But when creating a new app, a developer will always create the locale files after starting the spring process because creating the app starts spring and people usually run rails server at least once after app generation to ensure that all is well.

So at best it seems like spring should periodically re-scan the locales directory. At worse, we should add a comment to the Rails Guides i18n doc about needing to restart spring every time a new locale file is added. I'd be happy to help.

What do you think?

Thanks again.

— Reply to this email directly or view it on GitHub https://github.com/rails/rails/issues/20144#issuecomment-101797103.

rafaelfranca avatar May 28 '15 01:05 rafaelfranca

From @joelpresence on May 13, 2015 20:46

Graçias!

rafaelfranca avatar May 28 '15 01:05 rafaelfranca

I ran into the very same problem of locales not being loaded and spent far too much time trying to follow the guidelines w/o success :-(

silverdr avatar Aug 10 '16 09:08 silverdr

@silverdr did you manage to fix the issues in the meantime (longshot)?

budmc29 avatar Dec 24 '18 23:12 budmc29

It seems like an active issue right now. My project uses rails-i18n gem for some basic translations (I use only day names from there) and rails console displays localized names wrong, but the webapp does it right...

hron84 avatar Sep 24 '19 18:09 hron84