Class: Brut::FrontEnd::Components::LocaleDetection

Inherits:
Brut::FrontEnd::Component show all
Defined in:
lib/brut/front_end/components/locale_detection.rb

Overview

Produces the <brut-locale-detection> custom element, with attributes set as appropriate based on the server's understanding of the current session's locale.

The <brut-locale-detection> element exists to send a JSON payload back to the server (handled by Handlers::LocaleDetectionHandler), with information about the browser's time zone and locale.

This element doesn't need to do this if the server has this information. This component handles creating the right HTML to either ask the browser to send it, or not.

Instance Method Summary collapse

Methods inherited from Brut::FrontEnd::Component

component_name, #component_name

Methods included from Brut::FrontEnd::Component::Helpers

#global_component, #inline_svg

Methods included from I18n::ForHTML

#html_escape, #t

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from Brut::Framework::Errors

#abstract_method!, #bug!

Constructor Details

#initialize(session:) ⇒ LocaleDetection

Returns a new instance of LocaleDetection.



10
11
12
13
14
# File 'lib/brut/front_end/components/locale_detection.rb', line 10

def initialize(session:)
  @timezone = session.timezone_from_browser
  @locale   = session.http_accept_language.known? ? session.http_accept_language.weighted_locales.first&.locale : nil
  @url      = Brut::FrontEnd::Handlers::LocaleDetectionHandler.routing
end

Instance Method Details

#view_templateObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/brut/front_end/components/locale_detection.rb', line 16

def view_template
  attributes = {
    "url" => @url,
  }
  if @timezone
    attributes["timezone-from-server"] = @timezone.name
  end
  if @locale
    attributes["locale-from-server"] = @locale
  end
  if !Brut.container.project_env.production?
    attributes["show-warnings"] = true
  end

  brut_locale_detection(**attributes)
end