Files
SnowLuma/.pnpm-store/v11/files/c8/a6ecdf4b7805ac74c7ab48af3902d15b53568284e3c213ddfed989377245ad982a2a504ab2af99bb4169fb91669bb8b8082a68e32105fdec24d61afa96c8ac
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

118 lines
3.9 KiB
Plaintext

import { MiddlewareHandler } from "../../types.js";
import { Context } from "../../context.js";
//#region src/middleware/language/language.d.ts
export type DetectorType = 'path' | 'querystring' | 'cookie' | 'header';
export type CacheType = 'cookie';
export interface DetectorOptions {
/** Order of language detection strategies */
order: DetectorType[];
/** Query parameter name for language */
lookupQueryString: string;
/** Cookie name for language */
lookupCookie: string;
/** Index in URL path where language code appears */
lookupFromPathIndex: number;
/** Header key for language detection */
lookupFromHeaderKey: string;
/** Caching strategies */
caches: CacheType[] | false;
/** Cookie configuration options */
cookieOptions?: {
domain?: string;
path?: string;
sameSite?: 'Strict' | 'Lax' | 'None';
secure?: boolean;
maxAge?: number;
httpOnly?: boolean;
};
/** Whether to ignore case in language codes */
ignoreCase: boolean;
/** Default language if none detected */
fallbackLanguage: string;
/** List of supported language codes */
supportedLanguages: string[];
/** Optional function to transform detected language codes */
convertDetectedLanguage?: (lang: string) => string;
/** Enable debug logging */
debug?: boolean;
}
export interface LanguageVariables {
language: string;
}
export declare const DEFAULT_OPTIONS: DetectorOptions;
/**
* Parse Accept-Language header values with quality scores
* @param header Accept-Language header string
* @returns Array of parsed languages with quality scores
*/
export declare function parseAcceptLanguage(header: string): Array<{
lang: string;
q: number;
}>;
/**
* Validate and normalize language codes
* @param lang Language code to normalize
* @param options Detector options
* @returns Normalized language code or undefined
*/
export declare const normalizeLanguage: (lang: string | null | undefined, options: DetectorOptions) => string | undefined;
/**
* Detects language from query parameter
*/
export declare const detectFromQuery: (c: Context, options: DetectorOptions) => string | undefined;
/**
* Detects language from cookie
*/
export declare const detectFromCookie: (c: Context, options: DetectorOptions) => string | undefined;
/**
* Detects language from Accept-Language header
*/
export declare function detectFromHeader(c: Context, options: DetectorOptions): string | undefined;
/**
* Detects language from URL path
*/
export declare function detectFromPath(c: Context, options: DetectorOptions): string | undefined;
/**
* Collection of all language detection strategies
*/
export declare const detectors: {
readonly querystring: (c: Context, options: DetectorOptions) => string | undefined;
readonly cookie: (c: Context, options: DetectorOptions) => string | undefined;
readonly header: typeof detectFromHeader;
readonly path: typeof detectFromPath;
};
/** Type for detector functions */
export type DetectorFunction = (c: Context, options: DetectorOptions) => string | undefined;
/** Type-safe detector map */
export type Detectors = Record<keyof typeof detectors, DetectorFunction>;
/**
* Validate detector options
* @param options Detector options to validate
* @throws Error if options are invalid
*/
export declare function validateOptions(options: DetectorOptions): void;
/**
* Language detector middleware factory
* @param userOptions Configuration options for the language detector
* @returns Hono middleware function
*
* @example
* ```ts
* type Variables = LanguageVariables
* const app = new Hono<{ Variables: Variables }>()
*
* app.use(
* languageDetector({
* supportedLanguages: ['en', 'ja'], // Must include fallback
* fallbackLanguage: 'en', // Required
* })
* )
*
* app.get('/', (c) => {
* const lang = c.get('language')
* return c.text(`Current language: ${lang}`)
* })
* ```
*/
export declare const languageDetector: (userOptions: Partial<DetectorOptions>) => MiddlewareHandler;
//#endregion