@comvi/core
v0.2.0
Published
Type-safe, framework-agnostic i18n library with plugin architecture for JavaScript and TypeScript
Maintainers
Readme
@comvi/core is the framework-independent runtime that powers every Comvi i18n binding. If you already use @comvi/vue, @comvi/react, @comvi/solid, @comvi/svelte, @comvi/next, or @comvi/nuxt, you have it transitively — install this package directly only when you're building a custom integration or running Comvi i18n in vanilla Node/browser code.
Ships an ICU MessageFormat parser, a plugin system, and locale-aware Intl formatters out of the box.
About Comvi i18n
Comvi i18n is a modern, framework-agnostic internationalization library — ICU MessageFormat, rich-text component embedding, and locale-aware Intl formatters in ~8 kB gzipped with zero runtime dependencies and no eval (CSP-safe for Chrome extensions, Cloudflare Workers, and locked-down enterprise apps).
- Same API across Vue, React, SolidJS, Svelte, Next.js, and Nuxt.
- Real ICU MessageFormat — locale-correct plurals, ordinals, and gender via
Intl.PluralRules. Recognized by every major TMS. - Type-safe translation keys via TypeScript declaration merging — autocomplete and parameter validation everywhere.
- Pluggable — translation loading, locale detection, and in-context editing are opt-in plugins.
See the main repo for the full library overview, runnable demos, and the framework binding matrix.
Why @comvi/core?
- Zero runtime dependencies, ~8 kB gzipped — drops into any JS environment without a tree of transitive packages.
- No
evalornew Function— runs under a strict CSP withoutunsafe-eval. Safe for Chrome extensions, Cloudflare Workers, and locked-down enterprise apps. - Plugin system, not a kitchen sink — translation loading, locale detection, and editing are opt-in plugins. You only ship what you use.
📖 Documentation: https://comvi.io/docs/i18n/vanilla/
Install
npm install @comvi/coreQuick start
import { createI18n } from "@comvi/core";
const i18n = createI18n({
locale: "en",
fallbackLocale: "en",
translation: {
en: {
greeting: "Hello, {name}!",
items: "{count, plural, one {# item} other {# items}}",
},
uk: {
greeting: "Привіт, {name}!",
items: "{count, plural, one {# елемент} few {# елементи} other {# елементів}}",
},
},
});
await i18n.init();
i18n.t("greeting", { name: "Alice" }); // "Hello, Alice!"
i18n.t("items", { count: 5 }); // "5 items"ICU MessageFormat — locale-correct grammar, not just singular/plural
count === 1 ? "item" : "items" works in English. It silently ships broken grammar in Polish, Ukrainian, Arabic, Welsh, and 30+ other locales — those languages have 3, 4, sometimes 6 distinct plural categories that a binary if/else can't express. ICU MessageFormat is the standard syntax for handling them — the same syntax Crowdin, Lokalise, Phrase, and every major TMS already speak. Comvi i18n parses it via native Intl.PluralRules, so every CLDR plural category is correct by default.
Plurals across languages
{
"en": { "messages": "{count, plural, one {# message} other {# messages}}" },
"uk": {
"messages": "{count, plural, one {# повідомлення} few {# повідомлення} many {# повідомлень} other {# повідомлення}}"
},
"ar": {
"messages": "{count, plural, zero {لا توجد رسائل} one {رسالة واحدة} two {رسالتان} few {# رسائل} many {# رسالة} other {# رسالة}}"
}
}i18n.t("messages", { count: 0 }); // ar: "لا توجد رسائل" (zero form)
i18n.t("messages", { count: 1 }); // en: "1 message" uk: "1 повідомлення"
i18n.t("messages", { count: 5 }); // en: "5 messages" uk: "5 повідомлень" ar: "5 رسائل"
i18n.t("messages", { count: 22 }); // uk: "22 повідомлення" ← the "few" form, NOT the "many" formA naive English-style count === 1 ? singular : plural picks one Ukrainian form and ships it for every count — grammatically wrong for half your traffic.
Ordinals (1st, 2nd, 3rd…)
{ "rank": "{place, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}" }i18n.t("rank", { place: 1 }); // "1st"
i18n.t("rank", { place: 22 }); // "22nd"
i18n.t("rank", { place: 113 }); // "113th"Select (gender, role, status)
{ "greeting": "{gender, select, female {Welcome, madam} male {Welcome, sir} other {Welcome}}" }i18n.t("greeting", { gender: "female" }); // "Welcome, madam"
i18n.t("greeting", { gender: "male" }); // "Welcome, sir"
i18n.t("greeting", { gender: "other" }); // "Welcome"Locale-aware Intl formatters
Numbers, dates, currency, and relative time follow the active locale via native Intl:
await i18n.setLocale("de");
i18n.formatNumber(1234.5); // "1.234,5"
i18n.formatCurrency(99.99, "USD"); // "99,99 $"
i18n.formatDate(new Date(), { dateStyle: "long" }); // "15. Januar 2025"
i18n.formatRelativeTime(-2, "hour"); // "vor 2 Stunden"
i18n.dir; // "ltr" | "rtl" — handles script subtags (ku-Arab → rtl, ks-Deva → ltr)Type-safe translation keys
Declaration merging on TranslationKeys provides autocomplete and parameter validation per key. Generated automatically via @comvi/cli (TMS) or @comvi/vite-plugin (local JSON).
// src/types/i18n.d.ts
declare module "@comvi/core" {
interface TranslationKeys {
welcome: { name: string };
greeting: never;
"errors:NOT_FOUND": never;
}
}// ✓ Compiles — params shape matches the declaration
i18n.t("welcome", { name: "Alice" });
// ✓ No params needed
i18n.t("greeting");
// ✓ Namespaced keys use the ns option
i18n.t("NOT_FOUND", { ns: "errors" });What TypeScript catches:
// ✗ Expected 2 arguments, but got 1
i18n.t("welcome");
// ✗ Property 'name' is missing in type '{ age: number }'
i18n.t("welcome", { age: 5 });
// ✗ Type 'number' is not assignable to type 'string'
i18n.t("welcome", { name: 42 });
// ✗ Argument of type '"typo"' is not assignable to parameter
i18n.t("typo", { name: "Alice" });Plugins
Translation loading, locale detection, and editing are opt-in plugins. Pass them through .use() before .init():
import { createI18n } from "@comvi/core";
import { FetchLoader } from "@comvi/plugin-fetch-loader";
import { LocaleDetector } from "@comvi/plugin-locale-detector";
const i18n = createI18n({ locale: "en", fallbackLocale: "en" })
.use(
LocaleDetector({
order: ["querystring", "cookie", "localStorage", "navigator"],
lookupCookie: "i18n_locale",
}),
)
.use(
FetchLoader({
cdnUrl: "https://cdn.comvi.io/your-distribution-id",
}),
);
await i18n.init();Plugins run sequentially during .init(), with timeout protection (10s default) and error recovery for non-required plugins. Each can return a cleanup function called on .destroy() in LIFO order.
For the full API — namespaces, fallback chains, missing-key handling, RTL detection, lifecycle events, and writing your own plugins — see the documentation.
License
MIT © Comvi
