module Grass::Helpers::I18nHelper
Constants
- DEV_IP
- LNG_EXP_HTTP
- LNG_EXP_PATH
Public Instance Methods
browser_locale()
click to toggle source
Extract locale from accept language header
# File lib/grass/helpers/i18n_helper.rb, line 56 def browser_locale env['HTTP_ACCEPT_LANGUAGE'].scan(LNG_EXP_HTTP).first.to_sym unless env['HTTP_ACCEPT_LANGUAGE'].nil? end
country_info()
click to toggle source
Detailed country info
# File lib/grass/helpers/i18n_helper.rb, line 70 def country_info begin config['ip_country'].info(remote_ip) rescue nil end end
country_locales()
click to toggle source
Get all spoken languages in a country sorted by speakers count
# File lib/grass/helpers/i18n_helper.rb, line 61 def country_locales country_info ? country_info[:languages].split(",") : [] end
language_info()
click to toggle source
Hash to collect all language related info
# File lib/grass/helpers/i18n_helper.rb, line 15 def language_info { current: I18n.locale, path: path_locale, browser: browser_locale, country: country_locales } end
locale()
click to toggle source
Set locale by precedence path, browser, country, default
# File lib/grass/helpers/i18n_helper.rb, line 31 def locale return path_locale if !path_locale.nil? && I18n.available_locales.include?(path_locale) country_locales.each do |country_locale| return country_locale if I18n.available_locales.include? country_locale end country_locales.each do |country_locale| country_locale = country_locale.split("-").first.to_sym return country_locale if I18n.available_locales.include? country_locale end return browser_locale if I18n.available_locales.include? browser_locale I18n.default_locale end
path_locale()
click to toggle source
Extract locale from request path
# File lib/grass/helpers/i18n_helper.rb, line 49 def path_locale if path_locale = env['REQUEST_PATH'].scan(LNG_EXP_PATH).first path_locale.gsub("/","").to_sym end end
remote_ip()
click to toggle source
Client’s IP4 Address Stolen from Rack::Request
# File lib/grass/helpers/i18n_helper.rb, line 80 def remote_ip return (ENV["REMOTE_IP"]||=DEV_IP) if Goliat.env != :production if addr = env['HTTP_X_FORWARDED_FOR'] (addr.split(',').grep(/\d\./).first || env['REMOTE_ADDR']).to_s.strip else env['REMOTE_ADDR'] end end
set_locale()
click to toggle source
Set current locale
# File lib/grass/helpers/i18n_helper.rb, line 26 def set_locale I18n.locale = locale end