gram/packages/i18n
Repository files (latest commit first)
Filename Latest commit message Latest commit date
abiwab 7d67ccadaf
All checks were successful
CI / Build & Test (pull_request) Successful in 9m18s
chore: release v1.6.0
2026-12-01 14:38:56 +01:00
..
src feat(docs): set Neapolitan pizza as default playground preset 2026-11-30 11:19:52 +01:00
tests fix(i18n): read a fraction with a decimal numerator as a duration 2026-11-26 20:56:11 +01:00
CHANGELOG.md chore: release v1.6.0 2026-12-01 14:38:56 +01:00
package.json chore: release v1.6.0 2026-12-01 14:38:56 +01:00
README.md docs: rewrite root and package READMEs for v1.6 2026-11-29 22:35:50 +01:00
tsconfig.json feat: New i18n package for centralized localization 2026-06-08 20:35:33 +02:00

@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.