Reach a global audience with built-in multi-language support. Translate strings, handle pluralization, and auto-detect user preferences.
Detects language from Headers, Cookies, or Query params.
Smart handling of plural forms (one/other).
Organize translations with directory structure (lang/en/file.json).
Default configuration is handled in your application creation or config.
1// src/app.ts2import { createApp } from "canxjs";34const app = createApp({5port: 3000,6locale: 'en',7fallbackLocale: 'en'8});
Create JSON files in your lang directory, organized by locale and filename.
1// lang/en/messages.json2{3"welcome": "Welcome to our application",4"auth": {5"login": "Login",6"failed": "Invalid credentials"7},8"items": {9"one": "You have {count} item",10"other": "You have {count} items"11}12}
Use the __ helper function to translate strings.
1import { __, trans, trans_choice } from "canxjs";23// Basic translation4__("messages.welcome"); // "Welcome to our application"56// Nested keys7__("messages.auth.login"); // "Login"89// With parameters10__("messages.greeting", { name: "Alice" });1112// Choice / Pluralization13trans_choice("messages.items", 1, { count: 1 }); // "You have 1 item"14trans_choice("messages.items", 5, { count: 5 }); // "You have 5 items"1516// Using Alias17trans("messages.welcome");
Use the middleware to automatically set the locale based on the request.
1// Auto-detect locale from query, cookie, or header2import { localizationMiddleware } from "canxjs";34app.use(localizationMiddleware({5// detect: ['query', 'cookie', 'header'],6// queryKey: 'lang',7// cookieKey: 'locale'8}));910// GET /?lang=es -> sets locale to 'es'