Module: Bp3::ActionDispatch::RequestLocale
- Defined in:
- lib/bp3/action_dispatch/request_locale.rb
Instance Method Summary collapse
- #extract_locale ⇒ Object private
- #extract_locale_from_header ⇒ Object private
- #extract_locale_from_host ⇒ Object private
- #extract_locale_from_params ⇒ Object private
- #extract_locale_from_path ⇒ Object private
- #extract_locale_from_setting ⇒ Object private
- #locale_if_exists(locale) ⇒ Object private
- #locale_to_sym(locale) ⇒ Object private
-
#parse_header(header) ⇒ Object
private
example format: 'en;q=0.1,nl'.
- #request_locale ⇒ Object
Instance Method Details
#extract_locale ⇒ Object (private)
12 13 14 15 16 17 18 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 12 def extract_locale extract_locale_from_path || extract_locale_from_params || extract_locale_from_host || extract_locale_from_header || extract_locale_from_setting end |
#extract_locale_from_header ⇒ Object (private)
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 42 def extract_locale_from_header header = env['HTTP_ACCEPT_LANGUAGE'] return if header.blank? languages = parse_header(header) sorted = languages.sort_by { |_l, q| q }.reverse locales = sorted.map(&:first) symbolized = locales.map { |l| locale_to_sym(l) } available = symbolized.select { |l| I18n.available_locales.include?(l) } locale = available.first @locale_from = 'header' if locale locale end |
#extract_locale_from_host ⇒ Object (private)
36 37 38 39 40 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 36 def extract_locale_from_host locale = locale_if_exists(host.split('.').last) @locale_from = 'host' if locale locale end |
#extract_locale_from_params ⇒ Object (private)
27 28 29 30 31 32 33 34 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 27 def extract_locale_from_params return nil if params[:locale].blank? locale = params[:locale] locale = locale_if_exists(locale) @locale_from = 'params' if locale locale end |
#extract_locale_from_path ⇒ Object (private)
20 21 22 23 24 25 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 20 def extract_locale_from_path locale = original_fullpath.split('/')[original_fullpath.starts_with?('/') ? 1 : 0] locale = locale_if_exists(locale) @locale_from = 'path' if locale locale end |
#extract_locale_from_setting ⇒ Object (private)
56 57 58 59 60 61 62 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 56 def extract_locale_from_setting return if request_site&.config.nil? locale = locale_if_exists(request_site.configs.locale.presence) @locale_from = 'setting' if locale locale end |
#locale_if_exists(locale) ⇒ Object (private)
74 75 76 77 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 74 def locale_if_exists(locale) locale = locale_to_sym(locale) I18n.available_locales.include?(locale) ? locale : nil end |
#locale_to_sym(locale) ⇒ Object (private)
79 80 81 82 83 84 85 86 87 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 79 def locale_to_sym(locale) return nil if locale.nil? return locale.to_sym if locale.length == 2 locale, region = locale.split('-') return locale.to_sym if region.nil? :"#{locale}-#{region.upcase}" end |
#parse_header(header) ⇒ Object (private)
example format: 'en;q=0.1,nl'
65 66 67 68 69 70 71 72 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 65 def parse_header(header) languages = header.split(',').map(&:strip) languages.map do |language| locale, quality = language.split(/\s*;\s*q\s*=\s*/i) quality = quality.blank? ? 1.0 : quality.to_f [locale, quality] end end |
#request_locale ⇒ Object
6 7 8 |
# File 'lib/bp3/action_dispatch/request_locale.rb', line 6 def request_locale @request_locale ||= extract_locale end |