Skip to content

Commit

Permalink
Add new option --tr for rust-i18n-cli to support manual add translati…
Browse files Browse the repository at this point in the history
…on for tr!

Usage: cargo i18n --tr "Hello, world"
  • Loading branch information
varphone committed Jan 20, 2024
1 parent be265e8 commit bc3406a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anyhow::Error;
use clap::{Args, Parser};
use rust_i18n_support::TrKey;

use std::{collections::HashMap, path::Path};

use rust_i18n_extract::extractor::Message;
use rust_i18n_extract::{extractor, generator, iter};
mod config;

Expand All @@ -27,6 +29,22 @@ struct I18nArgs {
/// Extract all untranslated I18n texts from source code
#[arg(default_value = "./")]
source: Option<String>,
/// Add a translation to the localize file for tr!
#[arg(long, default_value = None)]
tr: Option<Vec<String>>,
}

/// Add translations to the localize file for tr!
fn add_translations(list: &[String], results: &mut HashMap<String, Message>) {
for item in list {
let index = results.len();
results.entry(item.tr_key()).or_insert(Message {
key: item.clone(),
index,
is_tr: true,
locations: vec![],
});
}
}

fn main() -> Result<(), Error> {
Expand All @@ -42,6 +60,10 @@ fn main() -> Result<(), Error> {
extractor::extract(&mut results, path, source)
})?;

if let Some(list) = args.tr {
add_translations(&list, &mut results);
}

let mut messages: Vec<_> = results.iter().collect();
messages.sort_by_key(|(_k, m)| m.index);

Expand Down

0 comments on commit bc3406a

Please sign in to comment.