I've installed rails_admin gem on my localized site (2 languages) and i need administration (/admin) to be always in English. According to documentation I should add following 2 lines to beginning of rails_admin.rb file.
require 'i18n'
I18n.default_locale = :de
But it's not working. Any idea how to do that?
Stumbled over the same Issue. Here is how I solved it:
class ApplicationController < ActionController::Base
include Clearance::Controller
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :set_locale
def set_locale
if [Clearance, RailsAdmin].include?(self.class.parent)
I18n.locale = :en
else
I18n.locale = params[:locale] || I18n.default_locale
end
end
end
RailsAdmin controllers are inheriting form your ApplicationController so you need to tell them explicitly to use the locale :en there or you can open the classes and overwrite set_locale.
It does state on the documentation that you only need to do this if your local is set to something other then English so you may find that you do not need to set this. If you do need to set this then make sure it is below the RailsAdmin.config do |config| line in rails_admin.rb
Update -
As you are still running into problems could you please let me know what version of ruby you are using? Have you run a bundle install? Could you try sudo gem install i18n. Also if it cannot find your locales you may need to point it at them i.e I18n.load_translations "#{RAILS_ROOT}/locales/#{locale}.rb"
Related
Currently I use this code as suggested by the Rails guide:
# application_controller.rb
around_action :switch_locale
def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end
This is thread safe and works perfectly for the application... but not for Devise! Devise keeps using the default language (even if the translated YML files are present).
What should I do in order to tell Devise the language for the current user?
I know that there is I18n.locale but it is not thread safe...
I followed the great Rails guide on engines (https://guides.rubyonrails.org/engines.html) and managed to get my engine working in a host app.
However, the engine is loading only the en.yml locale, even tough the host application has I18n.default_locale and I18n.locale both set to pt-br, and I have a config/locales/pt-br.yml file in my engine.
The guide only states that "For locales, simply place the locale files in the config/locales directory, just like you would in an application.", but apparently I need to do something else.
How would I make the engine to load the correct I18n locale files, based on the host app settings? If that's not possible, how the host app could set this locale in an initializer file, like a config option?
I managed to solve it. The key is to remember that I18n.locale is a setting that should be set on every request.
So on my engine entry point (lib/cron_monitor.rb), I defined a setting:
module CronMonitor
class << self
attr_accessor :i18n
end
self.i18n = I18n.default_locale
Then in my engine's ApplicationController, following Rails i18n documentation:
module CronMonitor
class ApplicationController < ActionController::Base
around_action :switch_locale
private
def switch_locale(&action)
locale = CronMonitor.i18n || I18n.default_locale
I18n.with_locale(locale, &action)
end
end
end
Also, if you have code in models or service objects that you also want to be translated, you should use I18n as well. Example:
module CronMonitor
class Category < ApplicationRecord
def self.some_method
message = I18n.translate(
"cron_monitor.starting_or_finishing.failure",
title: self.title,
locale: CronMonitor.i18n
)
end
end
I want to specify locale for specific path.
in my whole application I have my default local e set to :es
config.i18n.default_locale = :es
That works in the normal fashion for the whole application, I want to know how can I specify a specific locale for a set of paths...
I integrated a third party application called maily_herald, in order to make it's paths available for my application i have to add this line to my routes.rb file
mount MailyHerald::Webui::Engine => "/maily_webui"
the problem is that there are not translations for locale :es, so it does not work properly... I want to let all the paths under /maily_webui to have locale :en so the third party application will work in english while my application will still work in :es
any idea how to do this?
class ApplicationController < ActionController::Base
.....
before_filter :set_locale
def set_locale
if request.fullpath == "some_path"
I18n.locale = :some_locale
else
I18n.locale = params[:locale] || I18n.default_locale
end
end
end
I've spent the last few hours trying everything possible to make this work, googled, redesigned, tested, etc. - but somehow doesn't get it working.
ok, I would like to set the I18n.locale - fairly simple.
Basically I implemented what is written on the guide here:
http://guides.rubyonrails.org/i18n.html
Application_controller.rb
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
In fact mine looks like this:
class ApplicationController < ActionController::Base
logger.info "Point A..."
before_action :set_locale
def set_locale
logger.info "Point B..."
I18n.locale = extract_locale_from_accept_language_header
end
private
def extract_locale_from_accept_language_header
....
This Problem is that the locale gets set before I can set the locale, when first accessing the page. I would like to read browser settings of user first, if no locale is yet set. To give the user a chance to get to their localized site. If they change the locale subsequently (change language - fine, then the locale is set and that's what they are using until they decide to switch again).
I placed two lines in the code above and this is the output:
Point A...
Processing by StaticPagesController#home as HTML
Parameters: {"locale"=>"en"}
Point B...
I tried the following:
- removed default locale in application.rb: #config.i18n.default_locale = :en
- disabled all gems that could possible interfere (restarted the server each time)
Any help would be appreciated. Thanks, G
Could you try and see if this works for you?
def set_locale
#locale = extract_locale_from_accept_language_header
I18n.locale = #locale
end
UPDATE:
After a lot of comments and the logs you paste it looks like you are getting the locale via routes. This is why you see in the log locale: en . But what you actually want to do is different, set the locale based on the headers and not the url. Remove the locale references from routes files and see if that works for you.
We have an CMS application which allows to create shops from a backend for multiple users.. now when we want to implement translations we face a problem. Translations are just stored with a locale but what we need would be a user.id and a locale. Because each user can have his own translations per language.
I tried to implement a i18n Backend as described in this railscast but am now stucking with
http://railscasts.com/episodes/256-i18n-backends?view=asciicast
the customization per user.
is there a way to add an additional column into the translations and ask for local and user_id?
many thanks
There is not a single best solution for your given problem. It depends on your application. Given the information I have I'd suggest something like:
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :set_locale
private
def set_locale
I18n.locale = current_user.language || I18n.default_locale
end
end
This would set the 18n.locale to the language settings of your current_user. Obviously you need a language attribute in your users model for this to work.
Please have a look at http://xyzpub.com/en/ruby-on-rails/4.0/i18n.html for more examples.