gram/packages/i18n/README.md
abiwab a68a18c3c4
Some checks failed
CI / Build & Test (push) Has been cancelled
docs: rewrite root and package READMEs for v1.6
2026-11-29 22:35:50 +01:00

3.3 KiB

@gram-lang/i18n

npm License: GPL v3

Part of the Gram monorepo · Documentation

Localization and normalization module for the Gram recipe DSL. Normalizes mass, volume, and time unit aliases (e.g., càs, cuillère à soupe, teaspoon → tbsp, tsp) and holds all user-facing strings in English and French for the renderer, CLI views, and playground, as well as shopping category labels and AI language instructions.

Installation

npm install @gram-lang/i18n
# or
bun add @gram-lang/i18n

Usage

import { normalizeUnit, resolveTimeUnit } from '@gram-lang/i18n';

// Normalize measurement units
console.log(normalizeUnit('càs', 'fr'));        // 'tbsp'
console.log(normalizeUnit('tablespoon', 'en')); // 'tbsp'
console.log(normalizeUnit('g'));                // 'g'

// Normalize time units
console.log(resolveTimeUnit('heures', 'fr'));   // 'h'
console.log(resolveTimeUnit('minutes'));        // 'm'

Words and templates

getDictionary(lang) returns the dictionary for en or fr (renderer view labels, output and format names, duration units, and playground strings). fillTemplate(template, values) fills {name} placeholders and leaves missing keys intact so they show up instead of vanishing.

import { getDictionary, fillTemplate, formatNumber, parseDuration } from '@gram-lang/i18n';

const t = getDictionary('fr');
console.log(t.renderer.durationUnits);                           // { day: 'j', hour: 'h', minute: 'min', second: 's' }
console.log(fillTemplate('Serve at {time}', { time: '20:00' })); // 'Serve at 20:00'
console.log(formatNumber(1.5, 'fr'));                            // '1,5'
console.log(parseDuration('1h 30min'));                          // { ok: true, kind: 'single', ... }

Every key is defined in both en and fr together: surfaces never hardcode user-facing text.

How it works

Dictionaries are compiled once at startup into flat lookup maps via compileDictionary. When a locale ('fr', 'en') is provided, lookup checks that locale first and falls back to the global alias map.

Structure

  • src/index.ts: main package entry point.
  • src/dictionary.ts: compiles structured translation tables into flat lookup maps.
  • src/units.ts: mass and volume unit dictionary (FR & EN) and normalizer.
  • src/time.ts: time unit dictionary (FR & EN) and normalizer.
  • src/duration.ts: compound, clock, prime, and range duration parser (parseDuration()).
  • src/numbers.ts: locale-aware number formatting (formatNumber(), localizeDecimals(), toKitchenFraction()).
  • src/quantity.ts: quantity term arithmetic and formatting (termsTotal(), mergeTerms(), scaleTerms(), formatTerms()).
  • src/locales/: en and fr dictionaries and getDictionary().
  • src/template.ts: fillTemplate().
  • src/categories.ts: shopping list category keys and localized labels.
  • src/ai.ts: language instruction helper for optional CLI AI prompts.

License

Licensed under the GPL-3.0 license.