Class: Brut::I18n::HTTPAcceptLanguage

Inherits:
Object
  • Object
show all
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

AlwaysEnglish

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(weighted_locales) ⇒ HTTPAcceptLanguage

Returns a new instance of HTTPAcceptLanguage.

Parameters:



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_localesObject (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.

Parameters:

  • value (String)

    the value provided by the brower.

Returns:



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.

Returns:



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.

Parameters:

  • session_value (String)

    the value stored in the session.

Returns:



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_sessionString

Serialize for storage in the session

Returns:

  • (String)

    a string that can be stored in the session and later deserialized via from_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.

Returns:

  • (Boolean)


96
# File 'lib/brut/i18n/http_accept_language.rb', line 96

def known? = true

#to_sObject



102
# File 'lib/brut/i18n/http_accept_language.rb', line 102

def to_s = self.for_session