boxfw-i18n
v0.3.0
Published
Box Framework — built-in i18n/RTL support with locale detection and bilingual error messages
Maintainers
Readme
boxfw-i18n
Built-in internationalization (i18n) and RTL support for Box Framework — locale detection, bilingual error messages, and RTL metadata.
Installation
bun add boxfw-i18nRequires
boxfw-coreas a peer dependency.
Quick Start
import { Box } from "boxfw-core";
import { localeDetect, t, bilingualError } from "boxfw-i18n";
const app = new Box();
app.use(localeDetect({
default: "en",
supported: ["en", "fa", "ar"],
strategies: ["header", "query"],
}));
app.get("/", (c) => {
const greeting = t(c.locale, { en: "Hello", fa: "سلام" });
return c.text(greeting);
});Features
- Multi-strategy locale detection —
Accept-Languageheader, cookie, query parameter, IP (fallback) - Bilingual error messages —
bilingualError()returns localized error responses - RTL metadata —
rtlMeta()generates HTMLdirandlangattributes;isRtl()for conditional rendering - Extensible — register custom RTL languages with
addRtlLanguage()
API
// Middleware
app.use(localeDetect({ default: "en", supported: ["en", "fa"] }));
// Translation
t(locale, { en: "Hello", fa: "سلام" });
// Bilingual error
bilingualError(c, {
en: { error: "Not found", message: "The resource was not found" },
fa: { error: "پیدا نشد", message: "منبع مورد نظر یافت نشد" },
});
// RTL helpers
rtlMeta(locale); // → { dir: "rtl", lang: "fa" }
isRtl(locale); // → booleanLicense
MIT — see the LICENSE file for details.
