Class: Rack::ICU4X::Locale
- Inherits:
-
Object
- Object
- Rack::ICU4X::Locale
- Defined in:
- lib/rack/icu4x/locale.rb,
lib/rack/icu4x/locale/version.rb,
lib/rack/icu4x/locale/detector.rb,
lib/rack/icu4x/locale/negotiator.rb,
lib/rack/icu4x/locale/detector/query.rb,
lib/rack/icu4x/locale/detector/cookie.rb,
lib/rack/icu4x/locale/detector/header.rb
Overview
Rack middleware that detects user’s preferred locales from various sources.
Uses ICU4X’s maximize for script-safe language negotiation that respects script boundaries (e.g., zh-TW/Hant will NOT match zh-CN/Hans).
Defined Under Namespace
Modules: Detector Classes: Error, Negotiator
Constant Summary collapse
- ENV_KEY =
"rack.icu4x.locale"- DEFAULT_DETECTORS =
[:header].freeze
- VERSION =
"0.7.0"
Instance Method Summary collapse
-
#call(env) ⇒ Array
Response from the wrapped application.
-
#initialize(app, locales:, detectors: DEFAULT_DETECTORS, default: nil) ⇒ Locale
constructor
A new instance of Locale.
Constructor Details
#initialize(app, locales:, detectors: DEFAULT_DETECTORS, default: nil) ⇒ Locale
Returns a new instance of Locale.
46 47 48 49 50 51 52 53 |
# File 'lib/rack/icu4x/locale.rb', line 46 def initialize(app, locales:, detectors: DEFAULT_DETECTORS, default: nil) @app = app @locales = locales.map {|locale| normalize_locale(locale) } @detectors = build_detectors(detectors) @default = default && normalize_locale(default) validate_default_in_locales! @negotiator = Negotiator.new(@locales) end |
Instance Method Details
#call(env) ⇒ Array
Returns Response from the wrapped application.
57 58 59 60 |
# File 'lib/rack/icu4x/locale.rb', line 57 def call(env) env[ENV_KEY] = detect_locales(env) @app.call(env) end |