Class: Brut::I18n::HTTPAcceptLanguage
- Inherits:
-
Object
- Object
- Brut::I18n::HTTPAcceptLanguage
- Defined in:
- lib/brut/i18n/http_accept_language.rb
Overview
Manages the value for the HTTP Accept-Language header. Generally, you would not interact with this class directly, however it is used by Brut to make a guess as to which Locale a browser is reporting.
Direct Known Subclasses
Defined Under Namespace
Classes: AlwaysEnglish
Constant Summary collapse
- WeightedLocale =
A locale with the weight (value for q=) it was given in the Accept-Language header
Data.define(:locale, :q) do # Returns the primary locale for whatever locale # this is holding. For example, the primary locale # of "en-US" is "en". def primary_locale = self.locale.gsub(/\-.*$/,"") # True if this locale is a primary locale def primary? = self.primary_locale == self.locale # Return a new WeightedLocale that is the primary locale. def primary_only self.class.new(locale: self.primary_locale, q: self.q) end def ==(other) self.locale == other.locale end end
Instance Attribute Summary collapse
-
#weighted_locales ⇒ Object
readonly
Ordered list of locales, from highest-weighted to lowest.
Class Method Summary collapse
-
.from_browser(value) ⇒ Brut::I18n::HTTPAcceptLanguage
Parse the value provided by the browser via FrontEnd::Handlers::LocaleDetectionHandler via the
brut-locale-detection
custom element (which usesIntl.DateTimeFormat().resolvedOptions()
to determine the locale). -
.from_header(header_value) ⇒ Brut::I18n::HTTPAcceptLanguage
Parse from the HTTP Accept-Language header.
-
.from_session(session_value) ⇒ Brut::I18n::HTTPAcceptLanguage
Parse the value stored in the session.
Instance Method Summary collapse
-
#for_session ⇒ String
Serialize for storage in the session.
-
#initialize(weighted_locales) ⇒ HTTPAcceptLanguage
constructor
A new instance of HTTPAcceptLanguage.
-
#known? ⇒ Boolean
True if the values inside this object represent known locales, and not a guess based on missing information.
-
#to_s ⇒ Object
Constructor Details
#initialize(weighted_locales) ⇒ HTTPAcceptLanguage
Returns a new instance of HTTPAcceptLanguage.
90 91 92 |
# File 'lib/brut/i18n/http_accept_language.rb', line 90 def initialize(weighted_locales) @weighted_locales = weighted_locales.sort_by(&:q).reverse end |
Instance Attribute Details
#weighted_locales ⇒ Object (readonly)
Ordered list of locales, from highest-weighted to lowest.
87 88 89 |
# File 'lib/brut/i18n/http_accept_language.rb', line 87 def weighted_locales @weighted_locales end |
Class Method Details
.from_browser(value) ⇒ Brut::I18n::HTTPAcceptLanguage
Parse the value provided by the browser via
FrontEnd::Handlers::LocaleDetectionHandler via
the brut-locale-detection
custom element (which
uses Intl.DateTimeFormat().resolvedOptions()
to determine
the locale).
Because this value is not in the same format as the Accept-Language
header, it's q
is assumed to be 1.
56 57 58 59 60 61 62 63 |
# File 'lib/brut/i18n/http_accept_language.rb', line 56 def self.from_browser(value) value = value.to_s.strip if value == "" AlwaysEnglish.new else self.new([ WeightedLocale.new(locale: value, q: 1) ]) end end |
.from_header(header_value) ⇒ Brut::I18n::HTTPAcceptLanguage
Parse from the HTTP Accept-Language header.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/brut/i18n/http_accept_language.rb', line 69 def self.from_header(header_value) header_value = header_value.to_s.strip if header_value == "*" || header_value == "" AlwaysEnglish.new else values = header_value.split(/,/).map(&:strip).map { |language| locale,q = language.split(/;\s*q\s*=\s*/,2) WeightedLocale.new(locale: locale,q: q.nil? ? 1 : q.to_f) } if values.any? self.new(values) else AlwaysEnglish.new end end end |
.from_session(session_value) ⇒ Brut::I18n::HTTPAcceptLanguage
Parse the value stored in the session.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/brut/i18n/http_accept_language.rb', line 31 def self.from_session(session_value) values = session_value.to_s.split(/,/).map { |value| locale,q = value.split(/;/) WeightedLocale.new(locale:,q:) } if values.any? self.new(values) else AlwaysEnglish.new end end |
Instance Method Details
#for_session ⇒ String
Serialize for storage in the session
101 |
# File 'lib/brut/i18n/http_accept_language.rb', line 101 def for_session = @weighted_locales.map { |weighted_locale| "#{weighted_locale.locale};#{weighted_locale.q}" }.join(",") |
#known? ⇒ Boolean
True if the values inside this object represent known locales, and not a guess based on missing information. In general, this returns true if the values came from the Accept-Language header, or from the browser.
96 |
# File 'lib/brut/i18n/http_accept_language.rb', line 96 def known? = true |
#to_s ⇒ Object
102 |
# File 'lib/brut/i18n/http_accept_language.rb', line 102 def to_s = self.for_session |