@alxia/language

中文文档
0.3.15发布于 2026/11/02MITTypeScriptES Module零依赖近 30 天下载 1934

The request's language for alxia, typed as the languages you support: from the query, a cookie, the path or Accept-Language, weights and regions included

原始使用文档
文档目录5 节
  1. Usage
  2. Reading the app's context
  3. Options
  4. API
  5. Documentation

@alxia/language

The request's language for alxia, typed as the languages you support — never a string a client made up. No dependency.

bun add @alxia/language @alxia/core
bun add -d typescript

Usage

import { alxia } from '@alxia/core';
import { language } from '@alxia/language';

const supported = ['en', 'fr', 'pt-BR'] as const;
const greetings: Record<(typeof supported)[number], string> = { en: 'Hello', fr: 'Bonjour', 'pt-BR': 'Olá' };

const app = alxia()
    .use(language({ supported, fallback: 'en' }))
    .get('/', ({ language, reply }) => reply(200, greetings[language])); // 'en' | 'fr' | 'pt-BR'

app.listen(3000);

It reads, in order:

source
query?lang=fr
cookielanguage=fr
path/fr/products, at pathIndex
headerAccept-Language, by weight: de, fr-CA;q=0.8 is fr

then resolve(ctx) — a user's saved preference — then fallback. A tag matches whatever its case, by its base language (fr-CA for fr), or by a region of it (pt for pt-BR). languageSource says which source decided.

Every response says Content-Language, and Vary by the headers it read. With persist, a language the query named is kept in the cookie. Given to use on the app, it runs on every request: a 404 says Content-Language too.

Reading the app's context

Annotate resolve's parameter to decide by what an earlier middleware added — the language a signed-in user saved. The middleware then requires it: an app that does not give user before it cannot use it.

import { alxia, type BaseContext } from '@alxia/core';
import { language } from '@alxia/language';

const byUser = language({
    supported: ['en', 'fr'],
    fallback: 'en',
    order: ['query', 'cookie'],
    resolve: ({ user }: BaseContext & { user: { language: string } | null }) => user?.language,
});

alxia().plugin(auth).use(byUser); // auth derives user
alxia().use(byUser); // a compile error: this app gives no `user`

A resolve annotated any would require nothing, so the middleware is refused on every app: annotate what it reads, or leave it unannotated.

Options

optiondefault
supportedrequiredthe languages: language's type
fallbackrequiredone of them; the types refuse another
order['query', 'cookie', 'header']
query, cookielang, languagetheir names
pathIndex0
persistfalsetrue, or { maxAge, secure }
contentLanguagetrue
resolvenone(ctx) => string | undefined; annotate ctx to read what an earlier middleware adds
varynonethe headers resolve reads, added to Vary

API

export
language(options)the middleware, given to app.use: it adds language and languageSource
LanguageOptionsits options: supported, fallback, order, query, cookie, pathIndex, persist, contentLanguage, resolve, vary
negotiate(header, supported)the supported language Accept-Language prefers
parseAcceptLanguage(header), match(tag, supported)its parts
LanguageContext, LanguageSource, Acceptedits types
LanguageMiddleware<L, Requires>what language() returns: a middleware adding LanguageContext<L>, requiring of the app what resolve reads

Documentation

  • Guide: every option with its default and an example, how the language is found and Accept-Language negotiated, reading what an earlier middleware added, the typed context, and the headers the middleware adds.
  • Troubleshooting: an error, or a response in the wrong language, and what to do about it.
  • Roadmap: what is coming, and what is not planned.

版本与发布记录

@alxia/language 共发布 20 个版本,首次发布于 2026/10/04;过去 12 个月发布了 20 个版本。

版本发布日期距今
0.3.15latest2026/11/026 天前
0.3.142026/11/017 天前
0.3.132026/10/2910 天前
0.3.122026/10/2612 天前
0.3.112026/10/2613 天前
0.3.102026/10/2216 天前

关于 @alxia/language 的常见问题

@alxia/language 是什么?

The request's language for alxia, typed as the languages you support: from the query, a cookie, the path or Accept-Language, weights and regions included

如何安装 @alxia/language?

在项目目录运行 npm install @alxia/language,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(^6.0.3 || ^7.0.0)、@alxia/core(^0.19.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@alxia/language 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@alxia/language 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@alxia/language 可以商用吗?

@alxia/language 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alxia/language 的下载量有多少?

根据 npm 官方下载统计,@alxia/language 在 2026/10/06 – 2026/11/04 这 30 天内被下载 1,934 次,日均约 64 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@alxia/language 还在维护吗?

最新版本 0.3.15 发布于 2026/11/02(6 天前);至今共发布 20 个版本,其中过去 12 个月发布了 20 个;首次发布于 2026/10/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。