Set Locale Automatically in Ruby on Rails

set locale automatically in ruby on rails

try using gem geocoder and i18n_data gem and have a before_filter to a method which does

def checklocale
I18n.locale = I18nData.country_code(request.location.country)
end

Rails does not permit changing locale

Did you add the new locale in application.rb ?

config.i18n.enforce_available_locales = false
config.i18n.available_locales = [:en, :es, :de]
config.i18n.default_locale = :de

You also have to create the files en.yml , es.yml and de.yml in your config/locale folder.

Rails: Setting locale appropriately to IP address

I18n.locale isn't remembered between requests by default - you'll need to implement that yourself by saving to it to the session. After I18n.locale is set, you can use:

session[:locale] = I18n.locale

And then to pull it back:

I18n.locale = params[:locale] || session[:locale] || I18n.default_locale
# explicit params take precedence
# otherwise use the remembered session value
# fallback to the default for new users

Sidenote: consider moving your location = request.location so that it doesn't always run. You're taking a small performance hit (and a geocoding service lookup) on every request - even if you're not using the data.


By way of example, here is one way you could do this:

def set_locale
# explicit param can always override existing setting
# otherwise, make sure to allow a user preference to override any automatic detection
# then detect by location, and header
# if all else fails, fall back to default
I18n.locale = params[:locale] || user_pref_locale || session[:locale] || location_detected_locale || header_detected_locale || I18n.default_locale

# save to session
session[:locale] = I18n.locale
end

# these could potentially do with a bit of tidying up
# remember to return `nil` to indicate no match

def user_pref_locale
return nil unless current_user && current_user.locale.present?
current_user.locale
end

def location_detected_locale
location = request.location
return nil unless location.present? && location.country_code.present? && I18n.available_locales.include?(location.country_code)
location.country_code.include?("-") ? location.country_code : location.country_code.downcase
end

def header_detected_locale
return nil unless (request.env["HTTP_ACCEPT_LANGUAGE"] || "en").scan(/^[a-z]{2}/).first.present? && I18n.available_locales.include?((request.env["HTTP_ACCEPT_LANGUAGE"] || "en").scan(/^[a-z]{2}/).first)
(request.env["HTTP_ACCEPT_LANGUAGE"] || "en").scan(/^[a-z]{2}/).first
end

Ruby on Rails + Devise + I18n: how to set locale?

Take a look at Devise Wiki https://github.com/plataformatec/devise/wiki/I18n
They have lots of YML file samples.

If you still wanna write your own, try using something like this in your I18n files

en:
devise:
sessions:
signed_in: 'Signed in successfully.'

More info on GitHub https://github.com/plataformatec/devise#i18n

Rails locale not persistent

I recommend you to read this Rails guides about I18n to understand how I18n works in Rails. It's obvious that change of I18n.locale is temporary, and will be reset per session.

According to your requirement, you can store the selected locale in session. Here is the sample code:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :set_locale

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

Then you can clear the session[:locale] when user signed out.

# app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
def sign_out
# ...
session.delete :locale
end
end

Ruby on rails: how to set up string translation inline (witnout changing config/locales/*.yml)

KISS.

message = case I18n.locale
when 'ru'
"Предупреждение: Ваш комментарий слишком длинный."
else # keeping 'en' as default in case there is some unexpected locale
"Warning: your note is too long."
end
raise WorkflowError, message

Not setting default locale in Rails 3.1.3

Add to application.rb this line config.i18n.locale = :ca and Rails will set default locale. It is working for me in production server, but I set :ru locale



Related Topics



Leave a reply



Submit