pub struct DeepLX { /* private fields */ }Expand description
The main entry point for interacting with the DeepL translation service.
DeepLX provides methods to create a translation client and perform translation
requests. You can optionally specify a proxy (on non‑wasm32 targets),
choose source and target languages, and retrieve alternative translations.
Implementations§
Source§impl DeepLX
impl DeepLX
Sourcepub fn new(config: Config) -> Self
pub fn new(config: Config) -> Self
Constructs a new DeepLX instance.
§Parameters
Config- Configuration settings for the translation client.
§Panics
This method will panic if the provided proxy string is invalid.
§Examples
use deeplx::{Config, DeepLX};
let translator = DeepLX::new(Config::default());
let translator_with_proxy = DeepLX::new(Config {
#[cfg(not(target_arch = "wasm32"))]
proxy: Some("http://pro.xy".to_string()),
..Default::default()
});Sourcepub async fn translate(
&self,
source_lang: &str,
target_lang: &str,
text: &str,
deepl_session: Option<&str>,
) -> Result<DeepLXTranslationResult, Error>
pub async fn translate( &self, source_lang: &str, target_lang: &str, text: &str, deepl_session: Option<&str>, ) -> Result<DeepLXTranslationResult, Error>
Translates the given text from a source language to a target language.
This method automatically handles splitting the text into translation jobs, detecting the source language (if set to “auto”), and returning the translated text.
§Parameters
source_lang- The source language code, e.g."en". Use"auto"to let the system detect the language.target_lang- The target language code, e.g."zh"or"EN-GB"for a regional variant.text- The text to translate. Cannot be empty.deepl_session- An optional session string. IfNone, the “Free” method is used; otherwise “Pro”.
§Returns
On success, returns a DeepLXTranslationResult containing the translated text and alternatives.
On failure, returns an Err containing the underlying error.
§Examples
use deeplx::{Config, DeepLX};
async fn run() {
let translator = DeepLX::new(Config::default());
match translator
.translate("auto", "zh", "Hello, world!", None)
.await {
Ok(res) => println!("Translated: {}", res.data),
Err(e) => eprintln!("Error: {}", e),
}
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() {
run().await;
}
#[cfg(target_arch = "wasm32")]
#[tokio::main(flavor = "current_thread")]
async fn main() {
run().await;
}