baidu_trans

Crates.iobaidu_trans
lib.rsbaidu_trans
version0.7.5
sourcesrc
created_at2022-11-04 17:04:42.531403
updated_at2022-11-07 12:16:54.502113
description百度翻译API
homepagehttps://github.com/FengYouJun520/baidu_trans
repositoryhttps://github.com/FengYouJun520/baidu_trans
max_upload_size
id705294
size198,947
风游君 (FengYouJun520)

documentation

https://docs.rs/baidu_trans

README

baidu_trans

百度翻译API

Install

[dependencies]
baidu_trans =  { version = "0.7.5", features = [] }
anyhow = "1.0.66"
dotenv = "0.15.0"

examples

blocking feature:


use baidu_trans::{blocking::Client, config::Config, lang::Lang};

fn main() -> anyhow::Result<()> {
    dotenv::dotenv()?;
    let app_id = dotenv::var("APP_ID")?;
    let app_secret = dotenv::var("APP_SECRET")?;

    let client = Client::new(Config::new(app_id, app_secret));

    client.lang(Lang::Auto, Lang::Zh);
    let resp = client.translate(
        "As we discussed in Chapter 1, Hello Rust!, stack variables are preferred thanks to
their low overhead and speed compared to heap-allocated data, which
automatically introduces overhead thanks to the necessary heap pointer.",
    )?;

    if resp.error_code.is_some() {
        return Err(anyhow::Error::msg(resp.error_msg.unwrap()));
    }

    dbg!(resp);

    Ok(())
}

aio feature

in cargo.toml:

[dependencies]
anyhow = "1.0.66"
baidu_trans =  { version = "0.7.5", features = [ "aio"] }
tokio = { version = "1.21.2", features = ["full"] }
dotenv = "0.15.0"
use baidu_trans::{aio::Client, config::Config, lang::Lang};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenv::dotenv()?;
    let app_id = dotenv::var("APP_ID")?;
    let app_secret = dotenv::var("APP_SECRET")?;

    let client = Client::new(Config::new(app_id, app_secret));

    client.lang(Lang::Auto, Lang::Zh);
    let resp = client
        .translate(
            "As we discussed in Chapter 1, Hello Rust!, stack variables are preferred thanks to
their low overhead and speed compared to heap-allocated data, which
automatically introduces overhead thanks to the necessary heap pointer.",
        )
        .await?;

    if resp.error_code.is_some() {
        return Err(anyhow::Error::msg(resp.error_msg.unwrap()));
    }

    dbg!(resp);

    Ok(())
}

features

  • blocking: 默认特性
  • aio: 启动async/await功能支持
  • image: 启用图片翻译功能
  • domain: 启用垂直领域翻译功能
  • doc:启用文档翻译功能
Commit count: 28

cargo fmt