set locale automatically in ruby on rails [duplica

2020-02-10 05:14发布

how to set locale automatically on ruby on rails? For instance, if the web pages is opened up in Spain then the locale=es, similarly if it is in united kingdom then the locale=en and alike?

Please help me out.

5条回答
等我变得足够好
2楼-- · 2020-02-10 05:38

If i get what u mean, u will have to work with geolocation, get the user location based on his ip, and then set the locale based on it, but u must not forgot to set some default in case u can't get nothing of the user's ip.

Im sorry to say, but i didnt worked with geolocation on ruby yet. I did a fast research on rubygems and couldnt find any gem to simplify your work. =(

查看更多
欢心
3楼-- · 2020-02-10 05:42

From the Rails Guide, include this code in the ApplicationController

before_action :set_locale

def set_locale
  I18n.locale = extract_locale_from_tld || I18n.default_locale
end

# Get locale from top-level domain or return nil if such locale is not available
# You have to put something like:
#   127.0.0.1 application.com
#   127.0.0.1 application.it
#   127.0.0.1 application.pl
# in your /etc/hosts file to try this out locally
def extract_locale_from_tld
  parsed_locale = request.host.split('.').last
  I18n.available_locales.map(&:to_s).include?(parsed_locale) ? parsed_locale : nil
end

Essentially, this strips the final identifier (.en, .id, etc) from a url like application.co.id or example.com and sets the locale based on that. If a user comes from a locale you don't support, it returns to the default locale - English, unless set otherwise.

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-10 05:44

Include this is ApplicationController or BaseController

before_filter :set_locale

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

This means set locale to default or use locale from request, priority to the one sent in request. If you want to expand this to user, session, request you can do this.

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

More info here Similar question.

查看更多
放荡不羁爱自由
5楼-- · 2020-02-10 05:49

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
查看更多
Summer. ? 凉城
6楼-- · 2020-02-10 05:50

You can implement it like this in your ApplicationController:

class ApplicationController < ActionController::Base
  before_filter :set_locale

  def set_locale
    I18n.locale = extract_locale_from_headers
  end

  private

  def extract_locale_from_headers
    request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first.presence || 'en'
  end
end

It will "inspect" the received request, find the language of the client's browser and set it as your I18n locale.

Take a look at the RubyOnRails Guide about I18n for further instructions.


I highly recommend to have a set of supported locales and fallback to a default locale. Something like this:

ALLOWED_LOCALES = %w( fr en es ).freeze
DEFAULT_LOCALE = 'en'.freeze

def extract_locale_from_headers
  browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
  if ALLOWED_LOCALES.include?(browser_locale)
    browser_locale
  else
    DEFAULT_LOCALE
  end
end
查看更多
登录 后发表回答