Add Portuguese support for ago patterns#5
Add Portuguese support for ago patterns#5marcelomogami wants to merge 2 commits intoAmato21:masterfrom
Conversation
marcelomogami
commented
Mar 30, 2026
- Fix patternToRegex to escape properly (split/escape/join approach)
- Add singular/plural support for Portuguese units (dias, semanas, meses)
- Add 'ago' key for prefix patterns (há X dias)
- Add 'agosuffix' key for suffix patterns (X dias atrás)
- Add minutesago/hoursago translations for Portuguese
- Update autocomplete to support both prefix and suffix formats
- Add ago/agosuffix keys to French, German, Spanish, English
- Fix patternToRegex to escape properly (split/escape/join approach) - Add singular/plural support for Portuguese units (dias, semanas, meses) - Add 'ago' key for prefix patterns (há X dias) - Add 'agosuffix' key for suffix patterns (X dias atrás) - Add minutesago/hoursago translations for Portuguese - Update autocomplete to support both prefix and suffix formats - Add ago/agosuffix keys to French, German, Spanish, English
There was a problem hiding this comment.
Code Review
This pull request implements multi-language support for "ago" and "agosuffix" expressions in the date parser and suggestion engine, updating translation files for German, English, Spanish, French, and Portuguese. Key improvements include refactoring the parser to use localized suffix patterns and enhancing the suggestion logic to handle past date prefixes and suffixes. Feedback focuses on refining the regex for unit capturing in the parser to prevent over-matching and correcting a spelling error in the Portuguese translation.
src/parser.ts
Outdated
| .map(p => p.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')) | ||
| .join('|'); | ||
| const suffixPattern = escapedSuffix.includes('|') ? `(?:${escapedSuffix})` : escapedSuffix; | ||
| const agoRegex = new RegExp(`^(\\d+)\\s+(\\w+)\\s+${suffixPattern}$`, 'i'); |
There was a problem hiding this comment.
The regex (\w+) for capturing the unit in agosuffix expressions is too broad. This could lead to incorrect parsing if a non-time unit word is used (e.g., "2 foo ago" would be parsed as "2 days ago"). It would be more robust to constrain this to known time units, similar to how unitRegexMap is used for prefix patterns.
| const agoRegex = new RegExp(`^(\\d+)\\s+(\\w+)\\s+${suffixPattern}$`, 'i'); | |
| const agoRegex = new RegExp(`^(\d+)\s+(${Array.from(this.timeUnitMap.keys()).map(k => k.replace(/[.*+?^${}()|[\\]/g, '\\$&')).join('|')})\s+${suffixPattern}$`, 'i'); |
There was a problem hiding this comment.
Fixed in the latest commit. The regex now uses timeUnitMap.keys() to constrain unit matching to known time units.
| in: "em|daqui a", | ||
| in: "em|daqui a|daqui", | ||
| ago: "há", | ||
| agosuffix: "atrás|atras", |
There was a problem hiding this comment.
Intentionally kept both forms - many Portuguese speakers type without accents on mobile keyboards. Added a comment in the code explaining this.
- Constrain ago suffix regex to known time units from timeUnitMap - Add comment explaining intentional unaccented 'atras' variant