activeadmin + internationalization - ruby-on-rails-3

I've updated active_admin to version 0.3.0 to get internationalization working. But I have problems with it.
I have my pl.yml file updated with activeadmin section which looks like this:
pl:
active_admin:
blank_slate:
content: "Nie ma jeszcze rekordów."
link: "Nowy"
dashboard: "Dashboard2"
view: "Podgląd"
This didn't work, so I tried adding this code to my application.rb:
config.before_configuration do
I18n.locale = :pl
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*', '.{rb,yml}')]
I18n.reload!
end
Now internationalization seems to work in development environment, but I still have problems in other environments. I have problem with dashboard: key. Normally, in short, when I18n doesn't find the key it puts key: with capital letter, in this example it would be "Dashboard". But in my case i have something like this:
Develoment:
Production:
Is there anyone who had the same problem? I'm I doing something wrong, or is this an activeadmin bug? Any solution?

I had the same problem. I needed to do this to be able to get it to work in both production and development:
config.before_configuration do
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
I18n.locale = :nl
I18n.default_locale = :nl
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{rb,yml}').to_s]
config.i18n.locale = :nl
# bypasses rails bug with i18n in production\
I18n.reload!
config.i18n.reload!
end
config.i18n.locale = :nl
config.i18n.default_locale = :nl
Not very pretty, but probably caused by a bug in Rails.

in application.rb
config.i18n.default_locale = :fr
I18n.locale = config.i18n.locale = config.i18n.default_locale
I18n.reload!

The key reason maybe caused by : Rails chose the locale from enduser's browser, but not your config file. e.g. a Japanese is visiting your website with his browser using English , then your Rails app will show him the "English" text, but not Japanese that you want it to show.
According to Rails i18n document: http://guides.rubyonrails.org/i18n.html, you have to first of all:
edit config/application.rb to set the default_locale
config.i18n.default_locale = :cn
edit your app/controllers/application_controller.rb, to add a before_filter
before_filter :set_locale
# for those user whose browser is not using our default_locale, e.g. a Chinese using English broser,
# just like me. :)
def set_locale
I18n.locale = params[:local] || I18n.default_locale
end
in this case, you should have the "cn" as the default locale.
check your view page, by adding these line of code to any of your page. e.g.
# in products/index.html.erb
<h1>Products List</h1>
default_locale is: <%= I18n.default_locale %> <br/>
current_locale is: <%= I18n.locale %>
the output result should look like:
Products List
default_locale is: cn
current_locale is: cn
and your Rails application should work as you expect.

An alternative that seems to work is creating an initializer with the following:
# config/initializers/i18n_reload.rb
Rails.configuration.after_initialize do
I18n.reload!
end

Related

How can define default locale ? Rails 5.1

I try to config my default locale to fr........ but it dont work.
In my application.rb :
config.load_defaults 5.1
config.i18n.locale = :fr
config.i18n.default_locale = :fr
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
but I always have my app with params "locale"=>"en" where is the mistake ?
You need to set default_locale as you have, and also prsent available locales:
config.i18n.default_locale = :fr
I18n.available_locales = [:fr]
in your application.rb

Rails 3: production environment change locale but doesn't translate text [w/ custom fallbacks]

I have this app in which I use a bunch of locales (which are adjusted to be more suited to the domain of the app, ex: instead of using es-MX, I just use mx as locale)
And I have configured the fallbacks in application.rb
config.i18n.default_locale = :en
config.i18n.fallbacks = {
# sites
'cl' => 'es',
'mx' => 'es',
'lat' => 'es',
'br' => 'en',
'us' => 'en',
# lang
'es' => 'en',
'pt' => 'br',
}
And I set the locale by url ex: localhost:3001/cl (for chilean locale)
here's my code in app_controller
before_filter :set_locale
private
def set_locale
if supported_locale?(params[:locale])
I18n.locale = params[:locale]
end
end
And my routes
# public urls for sites
scope '/:locale' do
# index
match '/' => 'main#index', via: :get, as: :site
end
So, the thing is when I am in production I have localhost:3001/cl
and it calls the _logo.cl.html.erb partial and the locale printed in the console it's cl.
But the text are still in english. And in development everything works fine. Anyone has any idea about this?
I'll leave a couple of images
production/us
production/cl
development/cl
The thing was that the production.rb file define
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
overwriting the custom fallback rules that I had defined in application.rb and I just remove those lines and the problem was solved

rails-translate-routes gem: Is it possible to translate routes but keep (some) original ones?

I'm using rails-translate-routes gem to translate "front" routes only.
I'm using carrierwave to upload some files in my admin. Here's an uploader:
class CheatsheetUploader < CarrierWave::Uploader::Base
[...]
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
[...]
end
As you see, the path is using the name of the model and the name of the field.
When I try to get the file:
link_to "my file", download.cheatsheet.url
The path is the default one:
http://localhost:3000/uploads/download/cheatsheet/1/a_really_nice_file.pdf
And Rails give me a nice Routing error:
No route matches [GET] "/uploads/download/cheatsheet/1/a_really_nice_file.pdf"
Any way to handle this?
You can pass keep_untranslated_routes: true in your call to ActionDispatch::Routing::Translator.translate_from_file.
For example:
ActionDispatch::Routing::Translator.translate_from_file(
'config/locales/routes.yml',
no_prefixes: true,
keep_untranslated_routes: true)

rails 3 i18n set locale does not work

i would like translations to fallback to :en, but the default locale is :de. how can i achieve this best?
i tried in 'config/application.rb'
config.i18n.default_locale = :en
config.i18n.locale = :de
but I18n.locale is still :en after this.
any ideas?
If you set locale like the following line
I18n.locale = :de
Then after the line triggered, EVERY visitor will using de locale, not the default locale en.
So the better way is
In your application_controller.rb
before_filter: set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
Reply to Jonathan Clark:
Every controller inherited from application_controller will set the locale.
You cant set the locale in your environments.
you have to put default_locale in your environment config file and you have to set your locale in your application itself.
For example in your application_controller.rb
before_filter: set_locale
private
def set_locale
I18n.locale = params[:locale] if params[:locale]
end

image_tag in mailer not using asset_host

image_tag isn't using the asset_host I've set. Any ideas why? The only thing I can think of is it having to do with it being a Mailer.
config/environment/development.rb
config.action_controller.asset_host = "http://localhost:3000"
myMailer.rb
<%= image_tag "logo.png", :style=>"margin-left:10px; padding-bottom:15px;" %>
rendered as:
<img alt="Logo" src="/images/logo.png?1303090162" style="margin-left:10px; padding-bottom:15px;" />
In console:
> MyApp::Application.config.action_controller
#<OrderedHash {… :asset_host=>"http://localhost:3000", …}>
I need the image_tag to create a full path url because it will be showing up in an email.
I was wrong before. This is the solution you need (until rails 3.1 where the asset_host configurations become unified):
config.action_mailer.asset_host = "http://localhost:3000"
We need to specify both config.action_controller.asset_host and config.action_mailer.asset_host, on Rails 3.1 and 3.2.
To add the hostname to the image_tag on both e-mail and non-email views, add the following to your environment file:
config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host
Where 'http://localhost:3000' should be replaced by your host URL (and port if applicable).
This needs to be set on both action_controller and action_mailer, even in Rails 3.2.x.
The offending code as to why you can't do it is here:
# actionpack/lib/action_view/helpers/asset_paths.rb, line 27
def compute_public_path(source, dir, ext = nil, include_host = true)
# More code up here....
if controller && include_host
has_request = controller.respond_to?(:request)
source = rewrite_host_and_protocol(source, has_request)
end
end
Here is the offending file on GH: https://github.com/rails/rails/blob/master/actionpack/lib/action_view/helpers/asset_paths.rb
Since an ActionMailer View template lacks a Controller, you don't get the command to rewrite based on an asset_host. This should probably be a ticket opened to the Rails core team.
You can try the following config and see if it helps:
config.action_mailer.default_url_options = {:host=>"localhost", :port=>3000, :protocol=>"http://"}
I'm pretty sure it's only going to work for url_for though.