Skip to content

Conversation

@igorpecovnik
Copy link
Member

@igorpecovnik igorpecovnik commented Dec 25, 2025

Summary

This PR adds automated translation file synchronization with AI-powered translations using OpenAI API.

Features

  • Automatic Detection: Finds missing translation keys by comparing all locale files against en.json (source of truth)
  • AI Translation: Uses OpenAI API (gpt-4o-mini) for high-quality translations
  • Context-Aware: Provides section/key context to the AI for better, more accurate translations
  • Smart Prompts: Specialized prompts that understand technical UI terminology
  • Placeholder Preservation: Maintains i18next placeholders like {{count}} and {{boardName}}
  • Batch Processing: Translates in batches of 10 with rate limiting
  • Error Handling: Falls back to TODO: prefix if translation fails
  • GitHub Action: Runs on push, daily schedule, and manual trigger
  • Auto-PR: Creates pull requests automatically when updates are detected

Configuration

The workflow uses OpenAI API with:

  • OPENAI_API_KEY secret (required) - Get from platform.openai.com
  • OPENAI_MODEL variable (optional, default: gpt-4o-mini)
  • OPENAI_API variable (optional, default: https://api.openai.com/v1)

Translation Quality

OpenAI provides excellent translations with:

  • Understanding of technical terminology (when to keep "Flash", "SD card", etc. in English)
  • Context-aware translations based on section/key location
  • Natural, concise UI text appropriate for buttons and labels
  • Proper handling of plural forms and placeholders

Cost Estimation

Using gpt-4o-mini (default):

  • 10 keys: ~$0.00001
  • 50 keys: ~$0.00005
  • 100 keys: ~$0.0001

Very cost-effective for automated translation workflows.

Files Changed

  • .github/workflows/sync-locales.yml - GitHub Action workflow with OpenAI integration
  • scripts/sync-locales.js - AI translation sync script using OpenAI API
  • scripts/README.md - Complete documentation with setup instructions
  • src/locales/hr.json - Croatian locale (partial, for testing)

Testing

Added Croatian (hr) locale with partial translations to demonstrate the sync feature. When the workflow runs with an API key, it will automatically translate the missing 64 keys.


@igorpecovnik igorpecovnik changed the title 🌐 i18n: Add AI-powered translation sync automation i18n: Add AI-powered translation sync automation Dec 25, 2025
@igorpecovnik igorpecovnik added the status: in progress Currently being worked on label Dec 25, 2025
Adds automated translation file synchronization with AI-powered
translations using LibreTranslate API.

Features:
- Auto-detects missing translation keys from en.json source
- Translates new keys using LibreTranslate (free/public API)
- Preserves i18next placeholders like {{count}} and {{boardName}}
- Batch processing with rate limiting
- Falls back to TODO: prefix on translation failures
- GitHub Action runs daily and creates PRs automatically
- Supports custom LibreTranslate instances and API keys
Add push trigger to sync-locales workflow so translation files
are automatically updated whenever changes are pushed to the branch.
Fix bug where localeFiles array contained language codes without
.json extension, causing the script to attempt reading directories
instead of files.
@igorpecovnik igorpecovnik force-pushed the language branch 2 times, most recently from 19d9b7b to 72f8950 Compare December 26, 2025 10:14
Create partial Croatian translation file to test the auto-sync feature.
Only includes translations for home, flash, modal, and device sections
with missing keys to be filled by the sync script.

Add hr (Croatian) to the LOCALE_CODE_MAP for automated translation.
Replace LibreTranslate with OpenAI API for higher quality translations.

Benefits:
- Much better translation quality with context-aware AI
- Handles technical terminology appropriately
- Better placeholder preservation
- Configurable model selection (gpt-4o-mini default)
- Cost-effective with gpt-4o-mini
- No rate limiting issues

Changes:
- Use OpenAI Chat Completions API
- Add specialized system prompts for UI translation
- Provide section/key context for each translation
- Update workflow to use OPENAI_API_KEY secret
- Update documentation with OpenAI setup instructions
@igorpecovnik igorpecovnik changed the title i18n: Add AI-powered translation sync automation 🌐 i18n: Add AI-powered translation sync with OpenAI Dec 26, 2025
- Remove exit code 1 when translations are made
- The workflow already detects changes via git diff
- Clean up unused 'placeholders' variable
Create a minimal Croatian locale with only basic translations
to test the auto-sync feature.
- Change batch size from 10 to 1 request
- Increase delay from 1s to 22s between requests
- Add progress indicator for long-running translations
- This respects OpenAI free tier limit of 3 requests/minute

For faster translation, add a payment method to your OpenAI account
to increase rate limits.
Add support for different rate limits based on OpenAI account tier:
- Free tier: 1 request/22s (3 RPM limit)
- Paid tier: 50 requests/1s (500 RPM limit)

Changes:
- Add OPENAI_TIER environment variable (free/paid)
- Auto-adjust batch size and delays based on tier and model
- Show which tier is being used with helpful tips
- Update documentation with rate limit comparison table

Users can now set OPENAI_TIER=paid for 20-50x faster translations
after adding a payment method to their OpenAI account.
Add ability to retry translations that previously failed (marked with TODO:).

Changes:
- Add collectFailedTranslations() function to find TODO: keys
- Add RETRY_FAILED environment variable (default: false)
- Track and report retry statistics in summary
- Update workflow and documentation with retry instructions

Usage:
  RETRY_FAILED=true node scripts/sync-locales.js

This is useful when:
- Rate limits caused previous failures
- API had temporary issues
- After upgrading to paid tier for faster retries
Correct rate limits based on OpenAI's documented tiers:
- Free tier: 5 RPM (12s delay) - was too conservative at 22s
- Paid Tier 1-2: 200 RPM (300ms delay, batch of 50)
- Paid Tier 3-5: 500 RPM (120ms delay, batch of 50)

This makes free tier ~2x faster (20 min vs 37 min for 100 keys)
and paid tier even faster with proper batching.

Also updated documentation to show that even a small  balance
gets you Tier 1-2 with 40x speedup over free tier.
Your account has a strict 3 RPM limit on free tier.
Updated to use 21 second delay between requests to stay safely under the limit.

Free tier: ~35 min for 100 keys (at 3 RPM)
Paid tier: ~1 min for 100 keys (at 200 RPM) - 65x faster

Recommendation: Add just  billing to get 65x speedup.
@igorpecovnik igorpecovnik changed the title 🌐 i18n: Add AI-powered translation sync with OpenAI i18n: Add AI-powered translation sync with OpenAI Dec 26, 2025
Only run on manual dispatch and daily schedule to avoid
triggering on every push to the branch.
Remove the hr.json test file that was used for testing
the sync feature. Clean up the supported languages list.
@igorpecovnik igorpecovnik added status: ready for review Ready to be reviewed and removed status: in progress Currently being worked on labels Dec 26, 2025
@igorpecovnik igorpecovnik merged commit f6e23ab into main Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: ready for review Ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants