@gram-lang/i18n 1.0.0-beta.4 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/index.d.ts +31 -8
- package/dist/index.js +140 -46
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@gram-lang/i18n)
|
|
4
4
|
[](https://www.gnu.org/licenses/gpl-3.0)
|
|
5
5
|
|
|
6
|
-
*Part of the [Gram monorepo](https://
|
|
6
|
+
*Part of the [Gram monorepo](https://git.gram-lang.org/gram-lang/gram).*
|
|
7
7
|
|
|
8
8
|
The translation and normalization module for units and time within the Gram recipe language. It provides high-performance, O(1) lookup dictionaries and normalizers to translate locale-specific aliases (e.g., `càs`, `cuillère à soupe`, `teaspoon`) into standardized canonical representation units (e.g., `tbsp`, `tsp`).
|
|
9
9
|
|
|
@@ -11,7 +11,7 @@ The translation and normalization module for units and time within the Gram reci
|
|
|
11
11
|
|
|
12
12
|
## 📚 General Documentation
|
|
13
13
|
|
|
14
|
-
For full syntax specifications, grammar details, cheatsheets, and best practices, please refer to the **[Gram Documentation](https://gram-lang.org/)**.
|
|
14
|
+
For full syntax specifications, grammar details, cheatsheets, and best practices, please refer to the **[Gram Documentation](https://docs.gram-lang.org/)**.
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
type LanguageDict = Record<string, string[]>;
|
|
2
2
|
type CompiledDict = Record<string, string>;
|
|
3
|
-
/**
|
|
4
|
-
* Compiles human-readable language dictionaries into optimized O(1) lookup tables.
|
|
5
|
-
* Generates both language-specific lookups and a unified global fallback lookup.
|
|
6
|
-
*/
|
|
3
|
+
/** Compiles alias lists into O(1) lookup tables, per language plus a unified global fallback. */
|
|
7
4
|
declare function compileDictionary(dictionaries: Record<string, LanguageDict>): {
|
|
8
5
|
byLang: Record<string, CompiledDict>;
|
|
9
6
|
global: CompiledDict;
|
|
@@ -15,9 +12,7 @@ declare const TIME_DICTIONARIES: {
|
|
|
15
12
|
};
|
|
16
13
|
declare const TIME_BY_LANG: Record<string, CompiledDict>;
|
|
17
14
|
declare const TIME_GLOBAL: CompiledDict;
|
|
18
|
-
|
|
19
|
-
* Helper to normalize a time unit string into its canonical alias ('h', 'm', 's').
|
|
20
|
-
*/
|
|
15
|
+
declare const TIME_TO_MINUTES: Record<string, number>;
|
|
21
16
|
declare const resolveTimeUnit: (unit?: string | null, lang?: string) => string;
|
|
22
17
|
|
|
23
18
|
declare const UNIT_DICTIONARIES: {
|
|
@@ -26,6 +21,14 @@ declare const UNIT_DICTIONARIES: {
|
|
|
26
21
|
};
|
|
27
22
|
declare const UNIT_BY_LANG: Record<string, CompiledDict>;
|
|
28
23
|
declare const UNIT_GLOBAL: CompiledDict;
|
|
24
|
+
interface UnitMap {
|
|
25
|
+
base: string;
|
|
26
|
+
map: Record<string, number>;
|
|
27
|
+
}
|
|
28
|
+
declare const UNIT_CONVERSIONS: {
|
|
29
|
+
mass: UnitMap;
|
|
30
|
+
volume: UnitMap;
|
|
31
|
+
};
|
|
29
32
|
/**
|
|
30
33
|
* Normalizes a raw string unit into a standard canon unit.
|
|
31
34
|
* Examples: 'càs' -> 'tbsp', 'kilo' -> 'kg'.
|
|
@@ -34,6 +37,23 @@ declare function normalizeUnit(rawUnit: string | undefined | null, lang?: string
|
|
|
34
37
|
|
|
35
38
|
declare function getAiLanguageInstruction(lang?: string): string;
|
|
36
39
|
|
|
40
|
+
declare const CATEGORY_KEYS: readonly ["vegetables", "fruits", "meat", "seafood", "dairy", "grains", "legumes", "nuts", "oils", "herbs", "spices", "condiments", "sugars", "beverages", "other"];
|
|
41
|
+
type CategoryKey = (typeof CATEGORY_KEYS)[number];
|
|
42
|
+
/**
|
|
43
|
+
* Display label for every category key, in canonical order — for showing to
|
|
44
|
+
* a human (a prompt, a UI list). Never persist these as data; persist the
|
|
45
|
+
* key (`CategoryKey`) instead, and translate to a label only at display time.
|
|
46
|
+
*/
|
|
47
|
+
declare function getCategoryLabels(lang?: string): Record<CategoryKey, string>;
|
|
48
|
+
declare function isCategoryKey(value: string): value is CategoryKey;
|
|
49
|
+
/**
|
|
50
|
+
* Returns display labels only, in canonical key order. Kept for the one
|
|
51
|
+
* legitimate label-only use left (`shopper.ts`'s sort-order comparison,
|
|
52
|
+
* which also has to keep matching pre-F-03 databases that already have a
|
|
53
|
+
* translated label — not a key — written into `category`). Prefer
|
|
54
|
+
* `getCategoryLabels`/`CATEGORY_KEYS` for anything that persists a category
|
|
55
|
+
* as data.
|
|
56
|
+
*/
|
|
37
57
|
declare function getDefaultCategories(lang?: string): string[];
|
|
38
58
|
|
|
39
59
|
declare const en: {
|
|
@@ -96,6 +116,7 @@ declare const en: {
|
|
|
96
116
|
recipe: string;
|
|
97
117
|
scaleFactor: string;
|
|
98
118
|
examples: {
|
|
119
|
+
newRecipe: string;
|
|
99
120
|
canneles: string;
|
|
100
121
|
empanadas: string;
|
|
101
122
|
};
|
|
@@ -125,6 +146,8 @@ declare const en: {
|
|
|
125
146
|
};
|
|
126
147
|
options: {
|
|
127
148
|
scaleTitle: string;
|
|
149
|
+
scaleGlobal: string;
|
|
150
|
+
scaleByIngredient: string;
|
|
128
151
|
selectIngredient: string;
|
|
129
152
|
qty: string;
|
|
130
153
|
unit: string;
|
|
@@ -165,4 +188,4 @@ type GramLocale = typeof en;
|
|
|
165
188
|
|
|
166
189
|
declare function getDictionary(lang?: string): GramLocale;
|
|
167
190
|
|
|
168
|
-
export { type CompiledDict, type GramLocale, type LanguageDict, TIME_BY_LANG, TIME_DICTIONARIES, TIME_GLOBAL, UNIT_BY_LANG, UNIT_DICTIONARIES, UNIT_GLOBAL, compileDictionary, getAiLanguageInstruction, getDefaultCategories, getDictionary, normalizeUnit, resolveTimeUnit };
|
|
191
|
+
export { CATEGORY_KEYS, type CategoryKey, type CompiledDict, type GramLocale, type LanguageDict, TIME_BY_LANG, TIME_DICTIONARIES, TIME_GLOBAL, TIME_TO_MINUTES, UNIT_BY_LANG, UNIT_CONVERSIONS, UNIT_DICTIONARIES, UNIT_GLOBAL, type UnitMap, compileDictionary, getAiLanguageInstruction, getCategoryLabels, getDefaultCategories, getDictionary, isCategoryKey, normalizeUnit, resolveTimeUnit };
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,12 @@ var fr = {
|
|
|
33
33
|
};
|
|
34
34
|
var TIME_DICTIONARIES = { en, fr };
|
|
35
35
|
var { byLang: TIME_BY_LANG, global: TIME_GLOBAL } = compileDictionary(TIME_DICTIONARIES);
|
|
36
|
+
var TIME_TO_MINUTES = {
|
|
37
|
+
d: 60 * 24,
|
|
38
|
+
h: 60,
|
|
39
|
+
m: 1,
|
|
40
|
+
s: 1 / 60
|
|
41
|
+
};
|
|
36
42
|
var resolveTimeUnit = (unit, lang) => {
|
|
37
43
|
if (!unit) return "";
|
|
38
44
|
const clean = unit.trim().toLowerCase();
|
|
@@ -56,7 +62,13 @@ var en2 = {
|
|
|
56
62
|
// Volume — cups & imperial
|
|
57
63
|
cup: ["cups"],
|
|
58
64
|
pt: ["pint", "pints"],
|
|
59
|
-
|
|
65
|
+
// "quart"/"quarts" is deliberately NOT an alias for "qt" here (audit
|
|
66
|
+
// 2026-07-22, finding F-01): the French word "quart" means "a quarter"
|
|
67
|
+
// (as in "un quart d'heure"), not the US liquid measure — the identical
|
|
68
|
+
// spelling made it a silent false-friend when both dictionaries mapped it
|
|
69
|
+
// to the same canonical. "qt" is still usable by typing it directly.
|
|
70
|
+
qt: [],
|
|
71
|
+
gal: ["gallon", "gallons"],
|
|
60
72
|
"fl oz": ["floz", "fl. oz.", "fluid ounce", "fluid ounces"],
|
|
61
73
|
// Volume — metric
|
|
62
74
|
ml: ["milliliter", "milliliters"],
|
|
@@ -97,26 +109,73 @@ var fr2 = {
|
|
|
97
109
|
pinch: ["pinc\xE9e", "pincee", "pinc\xE9es", "pincees"],
|
|
98
110
|
smidgen: ["poussi\xE8re", "poussiere", "poussi\xE8res", "poussieres"],
|
|
99
111
|
drop: ["goutte", "gouttes"],
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
112
|
+
// Pas d'alias FR pour "quart" (cf. dict EN ci-dessus) ni pour "pinte" : la
|
|
113
|
+
// pinte française historique n'a pas de valeur moderne fiable et unique
|
|
114
|
+
// (elle a varié de ~0,93 L à plus de 3 L selon les régions avant la
|
|
115
|
+
// métrication) — mieux vaut un avertissement « unité inconnue » qu'une
|
|
116
|
+
// conversion silencieusement fausse.
|
|
104
117
|
"fl oz": ["once liquide", "onces liquides"],
|
|
105
118
|
// Volume — métrique
|
|
106
119
|
ml: ["millilitre", "millilitres"],
|
|
107
120
|
cl: ["centilitre", "centilitres"],
|
|
108
121
|
dl: ["d\xE9cilitre", "d\xE9cilitres", "decilitre", "decilitres"],
|
|
109
122
|
l: ["litre", "litres"],
|
|
123
|
+
// Volume — tasse française (250 mL), distincte du "cup" nord-américain
|
|
124
|
+
// (236,588 mL) : les fusionner sous un seul canonique produisait une
|
|
125
|
+
// erreur silencieuse d'environ 6 % (audit 2026-07-22, finding F-01).
|
|
126
|
+
tasse: ["tasse", "tasses"],
|
|
110
127
|
// Poids — métrique
|
|
111
128
|
mg: ["milligramme", "milligrammes"],
|
|
112
129
|
g: ["gramme", "grammes"],
|
|
113
130
|
kg: ["kilo", "kilos", "kilogramme", "kilogrammes"],
|
|
131
|
+
// Poids — livre française (500 g, la "livre métrique" toujours en usage
|
|
132
|
+
// courant), distincte de la livre impériale "lb" (453,592 g) — les fusionner
|
|
133
|
+
// produisait ~500 g → 453 g, une erreur silencieuse d'environ 10 %.
|
|
134
|
+
livre: ["livre", "livres"],
|
|
114
135
|
// Poids — impérial
|
|
115
|
-
oz: ["once", "onces"]
|
|
116
|
-
lb: ["livre", "livres"]
|
|
136
|
+
oz: ["once", "onces"]
|
|
117
137
|
};
|
|
118
138
|
var UNIT_DICTIONARIES = { en: en2, fr: fr2 };
|
|
119
139
|
var { byLang: UNIT_BY_LANG, global: UNIT_GLOBAL } = compileDictionary(UNIT_DICTIONARIES);
|
|
140
|
+
var UNIT_CONVERSIONS = {
|
|
141
|
+
mass: {
|
|
142
|
+
base: "g",
|
|
143
|
+
map: {
|
|
144
|
+
mg: 1e-3,
|
|
145
|
+
g: 1,
|
|
146
|
+
kg: 1e3,
|
|
147
|
+
oz: 28.3495,
|
|
148
|
+
lb: 453.592,
|
|
149
|
+
// French "livre" (métrique) — see above for why this is a distinct
|
|
150
|
+
// canonical from "lb" rather than an alias of it.
|
|
151
|
+
livre: 500
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
volume: {
|
|
155
|
+
base: "ml",
|
|
156
|
+
map: {
|
|
157
|
+
ml: 1,
|
|
158
|
+
cl: 10,
|
|
159
|
+
dl: 100,
|
|
160
|
+
l: 1e3,
|
|
161
|
+
drop: 0.078,
|
|
162
|
+
smidgen: 0.156,
|
|
163
|
+
pinch: 0.3125,
|
|
164
|
+
dash: 0.625,
|
|
165
|
+
tad: 1.25,
|
|
166
|
+
tsp: 4.9289,
|
|
167
|
+
tbsp: 14.7868,
|
|
168
|
+
cup: 236.588,
|
|
169
|
+
// French "tasse" — see above for why this is a distinct canonical
|
|
170
|
+
// from "cup" rather than an alias of it.
|
|
171
|
+
tasse: 250,
|
|
172
|
+
pt: 473.176,
|
|
173
|
+
qt: 946.353,
|
|
174
|
+
gal: 3785.41,
|
|
175
|
+
"fl oz": 29.5735
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
};
|
|
120
179
|
function normalizeUnit(rawUnit, lang) {
|
|
121
180
|
if (!rawUnit) return "";
|
|
122
181
|
const clean = rawUnit.toLowerCase().trim();
|
|
@@ -144,44 +203,68 @@ function getAiLanguageInstruction(lang = "en") {
|
|
|
144
203
|
}
|
|
145
204
|
|
|
146
205
|
// src/categories.ts
|
|
147
|
-
var
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
"
|
|
167
|
-
"Fruits",
|
|
168
|
-
"
|
|
169
|
-
"
|
|
170
|
-
"
|
|
171
|
-
"
|
|
172
|
-
"
|
|
173
|
-
"
|
|
174
|
-
"
|
|
175
|
-
"
|
|
176
|
-
"
|
|
177
|
-
"Condiments",
|
|
178
|
-
"
|
|
179
|
-
"
|
|
180
|
-
"
|
|
181
|
-
|
|
206
|
+
var CATEGORY_KEYS = [
|
|
207
|
+
"vegetables",
|
|
208
|
+
"fruits",
|
|
209
|
+
"meat",
|
|
210
|
+
"seafood",
|
|
211
|
+
"dairy",
|
|
212
|
+
"grains",
|
|
213
|
+
"legumes",
|
|
214
|
+
"nuts",
|
|
215
|
+
"oils",
|
|
216
|
+
"herbs",
|
|
217
|
+
"spices",
|
|
218
|
+
"condiments",
|
|
219
|
+
"sugars",
|
|
220
|
+
"beverages",
|
|
221
|
+
"other"
|
|
222
|
+
];
|
|
223
|
+
var CATEGORY_LABELS = {
|
|
224
|
+
fr: {
|
|
225
|
+
vegetables: "L\xE9gumes",
|
|
226
|
+
fruits: "Fruits",
|
|
227
|
+
meat: "Viandes",
|
|
228
|
+
seafood: "Poissons & Fruits de mer",
|
|
229
|
+
dairy: "Cr\xE9merie",
|
|
230
|
+
grains: "C\xE9r\xE9ales",
|
|
231
|
+
legumes: "L\xE9gumineuses",
|
|
232
|
+
nuts: "Noix & Graines",
|
|
233
|
+
oils: "Huiles",
|
|
234
|
+
herbs: "Herbes",
|
|
235
|
+
spices: "\xC9pices",
|
|
236
|
+
condiments: "Condiments",
|
|
237
|
+
sugars: "Sucres",
|
|
238
|
+
beverages: "Boissons",
|
|
239
|
+
other: "Autres"
|
|
240
|
+
},
|
|
241
|
+
en: {
|
|
242
|
+
vegetables: "Vegetables",
|
|
243
|
+
fruits: "Fruits",
|
|
244
|
+
meat: "Meat",
|
|
245
|
+
seafood: "Seafood",
|
|
246
|
+
dairy: "Dairy",
|
|
247
|
+
grains: "Grains",
|
|
248
|
+
legumes: "Legumes",
|
|
249
|
+
nuts: "Nuts",
|
|
250
|
+
oils: "Oils",
|
|
251
|
+
herbs: "Herbs",
|
|
252
|
+
spices: "Spices",
|
|
253
|
+
condiments: "Condiments",
|
|
254
|
+
sugars: "Sugars",
|
|
255
|
+
beverages: "Beverages",
|
|
256
|
+
other: "Other"
|
|
257
|
+
}
|
|
182
258
|
};
|
|
259
|
+
function getCategoryLabels(lang = "en") {
|
|
260
|
+
return CATEGORY_LABELS[lang] ?? CATEGORY_LABELS.en;
|
|
261
|
+
}
|
|
262
|
+
function isCategoryKey(value) {
|
|
263
|
+
return CATEGORY_KEYS.includes(value);
|
|
264
|
+
}
|
|
183
265
|
function getDefaultCategories(lang = "en") {
|
|
184
|
-
|
|
266
|
+
const labels = getCategoryLabels(lang);
|
|
267
|
+
return CATEGORY_KEYS.map((key) => labels[key]);
|
|
185
268
|
}
|
|
186
269
|
|
|
187
270
|
// src/locales/en.ts
|
|
@@ -245,6 +328,7 @@ var en3 = {
|
|
|
245
328
|
recipe: "Recipe",
|
|
246
329
|
scaleFactor: "Scale Factor %",
|
|
247
330
|
examples: {
|
|
331
|
+
newRecipe: "Blank (New Recipe)",
|
|
248
332
|
canneles: "Bordeaux Cannel\xE9s",
|
|
249
333
|
empanadas: "Beef Empanadas"
|
|
250
334
|
},
|
|
@@ -273,7 +357,9 @@ var en3 = {
|
|
|
273
357
|
gantt_target_time_label: "Serve at:"
|
|
274
358
|
},
|
|
275
359
|
options: {
|
|
276
|
-
scaleTitle: "
|
|
360
|
+
scaleTitle: "Modify Quantities",
|
|
361
|
+
scaleGlobal: "Overall Scale",
|
|
362
|
+
scaleByIngredient: "By Ingredient",
|
|
277
363
|
selectIngredient: "Select ingredient...",
|
|
278
364
|
qty: "Qty",
|
|
279
365
|
unit: "Unit",
|
|
@@ -370,8 +456,9 @@ var fr3 = {
|
|
|
370
456
|
viewLabel: "Vue :",
|
|
371
457
|
loadExample: "Charger un exemple...",
|
|
372
458
|
recipe: "Recette",
|
|
373
|
-
scaleFactor: "
|
|
459
|
+
scaleFactor: "Quantit\xE9s",
|
|
374
460
|
examples: {
|
|
461
|
+
newRecipe: "Vide (Nouvelle recette)",
|
|
375
462
|
canneles: "Cannel\xE9s Bordelais",
|
|
376
463
|
empanadas: "Empanadas de Boeuf"
|
|
377
464
|
},
|
|
@@ -400,7 +487,9 @@ var fr3 = {
|
|
|
400
487
|
gantt_target_time_label: "Servir \xE0 :"
|
|
401
488
|
},
|
|
402
489
|
options: {
|
|
403
|
-
scaleTitle: "
|
|
490
|
+
scaleTitle: "Modifier les quantit\xE9s",
|
|
491
|
+
scaleGlobal: "Au global",
|
|
492
|
+
scaleByIngredient: "Par ingr\xE9dient",
|
|
404
493
|
selectIngredient: "S\xE9lectionner un ingr\xE9dient...",
|
|
405
494
|
qty: "Qt\xE9",
|
|
406
495
|
unit: "Unit\xE9",
|
|
@@ -449,16 +538,21 @@ function getDictionary(lang) {
|
|
|
449
538
|
return dictionaries[code] || en3;
|
|
450
539
|
}
|
|
451
540
|
export {
|
|
541
|
+
CATEGORY_KEYS,
|
|
452
542
|
TIME_BY_LANG,
|
|
453
543
|
TIME_DICTIONARIES,
|
|
454
544
|
TIME_GLOBAL,
|
|
545
|
+
TIME_TO_MINUTES,
|
|
455
546
|
UNIT_BY_LANG,
|
|
547
|
+
UNIT_CONVERSIONS,
|
|
456
548
|
UNIT_DICTIONARIES,
|
|
457
549
|
UNIT_GLOBAL,
|
|
458
550
|
compileDictionary,
|
|
459
551
|
getAiLanguageInstruction,
|
|
552
|
+
getCategoryLabels,
|
|
460
553
|
getDefaultCategories,
|
|
461
554
|
getDictionary,
|
|
555
|
+
isCategoryKey,
|
|
462
556
|
normalizeUnit,
|
|
463
557
|
resolveTimeUnit
|
|
464
558
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gram-lang/i18n",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Gram recipe i18n and unit normalization logic",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
"license": "GPL-3.0-or-later",
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "https://
|
|
21
|
+
"url": "https://git.gram-lang.org/gram-lang/gram.git",
|
|
22
22
|
"directory": "packages/i18n"
|
|
23
23
|
},
|
|
24
24
|
"bugs": {
|
|
25
|
-
"url": "https://
|
|
25
|
+
"url": "https://git.gram-lang.org/gram-lang/gram/issues"
|
|
26
26
|
},
|
|
27
|
-
"homepage": "https://
|
|
27
|
+
"homepage": "https://git.gram-lang.org/gram-lang/gram/src/branch/main/packages/i18n#readme",
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
@@ -35,4 +35,4 @@
|
|
|
35
35
|
"prepublishOnly": "npm run build"
|
|
36
36
|
},
|
|
37
37
|
"type": "module"
|
|
38
|
-
}
|
|
38
|
+
}
|