Module: Wurk::Web::LocaleNegotiator

Defined in:
lib/wurk/web/locale_negotiator.rb

Overview

Picks the dashboard locale to hint at from a request's Accept-Language.

The hint is emitted as #wurk-root[data-locale] and sits third in the SPA's precedence chain (?locale= > stored pick > this > browser preferences), so it only decides the first paint for a visitor who has never chosen — which is what removes the flash of English.

Matching mirrors bundleFor() in frontend/src/i18n/index.ts rung for rung: exact tag, then the header tag's base subtag, then the first offered locale sharing that base ("pt" -> "pt-BR"). Diverging would emit a hint the SPA then resolves to a different bundle.

Constant Summary collapse

QUALITY_RE =

Quality weight of a single Accept-Language entry. Case-insensitive because the parameter name is (RFC 9110 §5.6.6), even though every browser sends a lowercase q.

/;\s*q\s*=\s*([0-9.]+)/i

Class Method Summary collapse

Class Method Details

.call(header, offered:) ⇒ Object

The best offered locale for header, or nil when it asks for none of them. offered is host-configured (Wurk::Web.config.offered_locales) and the return value is always one of its entries, never header text — that is what keeps request input out of the emitted attribute, and why a malformed tag needs no separate validation: it simply matches nothing.



29
30
31
32
33
34
35
# File 'lib/wurk/web/locale_negotiator.rb', line 29

def call(header, offered:)
  preferred_tags(header).each do |tag|
    hit = match(tag, offered)
    return hit if hit
  end
  nil
end