@lingo.dev/spec
v1.26.0
Published
Keywords
Readme
@lingo.dev/spec
The rules every Lingo.dev package and the CLI share: how a message key is derived, how plural and select messages are written in ICU, how a locale file is read, and how a locale's messages are loaded at runtime. Framework-free; jsonc-parser is its only dependency.
Getting started
import { createCatalog } from "@lingo.dev/spec";
// Locale files compiled by lingoCatalog() from @lingo.dev/vite
export const catalog = createCatalog(import.meta.glob("../locales/*.jsonc", { query: "?lingo" }), {
sourceLocale: "en",
});
const messages = await catalog("de");On a server that reads locale files itself, compileCatalog(await readFile(file, "utf8"), file) returns the same { key: value } map.
API
| Export | Does |
| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| createCatalog(modules, { sourceLocale? }) | A Catalog, (locale) => Promise<Record<string, string>>, over import.meta.glob("…/*.jsonc", { query: "?lingo" }). It memoises one promise per locale, answers {} for a locale without a file or one that fails to load, and ships no file for sourceLocale when NODE_ENV is production, since every lookup falls back to the source string. Deprecated: { query: "?raw" } still works, parsed in the browser by a lazily loaded parser with a one-time development warning. |
| compileCatalog(content, file) | The active entries of a locale file as { key: value }: comments and @orphan entries are dropped, and values, ICU included, pass through unchanged. A malformed file or a value that is not a string throws with the file, line and column. |
| readLocaleFile(content) | { entries }, each { key, value, metadata } with the @context, @src and @orphan read from the comment before it. Syntax errors throw. |
| getActiveEntries(entries) | The entries without metadata.orphan. |
| computeKey(source, context?) | The key of a source string and its optional context, the same in every package and the CLI. |
| buildIcuPlural(forms), buildIcuSelect(forms) | An ICU plural with the known categories in canonical order, or a select with sorted keys. |
Gotchas
compileCatalog and the JSONC parser it needs build into their own chunk, which createCatalog loads only for a ?raw file. An app that only reads compiled ?lingo modules ships neither.
