IdiomasApp es un backend de traducción y análisis lingüístico construido con TypeScript y el runtime de Bun. Expone un servicio HTTP con Elysia.js que usa validaciones con Zod y se integra con la API de OpenAI (modelos Qwen) para traducir, segmentar y explicar gramática en varios idiomas.
- TypeScript sobre Bun.js como runtime.
- Elysia.js para definir el servidor y las rutas.
- Zod y los esquemas de Elysia para validar solicitudes y respuestas.
- OpenAI API usando modelos Qwen para traducción, glosa interlineal y puntos gramaticales.
Los códigos ISO 639-1 aceptados se definen en src/schemas/languages.ts:
- Español (
es) - Inglés (
en) - Portugués (
pt) - Chino (
zh) - Vietnamita (
vi)
GET /
Respuesta de saludo simple para verificar que el servicio está vivo.GET /languages
Devuelve el listado de idiomas soportados.POST /translate/:grammar?
Traduce el texto recibido entextdesdel2hacial1, devuelve texto traducido y glosas morfema a morfema. Cuando:grammartiene cualquier valor, agrega puntos gramaticales. Sil2es chino (zh), la glosa incluye segmentacion en hanzi y pinyin.
- Asegúrate de tener Bun instalado y disponible en el
PATH. - Desde la raíz del proyecto ejecuta:
./start-backend.sh
- El script solicitará las variables de entorno necesarias si no existen:
AI_KEY(se solicita de forma oculta)AI_BASE_URLAI_MODEL
- El servidor se inicia en
http://localhost:3000y acepta solicitudes desdehttp://localhost:5173gracias a la configuración de CORS ensrc/index.ts.
POST /translate
{
"text": "Esto es un ejemplo simple.",
"l1": "es",
"l2": "en"
}- Instala dependencias (si es necesario):
bun install - Inicia el servidor:
bun run src/index.tso usa./start-backend.shpara que el script gestione las variables de entorno.
IdiomasApp is a translation and linguistic analysis backend built with TypeScript on the Bun runtime. It exposes an HTTP service with Elysia.js, validates inputs with Zod, and integrates with the OpenAI API (Qwen models) to translate, gloss, and extract grammar points across multiple languages.
- TypeScript on Bun.js runtime.
- Elysia.js to define the server and routes.
- Zod and Elysia schemas to validate requests and responses.
- OpenAI API using Qwen models for translation, interlinear glossing, and grammar extraction.
ISO 639-1 codes defined in src/schemas/languages.ts:
- Spanish (
es) - English (
en) - Portuguese (
pt) - Chinese (
zh) - Vietnamese (
vi)
GET /
Simple greeting to confirm the service is running.GET /languages
Returns the list of supported languages.POST /translate/:grammar?
Translates thetextfield froml2intol1, returning translated sentences and morpheme-by-morpheme glosses. When:grammarhas any value, grammar points are included. Ifl2is Chinese (zh), the gloss includes hanzi segmentation and pinyin.
- Ensure Bun is installed and available in your
PATH. - From the project root, run:
./start-backend.sh
- The script will prompt for required environment variables if they are missing:
AI_KEY(prompted silently)AI_BASE_URLAI_MODEL
- The server starts at
http://localhost:3000and accepts requests fromhttp://localhost:5173via the CORS configuration insrc/index.ts.
POST /translate
{
"text": "This is a simple example.",
"l1": "en",
"l2": "es"
}- Install dependencies (if needed):
bun install - Start the server:
bun run src/index.tsor use./start-backend.shto handle environment variables automatically.
- Local: ``
Protected endpoints require a short-lived access token obtained via Play Integrity attestation.
- Call
POST /auth/attestwith a Play Integrity token. - Use the returned token in
Authorization: Bearer <token>for protected routes.
Tokens expire after 15 minutes.
Simple health check.
Response:
Hello Language Enthusiast!
Returns the supported ISO 639-1 language codes.
Headers:
Authorization: Bearer <token>
Response:
{
"languages": ["spanish", "english", "portuguese", "chinese", "vietnamese"]
}Exchanges a Play Integrity token for a short-lived API token.
Request body:
{
"integrityToken": "<PLAY_INTEGRITY_TOKEN>"
}Response:
{
"token": "<JWT_TOKEN>",
"expiresIn": 900
}Error response (invalid attestation):
{
"error": "Attestation failed"
}Translates from l2 (source text language) to l1 (student native language) and returns interlinear glosses. If :grammar has any value (for example /translate/1 or /translate/grammar), grammar points are included. If l2 is zh, the gloss includes hanzi segmentation and pinyin.
Headers:
Authorization: Bearer <token>
Request body:
{
"text": "This is a simple example.",
"l1": "en",
"l2": "es"
}Response (alphabetic languages):
{
"request_id": "0195a0f2-0a5f-7ab8-9f19-4c34f99a6f7e",
"translatedText": ["This is a simple example."],
"glossedText": [
{
"originalText": ["esto", "es", "un", "ejemplo", "simple"],
"glossedWords": ["this", "is", "a", "example", "simple"]
}
]
}Error response (missing/invalid token):
{
"error": "Missing or invalid Authorization header"
}Response with grammar points (/translate/1):
{
"request_id": "0195a0f2-0a5f-7ab8-9f19-4c34f99a6f7e",
"translatedText": ["This is a simple example."],
"glossedText": [
{
"originalText": ["esto", "es", "un", "ejemplo", "simple"],
"glossedWords": ["this", "is", "a", "example", "simple"]
}
],
"grammarPoints": {
"points": [
{
"grammar_point": "Ser vs estar",
"sentence": "Esto es un ejemplo simple.",
"explanation": "The verb ser is used for inherent characteristics..."
}
]
}
}Response when l2 is zh:
{
"request_id": "0195a0f2-0a5f-7ab8-9f19-4c34f99a6f7e",
"translatedText": ["Hello world"],
"glossedText": [
{
"separateWords": ["你好", "世界"],
"pinyin": ["ni hao", "shi jie"],
"glossedWords": ["hello", "world"]
}
]
}textmust be between 1 and 450 characters after trimming and collapsing whitespace.l1andl2must be different and must match supported ISO 639-1 codes.- The detected language of
textmust matchl2(otherwise the request is rejected).
AI_KEY: API key for the LLM provider.AI_BASE_URL: Base URL for the LLM provider.AI_MODEL: Model used for glossing and grammar extraction (defaults toqwen3-max).MINIMAX_API_KEY: API key for MiniMax async TTS.MINIMAX_API_BASE_URL: Optional MiniMax base URL (defaults tohttps://api.minimax.io).MINIMAX_TTS_MODEL: Optional MiniMax TTS model override (defaults tospeech-2.8-turbo).MINIMAX_VOICE_EN|ES|PT|ZH|VI: Optional voice overrides per source language.PLAY_INTEGRITY_PACKAGE_NAME: Android package name used for Play Integrity validation.GOOGLE_SERVICE_ACCOUNT_JSON: Service account JSON (stringified) with Play Integrity access.ATTESTATION_JWT_SECRET: Secret used to sign short-lived access tokens.
See the standalone spec in openapi.yaml.