Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n defaulting to english in main app after upgrading Active Admin #434

Closed
Dagnan opened this issue Aug 31, 2011 · 32 comments
Closed

i18n defaulting to english in main app after upgrading Active Admin #434

Dagnan opened this issue Aug 31, 2011 · 32 comments

Comments

@Dagnan
Copy link

Dagnan commented Aug 31, 2011

Hi.

I upgraded Active Admin from 0.2.2 to 0.3.0, and now my main application defaults to English when looking for translations.

In my config/application.rb there is still:

config.i18n.default_locale = :fr
@pcreux
Copy link
Contributor

pcreux commented Sep 1, 2011

Well, this is interesting since active admin does not set the I18n.locale.

Can you check what I18n.locale is in your main application?

@Dagnan
Copy link
Author

Dagnan commented Sep 1, 2011

Hmm, its value is :en.
I18n.default_locale still gives :fr.

@Dagnan
Copy link
Author

Dagnan commented Sep 5, 2011

Problem is still here in the 0.3.1.

@jackdempsey
Copy link

I can confirm this. default_locale is still set to what I specify, but I18n.locale switches because of ActiveAdmin: https://gist.github.com/1196193

Will dig into it more.

I have a strange feeling it's a gem that it depends on doing the dirty deed. Maybe devise, though I'm not sure why (I use devise in the app already and it's never done this before).

Ok, I think this works as a quick hack:

34 config.i18n.default_locale = :'en-US'
35 config.i18n.locale = :'en-US'

or whatever you need. Just set the damn locale and be done with it!

@jackdempsey
Copy link

Worked on this a bunch more. Really hard to track down--it's almost like the first call to ActiveAdmin (in standard routes file) requires something to do with I18n, which sets the locale to :en, and thus the default_locale we're setting is ignored. Gotta sleep, eyes crossing at this point, hopefully others can shed some light on it as well.

@ghost ghost assigned pcreux Sep 6, 2011
@pcreux
Copy link
Contributor

pcreux commented Sep 6, 2011

To track down where the locale was being updated I did the following:

# In gem i18n.

module I18n
  class Config

    # Sets the current locale pseudo-globally, i.e. in the Thread.current hash.
    def locale=(locale)
      puts "locale=#{locale}"
      raise "money" rescue puts $!.stacktrace

      @locale = locale.to_sym rescue nil 
    end 

It looks like there is no call to I18n.locale= when loading active admin. Meh.

@jackdempsey
Copy link

@pcreux yep, I did that along the way. There IS a require of i18n, which will do the @@default_locale ||= :en, which locale is then based on. I thought when I commented that out things would be good again, but for some reason no. Definite meh.

@dawidw
Copy link

dawidw commented Sep 7, 2011

I have similar problem, I wrote about it here:

http://stackoverflow.com/questions/7193043/activeadmin-internationalization

Can anyone answer that?

@ysbaddaden
Copy link

I tracked this bug by raising an exception in I18n::Config#default_locale if @@default_locale isn't initialized.

~/.rvm/gems/ree-1.8.7-2011.03/gems/i18n-0.5.0/lib/i18n/config.rb:26:in `default_locale'
~/.rvm/gems/ree-1.8.7-2011.03/gems/i18n-0.5.0/lib/i18n/config.rb:6:in `locale'
~/.rvm/gems/ree-1.8.7-2011.03/gems/i18n-0.5.0/lib/i18n.rb:147:in `translate'
~/.rvm/gems/ree-1.8.7-2011.03/gems/i18n-0.5.0/lib/i18n.rb:164:in `translate!'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/resource/naming.rb:36:in `plural_resource_name'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/resource/menu.rb:24:in `menu_item_name'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/namespace.rb:198:in `register_with_menu'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/namespace.rb:51:in `register'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/application.rb:84:in `register'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin.rb:60:in `__send__'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin.rb:60:in `register'
~/work/ligos/project/app/admin/my_module.rb:1
~/.rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:235:in `load'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:235:in `load'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:225:in `load_dependency'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:593:in `new_constants_in'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:225:in `load_dependency'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activesupport-3.0.10/lib/active_support/dependencies.rb:235:in `load'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/application.rb:129:in `load!'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/application.rb:129:in `each'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/application.rb:129:in `load!'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin/application.rb:152:in `routes'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin.rb:63:in `__send__'
~/.rvm/gems/ree-1.8.7-2011.03/gems/activeadmin-0.3.1/lib/active_admin.rb:63:in `routes'
~/work/ligos/project/config/routes.rb:18

It looks like routes are initialized before the application actually is, thus leading ActiveAdmin to initialize itself with rails defaults instead of the application's defaults. Do you think this is fixable, somehow?

It looks some translations (like the menu) are done when ActiveAdmin loads, which is wrong (what happens if a user can choose the locale?). Translations must be done on display. It think that would actually fix the problem.

@Dagnan
Copy link
Author

Dagnan commented Sep 12, 2011

Removing the require i18n does not solve the problem (in the case of a Rails app of course), locale is still defaulting to english.

@rickclare
Copy link

+1
I am also experiencing this problem (Rails 3.1.1.rc1). Our default locale is set to "en-GB" (British English):

config.i18n.default_locale = :"en-GB"

After installing activeadmin I am finding that methods such as I18n.translate() are attempting to look in the :en scope, instead of the desired :en-GB scope. This is occurring in our production and test environments only.

(I'm guessing that, in our development environment, the config.cache_classes = false setting is helping the circumvent the problem).

@rickclare
Copy link

I think I've found a workaround for this: I've created a new file

config/initializers/001_i18n.rb ('001' filename prefix is an attempt to ensure this file gets loaded before active_admin.rb)

# The default locale is :en-GB and all translations from config/locales/*.rb,yml are auto loaded.
I18n.default_locale = :'en-GB'
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
I18n.reload!

@pi3r
Copy link

pi3r commented Sep 24, 2011

+1, same issue here.
My workaround is:

config.i18n.default_locale = :fr                                                                                                                                                                                             
I18n.locale = config.i18n.locale = config.i18n.default_locale

Ok it's ugly, but everything is working now! :)

@ysbaddaden
Copy link

The problem actually is located in resource_name and pural_resource_name methods of the ActiveAdmin::Resource::Naming module. These methods are used to determine the name of the resources and rely on model_name (from ActiveModel::Naming) to get translated names.

The problem is that resource_name and plural_resource_names are called when ActiveAdmin is initialized when routes are initialized and before rails is actually initialized. Thus the default locale has never been set but ActiveAdmin already called model_name which called I18n.t('activerecord.models.name.class_name') which eventually called I18n.default_locale which is set to the library default: en.

Even if rails initialized routes after it initialized the whole application, there is another side effect: you can't change the locale in production on a per-user basis, because resource names are cached here and there (eg: in Resource, Menu, etc).

The solution would be for resource_name and plural_resource_name to no longer rely on model_name and to add a human_name method instead, that would be called in views, when necessary and on demand (no cache, never).

I did that brute-force in a fork with a bunch of other I18n related things (I needed it ASAP, don't merge any of it as-is) : https://github.com/ysbaddaden/active_admin

@iain
Copy link

iain commented Sep 29, 2011

I've done the following workaround: I've made an initializer (config/initializers/i18n.rb) with the following:

I18n.locale = :nl # or whatever your default locale is
I18n.load_path += Dir[Rails.root.join("config/locales/**/*.yml")]
I18n.reload!

@dimitarvp
Copy link

Used same solution as @iain, subscribing for this issue in the hopes of removing the ugly override in the future.

@rolfb
Copy link
Contributor

rolfb commented Oct 18, 2011

This is very weird.

Did a session with ruby-debug, with a ApplicationController#index just printing the values of I18n.default_locale and I18n.locale.

Ruby-debug says I18n.default_locale and I18n.locale was "de" (which I had set it to).
ApplicationController says I18n.default_locale and I18n.locale was "en".

Any clues to why I18n called in the controller and I18n called the debugger is showing different results?

Hard bug to track down :)

@gabehesse
Copy link

Same solution as @iain. Hoping you fix it soon.

@ghost
Copy link

ghost commented Nov 16, 2011

+1
@lain, thanks for the workaround.

@Judit
Copy link

Judit commented Nov 18, 2011

I have a similar problem, but @iain workaround doesn't work for me, because I use a scope in the routes for I18n. I set the I18n.locale in the ApplicationController and it has no effects in the ActiveAdmin view elements translations (e.g. menu model name).
Did anybody fixed this issue with other workaround?

@namxam
Copy link

namxam commented Dec 8, 2011

+1 and trying the proposed workarounds.

@julioprotzek
Copy link

The problem happens only on Thin. On Webrick everything works fine.
@iain workaround works for me.

@pietervisser
Copy link

@iain, your workaround works for me as well, thanks!

@pcreux
Copy link
Contributor

pcreux commented Feb 18, 2012

I can reproduce this issue. Adding this to an initializer fixes it though:

I18n.locale = :nl # or whatever your default locale is
I18n.load_path += Dir[Rails.root.join("config/locales/**/*.yml")]
I18n.reload!

@mrbrdo
Copy link

mrbrdo commented May 14, 2012

Ran into this problem today... Still present :-/

@michalorman
Copy link

I have this problem still with activeadmin 0.4.4 and rails 3.2.3

@klebershimabuku
Copy link

+1

  • rails 3.2.3
  • activeadmin 0.4.4

@jpmckinney
Copy link
Contributor

Please try my pull request #1448 as I think it will fix this. You will need to copy your language's active_admin translations to config/locales/active_admin.LOCALE.yml, where LOCALE is your locale (es, fr, or other...).

@klebershimabuku
Copy link

I get this to work with an before_filter on my ApplicationController.rb file.

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

@jpmckinney
Copy link
Contributor

See the issues referenced from #1448. Non-English users shouldn't all have to write a before_filter just to get AA working in their language.

@jpmckinney
Copy link
Contributor

#1448 is now #1470.

@developer88
Copy link
Contributor

There are lots of similar issues in AA so i put it here. I fixed my problems with method set_locale as klebershimabuku wrote but i put the method not in ApplicationController because it is never called from there but in initializers/active_admin.rb in

ActiveAdmin::ResourceController.class_eval do

protected

Set ru locale by default for ActiveAdmin

def set_admin_locale
I18n.locale = :ru
end

end

@ghost
Copy link

ghost commented Apr 17, 2014

@pi3r thank you now works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests