rtranslate 0.1.1

A simple, dependency-free Rust wrapper for Google Translate public web API
Documentation
  • Coverage
  • 45.45%
    5 out of 11 items documented4 out of 5 items with examples
  • Size
  • Source code size: 17.74 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.86 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 30s Average build duration of successful builds.
  • all releases: 23s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ntdat104/rtranslate
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ntdat104

🦀 rtranslate

A simple, dependency-free Rust wrapper for the Google Translate public web API — built using only the Rust standard library.


✨ Features

  • 🔹 No external dependencies — uses std::process::Command with curl.
  • 🔹 Supports single and batch translations.
  • 🔹 Automatically detects the source language (sl=auto).
  • 🔹 Graceful error handling for empty responses or rate limits.
  • 🔹 Ready to use as a library or CLI example.

📦 Installation

Option 1 — Use directly from GitHub

cargo add --git https://github.com/ntdat104/rtranslate

Option 2 — Clone manually

git clone https://github.com/ntdat104/rtranslate.git
cd rtranslate
cargo build

🧰 Usage

Single translation

use rtranslate::translate;

fn main() {
    match translate("Hello world", "auto", "vi") {
        Ok(result) => println!("Translated: {}", result),
        Err(err) => eprintln!("Error: {}", err),
    }
}

Output:

Translated: Xin chào thế giới

Batch translation

use rtranslate::translate_vec;

fn main() {
    let phrases = ["Good morning", "How are you?", "Rust is amazing!"];
    let results = translate_vec(&phrases, "auto", "vi");

    for (i, res) in results.iter().enumerate() {
        match res {
            Ok(t) => println!("{}{}", phrases[i], t),
            Err(e) => println!("{} → ERROR: {}", phrases[i], e),
        }
    }
}

Example output:

Good morning → Chào buổi sáng
How are you? → Bạn khỏe không?
Rust is amazing! → Rust thật tuyệt vời!

🧪 Tests

You can run all unit and integration tests via:

cargo test

This will:

  • Validate URL encoding and parsing
  • Check translation parsing logic
  • Optionally perform real translation requests (requires internet)

▶️ Examples

Run the included usage example:

cargo run --example basic

examples/basic.rs:

use rtranslate::{translate, translate_vec};

fn main() {
    println!("--- Single Example ---");
    println!("{:?}", translate("Rust is fast", "auto", "vi"));

    println!("
--- Batch Example ---");
    let texts = ["Good morning", "Good night", "Have fun!"];
    let results = translate_vec(&texts, "auto", "vi");
    for (i, res) in results.iter().enumerate() {
        println!("{}{:?}", texts[i], res);
    }
}

⚠️ Notes

  • This library uses Google’s unofficial translate endpoint (translate.googleapis.com/translate_a/single).
  • There are no API keys required, but Google may rate-limit you for frequent requests.
  • Recommended for small translation workloads, CLI tools, or quick utilities.

🪪 License

MIT License © 2025 Tien Dat (ntdat104)


💡 Contributing

Pull requests are welcome!
Feel free to:

  • Add new language tests
  • Improve JSON parsing
  • Add async support (with hyper or reqwest)

🌍 Example Result (EN → VI)

Input Output
Hello world Xin chào thế giới
How are you? Bạn khỏe không?
Rust is great! Rust thật tuyệt vời!

rtranslate — lightweight, offline-friendly, and open to everyone. 🦀