You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/tempo/doc/tempo.cookbook.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -101,7 +101,7 @@ new Tempo('tomorrow afternoon');
101
101
102
102
::: tip
103
103
**Looking for Internationalized Parsing?**
104
-
Tempo can automatically translate months, weekdays, and relative terms (like 'yesterday', 'today', 'tomorrow') into foreign languages using your `locale` configuration. See the [Smart Parsing Guide](./tempo.parse.md#internationalized-parsing-locales) for full documentation and current capabilities.
104
+
Tempo can automatically translate months, weekdays, and relative terms (like 'yesterday', 'today', 'tomorrow') into foreign languages using your `locale` configuration. This requires enabling the parser option `parse: { localize: true }` (or the top-level `localize: true` flag) alongside your locale setting. See the [Smart Parsing Guide](./tempo.parse.md#internationalized-parsing-locales) for full documentation and current capabilities.
// Natively understand French input out-of-the-box!
134
+
// Natively understand French dates and core events!
135
135
newTempo('demain'); // parses as "tomorrow"
136
136
newTempo('15 fevrier 2026'); // parses as "15 February 2026"
137
-
newTempo('vendredi prochain'); // parses as "next Friday"
137
+
newTempo('vendredi'); // parses as the closest "Friday"
138
138
```
139
139
140
140
#### How it Works & Accent Normalization
@@ -144,24 +144,30 @@ It also automatically **normalizes and strips accents** from these generated rul
144
144
145
145
#### ⚠️ Current Limitations (What is NOT Available)
146
146
While `Intl` provides a robust foundation for month and weekday translations, there are limits to auto-localization:
147
-
***English Affixes**: Grammatical connector words like "ago", "next", "last", "in", and "from now" are heavily English-biased syntax rules. `Intl` does not provide translations for these parsing connectors, meaning `2 days ago` will only parse correctly if the keyword `ago` is used.
147
+
***English Affixes**: Grammatical connector words like "ago", "next", "last", "in", and "from now" are heavily English-biased syntax rules. `Intl` does not provide translations for these parsing connectors. When using the `Tempo` constructor with `parse: { localize: true }`, a relative string like `2 days ago`or `next Friday`will only parse correctly if the English connector keywords (`ago`, `next`) are used, unless Custom Aliases are used to bridge the gap.
148
148
***Time Units**: Words representing time units ("days", "weeks", "months") inside natural language strings are currently English-only.
149
149
***Grammar Structure**: The parser expects sequences matching standard English formats (e.g., `[value] [unit] [affix]`). Highly inflected languages or completely different phrase structures might fail to parse.
150
150
151
151
To bridge these gaps, you can register **Custom Aliases** (see below) to map foreign syntax to specific relative offsets manually!
152
152
153
153
### Custom Aliases (Events & Periods)
154
-
You can teach the parser new words:
154
+
You can teach the parser new words or entire foreign phrases to bridge translation gaps:
155
155
156
156
```typescript
157
157
Tempo.init({
158
+
locale: 'fr-FR',
159
+
parse: { localize: true },
158
160
event: {
161
+
// Map a full foreign phrase directly to an English-equivalent relative string
162
+
'vendredi prochain': () =>'next Friday',
163
+
// Or standard static events
159
164
'launch': '2026-12-01',
160
165
'party': () =>'next Friday 8pm'
161
166
}
162
167
});
163
168
164
-
const t =newTempo('party');
169
+
const t1 =newTempo('vendredi prochain'); // Parses accurately to next Friday
Enable full localization of mathematical modifier terms (e.g., mapping `"prochain"` to `">"` or `"next"`) and gracefully handle grammatical structure variations, such as inverted word ordering (e.g., trailing modifiers like `[weekday] [modifier]` vs. the English default `[modifier] [weekday]`).
5
+
6
+
## Architectural Considerations
7
+
8
+
### 1. Decoupling Math from Hardcoded English
9
+
Currently, the `parseModifier` function in `engine.lexer.ts` uses a strict `switch` statement that evaluates literal English strings (e.g., `case 'next': return 1`).
10
+
-**Challenge**: Passing foreign strings like `"prochain"` directly to this switch fails and defaults to `0`.
11
+
-**Solution Space**: Introduce a pre-lexing normalization step or a `modifier` registry that maps foreign string literals to standard internal mathematical tokens (like `>`, `<`, `+`, `-`) before they hit the mathematical evaluator.
12
+
13
+
### 2. Lexer & Master Guard Layout Flexibility
14
+
Tempo’s `Token.wkd` and standard layouts (e.g., `Pattern.WkdTime`) currently expect modifiers in specific positions (often as prefixes, with limited hardcoded suffixes like `next|last` for English).
15
+
-**Challenge**: When `parse: { localize: true }` is enabled, the localized snippet overrides completely drop trailing suffix captures.
16
+
-**Solution Space**: Update `support.init.ts` and `support.default.ts` to dynamically generate both prefix and suffix capture groups (`<mod_pre>` and `<mod_suf>`) in the localized regexes, allowing the parser to extract the modifier regardless of which side of the noun it appears.
17
+
18
+
### 3. Locale-Specific Grammatical Nuances
19
+
Different languages place modifiers in different structural positions depending on the entity.
20
+
-**Challenge**: A language might use a suffix for days (e.g., "vendredi prochain") but a prefix for other temporal periods.
21
+
-**Solution Space**: Should structural expectations be strictly tied to `Intl` locale codes, or should the engine use a "greedy" approach where it just attempts to extract modifiers from either side of the token without strictly enforcing grammatical correctness?
22
+
23
+
### 4. Configuration API Design
24
+
How will developers interact with this new capability?
25
+
-**Option A**: A brand new top-level configuration registry: `Tempo.init({ modifier: { 'prochain': 'next', 'dernier': 'last' } })`.
26
+
-**Option B**: Expanding the existing `event` or `snippet` objects.
27
+
-**Option C**: Can we extract these modifier words automatically from `Intl.RelativeTimeFormat`? (Investigate if `Intl` provides sufficient grammatical connector data).
28
+
29
+
### 5. Performance Implications
30
+
The core speed of Tempo relies heavily on Master Guard (RegEx) optimization and caching.
31
+
-**Challenge**: Adding multiple optional prefix and suffix capture branches to core snippets (like `wkd` and `rel`) will increase the complexity and backtracking potential of the Master Guard patterns.
32
+
-**Solution Space**: Ensure careful benchmarking when adding dynamic `<sfx>` groups to localized patterns.
0 commit comments