Module: Philiprehberger::LocaleKit::Negotiator

Defined in:
lib/philiprehberger/locale_kit/negotiator.rb

Overview

Content negotiation for matching requested locales against available locales.

Uses a fallback chain strategy: for each requested locale, tries the exact match first, then walks up the parent chain (e.g., en-Latn-US -> en-Latn -> en) looking for matches in the available set. Quality weights from Accept-Language parsing are respected.

Class Method Summary collapse

Class Method Details

.negotiate(requested, available, default: nil) ⇒ Locale?

Finds the best matching locale from available options.

Parameters:

  • requested (Array<String, Locale>)

    locales requested by the client, in preference order

  • available (Array<String, Locale>)

    locales the server can provide

  • default (String, Locale, nil) (defaults to: nil)

    fallback locale if no match found

Returns:

  • (Locale, nil)

    the best matching locale, or the default, or nil



17
18
19
20
21
22
23
24
25
# File 'lib/philiprehberger/locale_kit/negotiator.rb', line 17

def self.negotiate(requested, available, default: nil)
  requested_locales = normalize(requested)
  available_locales = normalize(available)

  return resolve_default(default) if requested_locales.empty? || available_locales.empty?

  best = find_best_match(requested_locales, available_locales)
  best || resolve_default(default)
end