Skip to content

Instantly share code, notes, and snippets.

@makshark
Created February 6, 2015 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makshark/52476a0bf853e99825fa to your computer and use it in GitHub Desktop.
Save makshark/52476a0bf853e99825fa to your computer and use it in GitHub Desktop.
class Language
AVAILABLE_LANGUAGE = ['en', 'ru']
LANGUAGE = {
DEFAULT: 'en',
RU: 'ru',
EN: 'en'
}
def t(key)
translation = Translation.where(key: key).first
translation["text_#{get_language}"] if translation
end
def set_language(language)
language = LANGUAGE[:DEFAULT] unless AVAILABLE_LANGUAGE.include?(language)
session[:language] = language
end
def self.get_language
unless session[:language].present?
language = get_country == 'UA' ? LANGUAGE[:RU] : LANGUAGE[:DEFAULT]
set_language language
end
session[:language]
end
def get_country
require 'geoip'
remote_ip = request.remote_ip
geo_ip_path = Rails.root.join('db', 'GeoIP.dat')
@geoip ||= GeoIP.new(geo_ip_path)
@geoip.country(remote_ip).country_code2 if remote_ip != '127.0.0.1'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment