Documentation Index
Fetch the complete documentation index at: https://mintlify.com/dreancaste/TriviaPP/llms.txt
Use this file to discover all available pages before exploring further.
TranslationService bridges SWAPI’s English-language data and TriviaPP’s Spanish-language UI. Because SWAPI returns values such as planet climates ("arid", "temperate, tropical") and film descriptions in English, this service transparently converts them to Spanish before they appear in question text or answer options. It uses the free MyMemory REST API for translation, applies automatic text chunking to stay within the API’s 450-character query limit, and capitalises the result for consistent display.
Constructor / Dependencies
| Dependency | Token | Role |
|---|---|---|
HttpClient | @angular/common/http | Issues GET requests to the MyMemory API |
Public Methods
translate()
The primary translation entry point. Translates a string from English to Spanish, capitalises each comma-separated segment of the result, and falls back to the original (capitalised) input if the API call fails.
The English text to translate. Typical inputs are SWAPI climate strings such as
"arid", "temperate, tropical", or "frozen".replace(/\bde\b/gi, 'de')— normalises straydeprepositions that MyMemory sometimes leaves with incorrect casing.capitalize()— splits on commas, trims each segment, uppercases the first character, and lowercases the rest (e.g."árido, TEMPLADO"→"Árido, Templado").
translateRaw()
The lower-level translation method called internally by translate(). Handles the chunking strategy for long texts and dispatches individual API calls.
Raw English text to translate. May be of any length — chunking is applied automatically.
- If
text.length <= 450: callsrequestTranslation(text)directly and returns the result. - If
text.length > 450: callssplitForApi(text)to break the text into sentence-level chunks, dispatches all chunks torequestTranslation()concurrently viaPromise.all(), and joins the translated segments with a space.
text on error rather than throwing.
Internal Methods (Implementation Detail)
These private methods are not part of the public API but are described here for contributors and advanced integrators.requestTranslation(text) (private)
Issues the actual HTTP GET to MyMemory and extracts the translated string from the response.
Endpoint:
text if the response is absent, if the API returns "QUERY LENGTH LIMIT EXCEEDED", or if any error occurs.
splitForApi(text) (private)
Splits text at sentence boundaries (., !, ?) to produce chunks each no longer than 450 characters.
Algorithm:
- Normalises whitespace.
- Splits on sentence-terminal punctuation using a regex.
- Accumulates sentences into the current chunk; when adding the next sentence would exceed 450 characters, flushes the current chunk and starts a new one.
- If a single sentence is itself longer than 450 characters, delegates to
splitLongText().
splitLongText(text) (private)
Splits an individual oversized string on word boundaries, accumulating words until the next word would exceed the 450-character limit, then flushing to a new chunk.
API Reference
| Parameter | Value |
|---|---|
| Endpoint | https://api.mymemory.translated.net/get |
Query param q | URL-encoded source text |
Query param langpair | en|es (English → Spanish) |
Max q length | 450 characters |
| Response field | responseData.translatedText |
MyMemory’s free tier allows approximately 5,000 words per day per IP address (anonymous usage). In a typical TriviaPP session, climate strings are short (under 50 characters each), so the limit is unlikely to be hit during normal play. If the daily limit is reached,
requestTranslation() returns the original English text, and the question will display untranslated values rather than failing.Usage in Context
TriviaService calls translate() directly when building planet climate questions:
"arid" → "Árido" and "temperate, tropical" → "Templado, Tropical" are produced this way before being assembled into the answer options array.