Skip to content

detectFromPath

reference
1 min readUpdated

Kind: Function

Source: src/middleware/language/language.ts

Part of: Middleware

Detects language from URL path

detectFromPath reads a URL path and determines the language encoded in that path. It is used by the language middleware to select the request language before downstream handlers process the request.

Signature

ts
function detectFromPath(c: Context, options: DetectorOptions): string | undefined

Parameters

NameType
cContext
optionsDetectorOptions

Returns: string | undefined

Diagram

mermaid
graph LR
  A[Request URL path] --> B[detectFromPath]
  B --> C[Detected language]
  C --> D[Language middleware]
  D --> E[Request handling]

Usage

ts
import { detectFromPath } from './middleware/language/language';

const language = detectFromPath('/en/docs/getting-started');

if (language) {
  console.log(`Detected language: ${language}`);
}

AI Coding Instructions

  • Pass the URL pathname rather than the full URL when calling detectFromPath.
  • Keep path parsing rules consistent with the language prefixes supported by the middleware.
  • Handle cases where no language is detected before selecting localized content.
  • Update related routing and middleware behavior when changing language path formats.

Was this page helpful?

Download as PDF
detectFromPath — Hono (narrator proof)