vyrn

std/i18n

1 export, from std/i18n.vyrn.

Typed translations as a generator library.

import { i18n } from "std/i18n"
import { t, setLocale, locale, Locale, TransKey, tCartItems } from i18n("./locales")

i18n(dir) reads every <locale>.json under dir at compile time, checks that the locales agree, compiles each ICU message to a Vyrn function body and returns the module as source text (vyrn emit-gen <file> shows it). The module holds Locale (one variant per file), setLocale/locale, TransKey (the finite type of every dotted key), one function per key with arguments taken from the message and the base-locale message as its doc, t(key), and transKey(s), which checks a runtime String against the key set.

The ICU subset: plain text; {name} (String); {n, plural, ...} (Int64, # is the number, other required); {k, select, ...} (a finite string type of the branch names). '' is a literal apostrophe, '{' and '}' literal braces. An argument name may not start with __. Numbers, booleans and null in a locale file, and a plural or select nested in a branch, fail generation.

Dependencies

The import graph shows the whole library.

Imported by
gen fn i18n(dir: String) -> String

Read every <locale>.json under dir, check the locales, and return the translation module's source.

Tests

19 test blocks in std/i18n.vyrn. CI runs all of them.

Test names
  • parseLocaleJson flattens nested keys with dots
  • parseLocaleJson rejects a non-string value
  • parseLocaleJson decodes escapes
  • scanArgs finds a plain string arg
  • scanArgs finds a plural arg
  • scanArgs finds a select arg with branch names
  • argSignature is order-independent for drift
  • plural categories cover the Slavic locales
  • compileBody turns a plural into an if-chain
  • plural conditions take the absolute value, so negatives pick the CLDR branch
  • an exact `=N` selector matches the count itself, not its absolute value
  • a select selector containing a comma is refused at scan time
  • keyFnName pascalizes dotted keys
  • regexEscapeKey doubles the backslash for dots
  • readLiteral keeps a lone apostrophe and does not swallow the placeholder
  • readLiteral: paired '' is one apostrophe, '{' is a literal brace
  • scanArgs sees the arg through an apostrophe-bearing literal
  • icuBadBrace flags an unmatched or stray brace, quote-aware
  • keyCollisionErrors names every collision class

Examples

3 programs in examples/ import this module. Each runs as WebAssembly and as a native binary.