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
Related
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
I have two locale files:
config/locales/en.yml
config/locales/es-cl.yml
I want to use "en" as default locale to translate to English language when "es-CL" translations are missing. So, this is my application.rb file:
config.i18n.available_locales = ['en', 'es-CL']
config.i18n.default_locale = 'en'
config.i18n.locale = 'es-CL'
On production.rb
config.i18n.fallbacks = true
But, when I start server, locales are on English language.
A chunk of my es-cl.yml (the translation is working when I set config.i18n.default_locale to "es-CL")
es-CL:
activerecord:
models:
admin_user:
one: Administrador
other: Administradores
producer:
one: Productora
other: Productoras
ticket:
one: Ticket
other: Tickets
The locale set in the config i.e config.locale = :es is not persistent. You will have to set the locale before request e.g in application_controller.
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || 'es'
end
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
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)
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