Skip to content

DetectorOptions

reference
3 min readUpdated

Kind: Interface

Source: src/middleware/language/language.ts

Part of: Middleware

DetectorOptions configures how language middleware detects, validates, and stores the active language. It defines detection sources, cache behavior, cookie settings, matching rules, fallback behavior, and the languages accepted by the application.

Properties

PropertyType
orderDetectorType[]
lookupQueryStringstring
lookupCookiestring
lookupFromPathIndexnumber
lookupFromHeaderKeystring
caches`CacheType[]
cookieOptions`{ domain?: string path?: string sameSite?: 'Strict'
ignoreCaseboolean
fallbackLanguagestring
supportedLanguagesstring[]
convertDetectedLanguage(lang: string) => string
debugboolean

Diagram

mermaid
graph LR
  Request[Incoming request] --> Order[Detection order]
  Order --> Query[Query string]
  Order --> Cookie[Cookie]
  Order --> Path[URL path]
  Order --> Header[Request header]

  Query --> Language[Detected language]
  Cookie --> Language
  Path --> Language
  Header --> Language

  Language --> Supported[Supported languages]
  Supported -->|match| Active[Active language]
  Supported -->|no match| Fallback[Fallback language]

  Active --> Cache[Configured caches]
  Cache --> CookieOptions[Cookie options]

Usage

ts
import type {
  CacheType,
  DetectorOptions,
  DetectorType,
} from './src/middleware/language/language'

function createDetectorOptions(
  lookupFromPathIndex: number,
): DetectorOptions {
  const order: DetectorType[] = ['querystring', 'cookie', 'path', 'header']
  const caches: CacheType[] = ['cookie']

  return {
    order,
    lookupQueryString: 'lang',
    lookupCookie: 'language',
    lookupFromPathIndex,
    lookupFromHeaderKey: 'accept-language',
    caches,
    cookieOptions: {
      path: '/',
      sameSite: 'Lax',
      httpOnly: true,
      secure: true,
    },
    ignoreCase: true,
    fallbackLanguage: 'en',
    supportedLanguages: ['en', 'fr', 'de'],
  }
}

AI Coding Instructions

  • Keep order aligned with the request sources supported by the language middleware.
  • Use supportedLanguages to limit accepted detected values, and set fallbackLanguage to a supported value.
  • Set caches to false when detected languages must not be persisted between requests.
  • Match lookupQueryString, lookupCookie, and lookupFromHeaderKey with the names used by application routes and clients.
  • Configure cookieOptions for the deployment environment, including secure, sameSite, and cookie scope.

How it works

DetectorOptions is the exported TypeScript interface that defines the complete configuration shape used by language detection functions and by languageDetector. Its required fields select detection sources, configure their lookups, define language matching, caching, and fallback behavior; cookieOptions, convertDetectedLanguage, and debug are optional. src/middleware/language/language.ts:13-45

Language candidates are trimmed, optionally converted, then checked first for an exact configured match. If that fails, a longer detected tag may match the longest configured prefix immediately followed by -; otherwise it is rejected. src/middleware/language/language.ts:92-131 For header detection, parsed Accept-Language entries are examined in parser order, which is sorted by descending quality only when the parser observes a later entry with a higher quality value. src/middleware/language/language.ts:153-167 src/utils/accept.ts:211-237

languageDetector accepts Partial<DetectorOptions>, overlays it on DEFAULT_OPTIONS, and separately merges nested cookie settings. The defaults check query string lang, cookie language, then header accept-language; accept en; fall back to en; cache in a cookie; ignore case; and set cache-cookie defaults of SameSite=Strict, Secure, one-year maxAge, and HttpOnly. src/middleware/language/language.ts:51-68 src/middleware/language/language.ts:292-300

Before returning middleware, construction throws Error when the fallback is absent from supportedLanguages, the path index is negative, or order contains a name outside the detector map. src/middleware/language/language.ts:204-216 On each request, the resulting language is stored as ctx variable language before downstream middleware runs. src/middleware/language/language.ts:304-308

Was this page helpful?

Download as PDF
DetectorOptions — Hono (narrator proof)