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
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'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.
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"
I am creating a little website which is currently being translated into different languages. I use Rails' out-of-the-box translation: l18n. To change the localization, a parameter called locale must be given, e.g.: http://localhost:3000/?locale=nl.
In ApplicationController this parameter is saved into a session variable and used as localization. How can I check if the locale actually exists? Are there any built in functions, or do I need to add an exists: "true" to every localization file to check it?
Rails will default to "en" as the default locale in case if a locale doesn't exist. So to be nasty if I pass http://localhost:3000/?locale=de and that translation doesn't exist, 'en' will be used.
Have a look here http://guides.rubyonrails.org/i18n.html , especially the section "2.3 Setting and Passing the Locale"
#config/initializers/available_locales.rb
# Get loaded locales conveniently
module I18n
class << self
def available_locales; backend.available_locales; end
end
module Backend
class Simple
def available_locales; translations.keys.collect { |l| l.to_s }.sort; end
end
end
end
# You need to "force-initialize" loaded locales
I18n.backend.send(:init_translations)
AVAILABLE_LOCALES = I18n.backend.available_locales
RAILS_DEFAULT_LOGGER.debug "* Loaded locales: #{AVAILABLE_LOCALES.inspect}"
You can then wrap the constant for easy access in ApplicationController:
class ApplicationController < ActionController::Base
def available_locales; AVAILABLE_LOCALES; end
end
You can implement it like this in your ApplicationController:
before_filter :set_locale
def set_locale
I18n.locale = extract_locale_from_params
end
def extract_locale_from_params
parsed_locale = params[:locale]
(available_locales.include? parsed_locale) ? parsed_locale : nil
end
HTH