cmark-translate 0.2.2

Translate CommonMark using DeepL API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// SPDX-License-Identifier: MIT
//!
//! DeepL REST API wrapper
//!

pub struct Deepl {
    config: DeeplConfig,
}

impl Deepl {
    // New DeepL instance from default config file (deepl.toml or ~/.deepl.toml)
    pub fn new() -> std::io::Result<Self> {
        let deepl_config = DeeplConfig::new()?;

        Ok(Self {
            config: deepl_config,
        })
    }

    /// New DeepL instance from specific config file
    pub fn with_config<P: AsRef<std::path::Path>>(config_path: P) -> std::io::Result<Self> {
        let deepl_config = DeeplConfig::with_config(config_path)?;

        Ok(Self {
            config: deepl_config,
        })
    }

    /// Translate single text string
    #[allow(dead_code)]
    pub async fn translate(
        &self,
        from_lang: Language,
        to_lang: Language,
        formality: Formality,
        body: &str,
    ) -> reqwest::Result<String> {
        let mut result = self
            .translate_strings(from_lang, to_lang, formality, &vec![body])
            .await?;
        if 0 < result.len() {
            Ok(result.swap_remove(0))
        } else {
            // Empty response
            Ok(String::new())
        }
    }

    pub async fn translate_strings(
        &self,
        from_lang: Language,
        to_lang: Language,
        formality: Formality,
        body: &Vec<&str>,
    ) -> reqwest::Result<Vec<String>> {
        let mut params = vec![
            ("source_lang", from_lang.as_langcode()),
            ("target_lang", to_lang.as_langcode()),
            ("preserve_formatting", "1"),
            ("formality", formality.to_str()),
        ];
        if let Some(glossary_id) = self.config.glossary(from_lang, to_lang) {
            log::debug!("Use glossary {}", glossary_id);
            params.push(("glossary_id", glossary_id));
        }

        // add texts to be translated
        for t in body {
            params.push(("text", *t));
        }

        // Make DeepL API request
        let client = reqwest::Client::new();
        let resp = client
            .post(self.config.endpoint("translate"))
            .header(
                "authorization",
                format!("DeepL-Auth-Key {}", self.config.api_key),
            )
            .form(&params)
            .send()
            .await?;

        // Returns error
        resp.error_for_status_ref()?;

        // Parse response
        let deepl_resp = resp.json::<DeeplTranslationResponse>().await?;
        Ok(deepl_resp
            .translations
            .into_iter()
            .map(|t| t.text)
            .collect())
    }

    /// Translate XML string
    pub async fn translate_xml(
        &self,
        from_lang: Language,
        to_lang: Language,
        formality: Formality,
        xml_body: &str,
    ) -> reqwest::Result<String> {
        // Prepare request parameters
        let mut params = vec![
            ("source_lang", from_lang.as_langcode()),
            ("target_lang", to_lang.as_langcode()),
            ("preserve_formatting", "1"),
            ("formality", formality.to_str()),
            ("tag_handling", "xml"),
            ("ignore_tags", "header,embed,object"),
            (
                "splitting_tags",
                "blockquote,li,dt,dd,p,h1,h2,h3,h4,h5,h6,th,td",
            ),
            ("non_splitting_tags", "embed,em,strong,del,a,img"),
        ];
        if let Some(glossary_id) = self.config.glossary(from_lang, to_lang) {
            log::debug!("Use glossary {}", glossary_id);
            params.push(("glossary_id", glossary_id));
        }
        params.push(("text", xml_body));

        // Make DeepL API request
        let client = reqwest::Client::new();
        let resp = client
            .post(self.config.endpoint("translate"))
            .header(
                "authorization",
                format!("DeepL-Auth-Key {}", self.config.api_key),
            )
            .form(&params)
            .send()
            .await?;

        // Returns error
        resp.error_for_status_ref()?;

        // Parse response
        let mut deepl_resp = resp.json::<DeeplTranslationResponse>().await?;
        if 0 < deepl_resp.translations.len() {
            Ok(deepl_resp.translations.swap_remove(0).text)
        } else {
            // Empty response
            Ok(String::new())
        }
    }

    /// Register new glossary
    pub async fn register_glossaries<S: AsRef<str>>(
        &self,
        name: &str,
        from_lang: Language,
        to_lang: Language,
        glossaries: &[(S, S)],
    ) -> reqwest::Result<DeeplGlossary> {
        // Remove spaces, empty items
        let mut filtered_glossaries = glossaries
            .iter()
            .filter_map(|(from, to)| {
                let from_trimed = from.as_ref().trim();
                let to_trimed = to.as_ref().trim();
                if from_trimed.is_empty() || to_trimed.is_empty() {
                    None
                } else {
                    Some((from, to))
                }
            })
            .collect::<Vec<_>>();

        // Check duplicates
        filtered_glossaries.sort_by(|(from1, _), (from2, _)| from1.as_ref().cmp(from2.as_ref()));
        filtered_glossaries.iter().fold("", |prev_key, (from, _)| {
            let key = from.as_ref();
            if prev_key == key {
                // Duplicated
                log::warn!("Duplicated key : \"{}\"", key);
            }
            key
        });

        // Make TSV text
        let tsv: String = filtered_glossaries
            .iter()
            .map(|(from, to)| {
                let row = format!("{}\t{}", from.as_ref(), to.as_ref());
                log::trace!("TSV: {}", row);
                row
            })
            .collect::<Vec<String>>()
            .join("\n");

        // Make DeepL API request
        let client = reqwest::Client::new();
        let resp = client
            .post(self.config.endpoint("glossaries"))
            .header(
                "authorization",
                format!("DeepL-Auth-Key {}", self.config.api_key),
            )
            .form(&[
                ("name", name),
                ("source_lang", from_lang.as_langcode()),
                ("target_lang", to_lang.as_langcode()),
                ("entries_format", "tsv"),
                ("entries", &tsv),
            ])
            .send()
            .await?;

        if let Err(err) = resp.error_for_status_ref() {
            // Returns error with printing details
            if let Ok(err_body_text) = resp.text().await {
                log::error!("{}", err_body_text);
            }
            Err(err)
        } else {
            // Success, parse response
            let deepl_resp = resp.json::<DeeplGlossary>().await?;
            Ok(deepl_resp)
        }
    }

    /// List registered glossaries
    pub async fn list_glossaries(&self) -> reqwest::Result<Vec<DeeplGlossary>> {
        // Make DeepL API request
        let client = reqwest::Client::new();
        let resp = client
            .get(self.config.endpoint("glossaries"))
            .header(
                "authorization",
                format!("DeepL-Auth-Key {}", self.config.api_key),
            )
            .send()
            .await?;

        // Returns error
        resp.error_for_status_ref()?;

        // Parse response
        let deepl_resp = resp.json::<DeeplListGlossariesResponse>().await?;
        Ok(deepl_resp.glossaries)
    }

    /// Remove registered glossaries
    pub async fn remove_glossary(&self, id: &str) -> reqwest::Result<()> {
        // Make DeepL API request
        let client = reqwest::Client::new();
        let resp = client
            .delete(self.config.endpoint(&format!("glossaries/{}", id)))
            .header(
                "authorization",
                format!("DeepL-Auth-Key {}", self.config.api_key),
            )
            .send()
            .await?;

        // Check response
        resp.error_for_status()?;

        Ok(())
    }

    /// Get usage, returns translated characters
    pub async fn get_usage(&self) -> reqwest::Result<i32> {
        // Make DeepL API request
        let client = reqwest::Client::new();
        let resp = client
            .get(self.config.endpoint("usage"))
            .header(
                "authorization",
                format!("DeepL-Auth-Key {}", self.config.api_key),
            )
            .send()
            .await?;

        // Returns error
        resp.error_for_status_ref()?;

        // Parse response
        let deepl_resp = resp.json::<DeeplUsageResponse>().await?;
        Ok(deepl_resp.character_count)
    }
}

#[derive(Clone, Copy, serde::Deserialize)]
pub enum Language {
    De,
    Es,
    En,
    Fr,
    It,
    Ja,
    Nl,
    Pt,
    PtBr,
    Ru,
}

impl Language {
    pub fn as_langcode(&self) -> &'static str {
        match self {
            Self::De => "de",
            Self::Es => "es",
            Self::En => "en",
            Self::Fr => "fr",
            Self::It => "it",
            Self::Ja => "ja",
            Self::Nl => "nl",
            Self::Pt => "pt-br",
            Self::PtBr => "pt-br",
            Self::Ru => "ru",
        }
    }
}

impl std::str::FromStr for Language {
    type Err = std::io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let lowcase = s.to_ascii_lowercase();
        match lowcase.as_str() {
            "de" => Ok(Self::De),
            "es" => Ok(Self::Es),
            "en" => Ok(Self::En),
            "fr" => Ok(Self::Fr),
            "it" => Ok(Self::It),
            "ja" => Ok(Self::Ja),
            "nl" => Ok(Self::Nl),
            "pt" => Ok(Self::Pt),
            "pt-br" => Ok(Self::PtBr),
            "ru" => Ok(Self::Ru),
            _ => Err(std::io::Error::from(std::io::ErrorKind::InvalidInput)),
        }
    }
}

/// Translation output formality
#[derive(Clone, Copy, serde::Deserialize)]
pub enum Formality {
    Default,
    Formal,
    Informal,
}

impl Formality {
    pub fn to_str(&self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::Formal => "prefer_more",
            Self::Informal => "prefer_less",
        }
    }
}

impl Default for Formality {
    fn default() -> Self {
        Self::Default
    }
}

impl std::str::FromStr for Formality {
    type Err = std::io::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let lowcase = s.to_ascii_lowercase();
        match lowcase.as_str() {
            "default" => Ok(Self::Default),
            "formal" => Ok(Self::Formal),
            "informal" => Ok(Self::Informal),
            _ => Err(std::io::Error::from(std::io::ErrorKind::InvalidInput)),
        }
    }
}

#[derive(serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
struct DeeplConfig {
    api_key: String,
    glossaries: std::collections::HashMap<String, String>,
}

impl DeeplConfig {
    // Search default config file
    fn new() -> std::io::Result<Self> {
        use std::path::PathBuf;
        let config_files = [
            PathBuf::new().join("deepl.toml"),
            dirs::home_dir()
                .unwrap_or(PathBuf::new())
                .join(".deepl.toml"),
        ];

        for config_file in config_files {
            match Self::with_config(&config_file) {
                Ok(conf) => {
                    log::debug!("Read config file {:?}", config_file);
                    return Ok(conf);
                }
                Err(err) => {
                    if err.kind() == std::io::ErrorKind::NotFound {
                        log::debug!("Config file {:?} NOT found.", &config_file);
                    } else {
                        // Other err, stop searching
                        log::error!("Can not parse config file {:?} : {:?}", &config_file, err);
                        return Err(err);
                    }
                }
            }
        }

        // Config file not found
        Err(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "deepl.toml NOT found",
        ))
    }

    // Config from specific file
    fn with_config<P: AsRef<std::path::Path>>(config_path: P) -> std::io::Result<Self> {
        use std::io::Read;
        let mut file = std::fs::File::open(&config_path)?;

        // Read .deepl as TOML
        let mut config = String::new();
        file.read_to_string(&mut config)?;
        let deepl_config: DeeplConfig = toml::from_str(&config)?;

        Ok(deepl_config)
    }

    // DeepL endpoint URL
    fn endpoint(&self, api: &str) -> String {
        if self.api_key.ends_with(":fx") {
            // API free plan key
            format!("https://api-free.deepl.com/v2/{}", api)
        } else {
            // API Pro key
            format!("https://api.deepl.com/v2/{}", api)
        }
    }

    // Find glossary
    fn glossary<'a>(&'a self, from_lang: Language, to_lang: Language) -> Option<&'a str> {
        let glossary_key = format!("{}_{}", from_lang.as_langcode(), to_lang.as_langcode());
        self.glossaries.get(&glossary_key).map(|v| v.as_str())
    }
}

/// DeepL translation response JSON
#[derive(serde::Deserialize)]
#[serde(rename_all = "snake_case")]
struct DeeplTranslationResponse {
    translations: Vec<DeeplTranslationResponseInner>,
}

/// DeepL response JSON for each translations
#[derive(serde::Deserialize)]
#[serde(rename_all = "snake_case")]
struct DeeplTranslationResponseInner {
    #[allow(dead_code)]
    detected_source_language: String,
    text: String,
}

/// DeepL list glossaries response JSON
#[derive(serde::Deserialize)]
#[serde(rename_all = "snake_case")]
struct DeeplListGlossariesResponse {
    glossaries: Vec<DeeplGlossary>,
}

/// DeepL response JSON for each glossaries
#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub struct DeeplGlossary {
    pub glossary_id: String,
    pub name: String,
    pub ready: bool,
    pub source_lang: String,
    pub target_lang: String,
    pub creation_time: String,
    pub entry_count: i32,
}

/// DeepL usage response JSON
#[derive(serde::Deserialize)]
#[serde(rename_all = "snake_case")]
struct DeeplUsageResponse {
    character_count: i32,
    #[allow(dead_code)]
    character_limit: i32,
}

#[cfg(test)]
mod test {
    use super::*;

    #[tokio::test]
    async fn plain_text_translation() {
        let deepl = Deepl::new().unwrap();

        let resp = deepl
            .translate(
                Language::En,
                Language::De,
                Formality::Default,
                "Hello, World!",
            )
            .await
            .unwrap();
        assert_eq!(&resp, "Hallo, Welt!");
    }
}