Skip to content

Instantly share code, notes, and snippets.

@yosiyuki
Created October 12, 2012 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yosiyuki/3880116 to your computer and use it in GitHub Desktop.
Save yosiyuki/3880116 to your computer and use it in GitHub Desktop.
Do something with translating client's ip address to locale in Rails
class ApplicationController < ActionController::Base
before_filter :set_locale_from_remote_addr
private
def set_locale_from_remote_addr
return do_something(session[:locale] if session[:locale]
raise UnknownIPAddessError unless request.remote_addr
geoip = GeoIP.new(Rails.root + "db/GeoLiteCountry.dat").country(request.remote_addr)
raise UnsupportedIPAddressError unless geoip
do_something(geoip.country_code2.downcase.to_sym)
end
def do_something locale
# do something with client's locale
# for example, set I18n locale
I18n.locale = locale
# another example, to set header image by country
@header_image_filename = "header_image_#{locale}.png"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment