arb 0.1.0

Localize flutter apps with DeepL AI translations
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
use anyhow::{anyhow, Result};
use arb_lib::{
    deepl::{ApiOptions, DeeplApi, Lang, LanguageType},
    ArbFile, ArbKey, Intl, Invalidation, TranslationOptions,
};
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, HashMap},
    path::{Path, PathBuf},
};

use csv::{ReaderBuilder, Writer, WriterBuilder};

#[derive(Debug, Serialize, Deserialize)]
struct CsvRow {
    id: String,
    source: String,
    target: String,
    correction: String,
    comment: String,
}

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Arb {
    #[clap(subcommand)]
    cmd: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Translate the template to a language.
    #[clap(alias = "tl")]
    Translate {
        /// API key.
        #[clap(short, long, hide_env_values = true, env = "DEEPL_API_KEY")]
        api_key: String,

        /// Invalidate all keys.
        #[clap(short, long)]
        force: bool,

        /// Invalidate specific keys.
        #[clap(short, long)]
        invalidate: Vec<String>,

        /// Directory of human-translated overrides.
        #[clap(long)]
        overrides: Option<PathBuf>,

        /// Translate and write to disc.
        #[clap(long)]
        apply: bool,

        /// File name prefix.
        #[clap(short, long)]
        name_prefix: Option<String>,

        /// Target language.
        #[clap(short, long)]
        lang: Lang,

        /// Localization YAML file.
        file: PathBuf,
    },
    /// Update existing translations.
    #[clap(alias = "up")]
    Update {
        /// API key.
        #[clap(short, long, hide_env_values = true, env = "DEEPL_API_KEY")]
        api_key: String,

        /// Invalidate all keys.
        #[clap(short, long)]
        force: bool,

        /// Invalidate specific keys.
        #[clap(short, long)]
        invalidate: Vec<String>,

        /// Directory of human-translated overrides.
        #[clap(long)]
        overrides: Option<PathBuf>,

        /// Translate and write to disc.
        #[clap(long)]
        apply: bool,

        /// File name prefix.
        #[clap(short, long)]
        name_prefix: Option<String>,

        /// Localization YAML file.
        file: PathBuf,
    },

    /// Print account usage.
    Usage {
        /// API key.
        #[clap(short, long, hide_env_values = true, env = "DEEPL_API_KEY")]
        api_key: String,
    },
    /// List language application resource bundles.
    #[clap(alias = "ls")]
    List {
        /// File name prefix.
        #[clap(short, long)]
        name_prefix: Option<String>,

        /// Localization YAML file.
        file: PathBuf,
    },
    /// Print supported languages.
    Languages {
        /// API key.
        #[clap(short, long, hide_env_values = true, env = "DEEPL_API_KEY")]
        api_key: String,

        /// Language type (source or target).
        #[clap(short, long, default_value = "source")]
        language_type: LanguageType,
    },
    /// Diff template with language(s).
    Diff {
        /// File name prefix.
        #[clap(short, long)]
        name_prefix: Option<String>,

        /// Languages to compare to the template language.
        #[clap(short, long)]
        languages: Vec<Lang>,

        /// Localization YAML file.
        file: PathBuf,
    },
    /// CSV comparison between template and a target language.
    Compare {
        /// File name prefix.
        #[clap(short, long)]
        name_prefix: Option<String>,

        /// Target language.
        #[clap(short, long)]
        lang: Lang,

        /// Directory of human-translated overrides.
        #[clap(long)]
        overrides: Option<PathBuf>,

        /// Output file for CSV document.
        #[clap(short, long)]
        output: Option<PathBuf>,

        /// Localization YAML file.
        file: PathBuf,
    },
    /// Import CSV corrections to an overrides JSON file.
    Import {
        /// File name prefix.
        #[clap(short, long)]
        name_prefix: Option<String>,

        /// Column delimiter character.
        #[clap(short, long, default_value = ",")]
        delimiter: char,

        /// Target language.
        #[clap(short, long)]
        lang: Lang,

        /// CSV comparison with corrections.
        #[clap(short, long)]
        input: PathBuf,

        /// Directory of human-translated overrides.
        #[clap(long)]
        overrides: Option<PathBuf>,

        /// Localization YAML file.
        file: PathBuf,
    },
}

#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
    tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::new(
            std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into()),
        ))
        .with(
            tracing_subscriber::fmt::layer()
                .with_target(false)
                .without_time(),
        )
        .init();

    let args = Arb::parse();
    match args.cmd {
        Command::Update {
            file,
            api_key,
            name_prefix,
            apply,
            force,
            invalidate,
            overrides,
        } => {
            let mut intl = new_intl(&file, name_prefix.clone())?;

            let overrides = overrides.or(intl.overrides_dir().map(PathBuf::from));
            let overrides = if let Some(dir) = &overrides {
                Some(intl.load_overrides(dir, None)?)
            } else {
                None
            };

            let translations = intl.list_translated()?;
            for lang in translations.keys() {
                if lang == intl.template_language() {
                    continue;
                }
                translate_language(
                    &mut intl,
                    *lang,
                    api_key.clone(),
                    apply,
                    force,
                    invalidate.clone(),
                    overrides.clone(),
                )
                .await?;
            }

            if !apply {
                tracing::warn!("dry run, use --apply to translate");
            }
        }

        Command::Translate {
            lang,
            file,
            api_key,
            name_prefix,
            apply,
            force,
            invalidate,
            overrides,
        } => {
            let mut intl = new_intl(&file, name_prefix.clone())?;

            let overrides = overrides.or(intl.overrides_dir().map(PathBuf::from));
            let overrides = if let Some(dir) = &overrides {
                Some(intl.load_overrides(dir, None)?)
            } else {
                None
            };

            translate_language(
                &mut intl, lang, api_key, apply, force, invalidate, overrides,
            )
            .await?;

            if !apply {
                tracing::warn!("dry run, use --apply to translate");
            }
        }
        Command::Usage { api_key } => {
            let options = ApiOptions::new(api_key);
            let api = DeeplApi::new(options);
            let usage = api.usage().await?;
            serde_json::to_writer_pretty(std::io::stdout(), &usage)?;
            println!();
        }
        Command::Languages {
            api_key,
            language_type,
        } => {
            let options = ApiOptions::new(api_key);
            let api = DeeplApi::new(options);
            let langs = api.languages(language_type).await?;
            serde_json::to_writer_pretty(std::io::stdout(), &langs)?;
        }
        Command::Diff {
            name_prefix,
            file,
            languages,
        } => {
            let mut output = BTreeMap::new();
            let intl = new_intl(file, name_prefix)?;
            let template = intl.template_content()?;
            for lang in languages {
                let lang_file = intl.load_or_default(lang)?;
                let diff = template.diff(&lang_file, intl.cache().get_file(&lang));
                output.insert(lang, diff);
            }
            serde_json::to_writer_pretty(std::io::stdout(), &output)?;
        }
        Command::List { file, name_prefix } => {
            let intl = new_intl(file, name_prefix)?;
            let output = intl.list_translated()?;
            serde_json::to_writer_pretty(std::io::stdout(), &output)?;
        }
        Command::Compare {
            file,
            name_prefix,
            output,
            lang,
            overrides,
        } => {
            let intl = new_intl(file, name_prefix)?;

            let overrides = overrides.or(intl.overrides_dir().map(PathBuf::from));
            let overrides = if let Some(dir) = &overrides {
                Some(intl.load_overrides(dir, Some(vec![lang]))?)
            } else {
                None
            };
            let template_lang = intl.template_language();
            let template = intl.template_content()?;
            let translated = intl.list_translated()?;
            let mut rows: Vec<CsvRow> = Vec::new();
            for (language, path) in translated {
                if language != lang {
                    continue;
                }

                let contents = std::fs::read_to_string(path)?;
                let file: ArbFile = serde_json::from_str(&contents)?;

                for entry in template.entries() {
                    if entry.is_translatable() {
                        let correction = if let Some(overrides) = &overrides {
                            if let Some(file) = overrides.get(&lang) {
                                if let Some(entry) = file.lookup(entry.key().as_ref()) {
                                    entry
                                        .value()
                                        .as_str()
                                        .map(|s| s.to_string())
                                        .unwrap_or_default()
                                } else {
                                    String::new()
                                }
                            } else {
                                String::new()
                            }
                        } else {
                            String::new()
                        };

                        if let Some(target) = file.lookup(entry.key().as_ref()) {
                            rows.push(CsvRow {
                                id: entry.key().as_ref().to_string(),
                                source: entry
                                    .value()
                                    .as_str()
                                    .map(|s| s.to_string())
                                    .unwrap_or_default(),
                                target: target
                                    .value()
                                    .as_str()
                                    .map(|s| s.to_string())
                                    .unwrap_or_default(),
                                correction,
                                comment: String::new(),
                            });
                        }
                    }
                }
            }

            if let Some(path) = output {
                let wtr = WriterBuilder::new().has_headers(false).from_path(path)?;
                write_csv_rows(wtr, rows, *template_lang, lang)?;
            } else {
                let wtr = WriterBuilder::new()
                    .has_headers(false)
                    .from_writer(std::io::stdout());
                write_csv_rows(wtr, rows, *template_lang, lang)?;
            }
        }
        Command::Import {
            file,
            name_prefix,
            lang,
            delimiter,
            overrides,
            input,
        } => {
            let intl = new_intl(file, name_prefix)?;

            let overrides = overrides.or(intl.overrides_dir().map(PathBuf::from));
            let overrides = overrides.ok_or_else(|| {
                anyhow!("no overrides, either configure overrides-dir or set --overrides")
            })?;
            let mut overrides_map = intl.load_overrides(&overrides, Some(vec![lang]))?;
            let mut default = ArbFile::default();
            let overrides_file = overrides_map.get_mut(&lang).unwrap_or_else(|| &mut default);
            let mut rdr = ReaderBuilder::new()
                .delimiter(delimiter as u8)
                .from_path(input)?;
            for result in rdr.deserialize() {
                // Our headers don't match the field names!
                let record: (String, String, String, String, String) = result?;
                let record = CsvRow {
                    id: record.0,
                    source: record.1,
                    target: record.2,
                    correction: record.3,
                    comment: record.4,
                };
                if !record.correction.is_empty() {
                    overrides_file.insert_translation(&ArbKey::new(&record.id), record.correction);
                }
            }

            let output_name = intl.format_file_name(lang);
            let output_file = overrides.join(output_name);

            tracing::info!(path = %output_file.display(), "write file");
            serde_json::to_writer_pretty(std::fs::File::create(&output_file)?, &overrides_file)?;
        }
    }
    Ok(())
}

fn new_intl(path: impl AsRef<Path>, name_prefix: Option<String>) -> Result<Intl> {
    Ok(Intl::new_with_prefix(path, name_prefix)?)
}

async fn translate_language(
    intl: &mut Intl,
    lang: Lang,
    api_key: String,
    apply: bool,
    force: bool,
    invalidate: Vec<String>,
    overrides: Option<HashMap<Lang, ArbFile>>,
) -> Result<()> {
    let invalidation = if force {
        Some(Invalidation::All)
    } else if !invalidate.is_empty() {
        Some(Invalidation::Keys(invalidate))
    } else {
        None
    };

    let api = DeeplApi::new(ApiOptions::new(api_key));
    let options = TranslationOptions {
        target_lang: lang,
        dry_run: !apply,
        invalidation,
        overrides,
        disable_cache: false,
    };

    let result = intl.translate(&api, options).await?;

    if apply {
        let content = serde_json::to_string_pretty(&result.translated)?;
        let file_path = intl.file_path(lang)?;
        tracing::info!(path = %file_path.display(), "write file");
        std::fs::write(&file_path, &content)?;
    }
    Ok(())
}

fn write_csv_rows<W: std::io::Write>(
    mut wtr: Writer<W>,
    rows: Vec<CsvRow>,
    source: Lang,
    target: Lang,
) -> Result<()> {
    let source_header = format!("Source ({})", source);
    let target_header = format!("Target ({})", target);
    let correction_header = format!("Correction ({})", target);
    wtr.write_record(&[
        "Identifier",
        &source_header,
        &target_header,
        &correction_header,
        "Comment",
    ])?;
    for row in rows {
        wtr.serialize(&row)?;
    }
    wtr.flush()?;
    Ok(())
}