Crate baidu_trans

Crate baidu_trans 

Source
Expand description

百度翻译SDK

§支持的功能

  • 文本翻译
  • 图片翻译
  • 垂直领域翻译
  • 文档翻译

引入依赖:

[dependencies]
baidu_trans =  { version = "0.7.5", features = [] }

如果要使用async/await,需要添加aio features。

如果要支持图片翻译,需要添加image feature。

§基本用法

§使用阻塞的方式。

添加依赖:

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

默认启用的是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));

    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.",
    )?;

    assert_eq!(resp.error_code, None);

    Ok(())
}

§使用async/await方式

添加依赖:

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

需要在cargo.toml中添加aio features。

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));

    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?;

    assert_eq!(resp.error_code, None);

    dbg!(resp);

    Ok(())
}

§图片翻译

需要启用image feature.

[dependencies]
baidu_trans =  { version = "0.7.5", features = ["image"] }

使用方式

use baidu_trans::blocking::Client;
use baidu_trans::config::Config;
use baidu_trans::lang::Lang;
use std::fs;

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 data = fs::read("a.png")?;
    /// 图片名称必须填写
    let resp = client.image_translate("a.png", data)?;
    assert_eq!(resp.error_code, "0");
    Ok(())
}

§垂直领域翻译

需要启用domain feature.

[dependencies]
baidu_trans =  { version = "0.7.5", features = ["domain"] }

用法

use baidu_trans::blocking::Client;
use baidu_trans::config::Config;
use baidu_trans::domain::Domain;
use baidu_trans::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.domain_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.",
       Domain::Electronics,
   )?;

   dbg!(resp);
   Ok(())
}

Modules§

aio
异步API
blocking
阻塞API
config
客户端配置
constant
相关常量
document
文档翻译相关模块
domain
垂直领域翻译相关模块
image
图片翻译相关模块
lang
可翻译的语种列表
model
通用翻译相关模块
util
相关辅助函数