Module: Grass::Helpers::I18nHelper
- Included in:
- Front
- Defined in:
- lib/grass/helpers/i18n_helper.rb
Constant Summary collapse
- LNG_EXP_PATH =
/^\/[a-z]{2}\-[A-Z]{2}\/|^\/[a-z]{2}\-[A-Z]{2}$|^\/[a-z]{2}\/|^\/[a-z]{2}$/
- LNG_EXP_HTTP =
/[a-z]{2}\-[A-Z]{2}|[a-z]{2}/
- DEV_IP =
ENV['DEV_IP'] || "86.185.186.43"
Class Method Summary collapse
-
.browser_locale ⇒ Object
Extract locale from accept language header.
-
.country_info ⇒ Object
Detailed country info.
-
.country_locales ⇒ Object
Get all spoken languages in a country sorted by speakers count.
-
.language_info ⇒ Object
Hash to collect all language related info.
-
.locale ⇒ Object
Set locale by precedence path, browser, country, default.
-
.path_locale ⇒ Object
Extract locale from request path.
-
.remote_ip ⇒ Object
Client’s IP4 Address Stolen from Rack::Request.
-
.set_locale ⇒ Object
Set current locale.
Class Method Details
.browser_locale ⇒ Object
Extract locale from accept language header
56 57 58 |
# 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 ⇒ Object
Detailed country info
70 71 72 73 74 75 76 |
# 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 ⇒ Object
Get all spoken languages in a country sorted by speakers count
61 62 63 |
# File 'lib/grass/helpers/i18n_helper.rb', line 61 def country_locales country_info ? country_info[:languages].split(",") : [] end |
.language_info ⇒ Object
Hash to collect all language related info
15 16 17 18 19 |
# 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 ⇒ Object
Set locale by precedence path, browser, country, default
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 ⇒ Object
Extract locale from request path
49 50 51 52 53 |
# 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 ⇒ Object
Client’s IP4 Address Stolen from Rack::Request
80 81 82 83 84 85 86 87 |
# 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 ⇒ Object
Set current locale
26 27 28 |
# File 'lib/grass/helpers/i18n_helper.rb', line 26 def set_locale I18n.locale = locale end |