Create a little application which manages an l10n messages database #290

Merged
savanni merged 16 commits from l10n-db into main 2025-02-25 06:45:56 +00:00
2 changed files with 40 additions and 23 deletions
Showing only changes of commit 76de75210f - Show all commits

View File

@ -1,7 +1,5 @@
use std::{ use std::{
io::{BufReader, Read, Write}, fmt, io::{BufReader, Read, Write}, path::PathBuf, process::Command
path::PathBuf,
process::Command,
}; };
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
@ -68,32 +66,26 @@ struct Report {
out_of_date: Vec<String>, out_of_date: Vec<String>,
} }
impl fmt::Display for Report {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Out of date messages\n")?;
for key in self.out_of_date.iter() {
write!(f, "\t{}\n", key)?;
}
Ok(())
}
}
fn generate_report( fn generate_report(
bundle: &Bundle, bundle: &Bundle,
base_locale: &LanguageIdentifier, base_locale: &LanguageIdentifier,
locales: Vec<LanguageIdentifier>, locales: Vec<LanguageIdentifier>,
) -> Report { ) -> Report {
let mut report: Report = Default::default(); let mut report: Report = Default::default();
let locales: Vec<LanguageIdentifier> =
locales.into_iter().filter(|a| a != base_locale).collect();
for (key, message) in bundle.message_iter() { for (key, message) in bundle.message_iter() {
match message.variant(base_locale) { if message.variants_out_of_date(base_locale).len() > 0 {
Some(ref base_variant) => {
for locale in locales.iter() {
match message.variant(locale) {
Some(v) if v.modified() < base_variant.modified() => {
report.out_of_date.push(key.to_owned()) report.out_of_date.push(key.to_owned())
} }
Some(_) => {}
None => report.out_of_date.push(key.to_owned()),
}
}
}
None => {
report.source_deleted.push(key.clone());
}
}
let base_variant = message.variant(base_locale).clone();
} }
report report
@ -141,7 +133,7 @@ fn main() {
} }
Some(Commands::Report) => { Some(Commands::Report) => {
let report = generate_report(&bundle, &config.base_locale, config.locales); let report = generate_report(&bundle, &config.base_locale, config.locales);
println!("{:?}", report); println!("{}", report);
} }
None => {} None => {}
} }

View File

@ -1,4 +1,8 @@
use std::{collections::HashMap, io::{BufReader, Read}, path::Path}; use std::{
collections::HashMap,
io::{BufReader, Read},
path::Path,
};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use icu_locid::LanguageIdentifier; use icu_locid::LanguageIdentifier;
@ -47,6 +51,27 @@ impl Message {
modified: Utc::now(), modified: Utc::now(),
}) })
} }
pub fn variants_out_of_date(
&self,
base_locale: &LanguageIdentifier,
) -> Vec<LanguageIdentifier> {
match self
.variants
.get(base_locale)
.map(|variant| variant.modified())
{
Some(base_date) => self
.variants
.iter()
.filter(|(_, value)| base_date > value.modified())
.map(|(locale, _)| locale.clone())
.collect(),
None => vec![],
}
}
// pub fn missing_variants(&self, locals: Vec<LanguageIdentifier>) -> Vec<LanguageIdentifier> {}
} }
#[derive(Deserialize, Serialize, Debug, Clone)] #[derive(Deserialize, Serialize, Debug, Clone)]