Overview
Parses translation text into an ordered array of ID-translation pairs, preserving the exact order of appearance and keeping duplicate IDs as separate entries. This differs fromparseTranslations which returns a Map and cannot represent duplicates.
Function Signature
Parameters
Raw translation text containing translations in the format “ID - Translation text”. Does not need to be pre-normalized.
Returns
Array of objects with
id and translation properties, in the order they appear in the input text. Each entry’s translation has the ID prefix removed and whitespace trimmed.Usage
Basic Example
Extracting IDs in Order
Preserving Duplicates
Multi-line Translations
Validating Order
Sequential Processing
How It Works
-
Normalization: Automatically normalizes the input using
normalizeTranslationText - Pattern Matching: Uses regex to find all translation markers (ID followed by dash)
- Text Extraction: For each ID, extracts all text until the next marker or end of string
- Prefix Removal: Removes the “ID - ” prefix and trims whitespace from each translation
- Order Preservation: Returns entries in exact appearance order
Comparison with parseTranslations
| Feature | parseTranslationsInOrder | parseTranslations |
|---|---|---|
| Return Type | Array | Object with Map |
| Preserves Order | Yes | No (Map iteration order) |
| Preserves Duplicates | Yes | No (last wins) |
| Lookup Performance | O(n) | O(1) |
| Use Case | Validation, Sequential | Fast lookup |
When to Use
UseparseTranslationsInOrder when:
- Order matters: You need to validate or preserve the sequence of translations
- Duplicate detection: You need to detect and handle duplicate IDs
- Sequential processing: You’re processing translations one-by-one in order
- Validation pipelines: Building validation logic that checks response structure
- Debugging: Analyzing LLM output issues where order is important
- You need fast O(1) lookup by ID → use
parseTranslations - You only need the IDs → use
extractTranslationIds - Working with very large datasets where array iteration would be slow
Best Practices
-
Validate expected order:
-
Detect duplicates:
-
Check for missing IDs:
Related Functions
parseTranslations- Parse into Map for fast lookupextractTranslationIds- Get only IDs in ordernormalizeTranslationText- Used internally for normalization