Created
October 12, 2012 16:31
-
-
Save yosiyuki/3880116 to your computer and use it in GitHub Desktop.
Do something with translating client's ip address to locale in Rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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