kmp-project-template icon indicating copy to clipboard operation
kmp-project-template copied to clipboard

feat: add translation script and GitHub Action for mobile i18n autofill

Open markrizkalla opened this issue 10 months ago • 6 comments

  • Create translate.py to automate Android string resource translations using the Google Gemini API, featuring placeholder/markup preservation, change detection via snapshots, and batch processing.
  • Add .github/workflows/mobile-i18n-autofill-pr.yml to trigger translation autofill on pull requests to the development branch.
  • Support translation for 24 locales using the gemma-3-27b-it model.
  • Implement automated validation of translated Android resources and direct commits of updates to PR branches.

Summary by CodeRabbit

  • New Features

    • Automated mobile localization: PRs touching Android resources trigger AI-assisted translations for multiple locales, validate generated resources, preserve formatting/placeholders/comments, and provide a concise summary.
    • Local/CLI translation tool: run end-to-end Android string translations locally with snapshotting, validation, placeholder safety, and merge-ready outputs.
  • Chores

    • CI workflow added to autofill translations on PRs and push standardized commits back to the PR branch when changes occur.

markrizkalla avatar Jan 31 '26 20:01 markrizkalla

[!NOTE]

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • [ ] ▶️ Resume reviews
  • [ ] 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds a GitHub Actions workflow and a new translate.py tool that auto-discovers Android strings.xml, uses Gemini to generate translations for specified locales with snapshot-driven diffs, validates resources via Gradle, and commits translation updates back to the PR head when produced. (33 words)

Changes

Cohort / File(s) Summary
GitHub Actions Workflow
.github/workflows/mobile-i18n-autofill-pr.yml
New workflow triggered on PRs to dev for cmp-android/**, feature/**, the workflow file, or translate.py. Checks out PR head, sets up JDK21 & Python3.11, installs deps, runs translate.py --apply with locales/model/batch-size, validates with Gradle, and commits/pushes translation changes if present.
Translation Engine & CLI
translate.py
New, comprehensive translator: GeminiTranslator (batch/single, retries, backoff, throttling), dataclasses and exceptions, snapshot-driven change detection, placeholder/tag freeze/unfreeze, XML read/write preserving comments/attributes, sanitization/validation, create/merge locale files, summary reporting, and a CLI with mode/locales/model options.
Documentation
docs/TRANSLATE.md
New documentation describing usage, CLI flags, behaviors (apply/check), implementation details (snapshots, placeholders, rate limiting), and requirements for translate.py.
Workflow Targets / Android resources
cmp-android/..., feature/...
Workflow scans and may modify strings.xml under these directories; generated locale XMLs are validated via ./gradlew :cmp-android:processDemoDebugResources before being committed to the PR branch.

Sequence Diagram

sequenceDiagram
    participant PR as "GitHub PR"
    participant GHA as "GitHub Actions"
    participant Script as "translate.py"
    participant API as "Gemini API"
    participant FS as "Filesystem/Repo"
    participant Gradle as "Gradle"

    PR->>GHA: PR created/updated (paths matched)
    GHA->>GHA: Checkout PR head, setup JDK21 & Python3.11
    GHA->>Script: Invoke translate.py (--apply, locales, model)
    Script->>FS: Discover source `strings.xml`, load snapshots
    FS-->>Script: Return source entries
    Script->>Script: Freeze placeholders/tags, batch requests
    Script->>API: Send batch translation requests
    API-->>Script: Return translated frozen texts
    Script->>Script: Unfreeze, validate, sanitize translations
    Script->>FS: Write/merge locale XMLs and update snapshots
    Script-->>GHA: Return has_changes flag
    GHA->>Gradle: Run resource validation task
    Gradle-->>GHA: Return validation result
    alt has_changes == true
        GHA->>FS: Commit & push translation changes to PR head
    end

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Poem

🐰 I hopped through strings with gentle care,
Froze tags and placeholders in the air,
Sent batches to Gemini’s bright machine,
Unwrapped them neat, XML kept clean,
Pushed translations home — a small rabbit’s scene.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.01% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: adding a translation script and GitHub Action for mobile i18n autofill.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Jan 31 '26 20:01 coderabbitai[bot]

@coderabbitai full review.

biplab1 avatar Feb 04 '26 14:02 biplab1

@biplab1, I will conduct a full review of the PR including the translation script and GitHub Actions workflow.

✅ Actions performed

Full review triggered.

coderabbitai[bot] avatar Feb 04 '26 14:02 coderabbitai[bot]

@niyajali If everything looks good on your end, feel free to merge this.

biplab1 avatar Feb 05 '26 14:02 biplab1

@markrizkalla Can you please mark all the conversations as resolved?

biplab1 avatar Feb 05 '26 14:02 biplab1

@markrizkalla, thanks for your pull request, and @biplab1, thank you for the review. Unfortunately, I can't merge it yet as it requires some improvements. For instance, the current workflow hardcodes specific locales for translation generation. This will cause issues if the project doesn't support these locales or if additional locales are added. Additionally, we need to trigger this workflow based on user preference rather than on every pull request.

You are welcome to address other matters, and I will handle this when my schedule allows. Thank you!

niyajali avatar Feb 05 '26 17:02 niyajali