diff --git a/Cargo.lock b/Cargo.lock index 2f50fae..4982051 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2418,6 +2418,7 @@ dependencies = [ "clap", "icu_locid", "serde 1.0.218", + "serde_json", "tempfile", "thiserror 2.0.11", "toml", @@ -3915,9 +3916,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.136" +version = "1.0.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "336a0c23cf42a38d9eaa7cd22c7040d04e1228a19a933890805ffd00a16437d2" +checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6" dependencies = [ "itoa", "memchr", diff --git a/l10n-db/Cargo.toml b/l10n-db/Cargo.toml index e36077b..8016440 100644 --- a/l10n-db/Cargo.toml +++ b/l10n-db/Cargo.toml @@ -8,6 +8,7 @@ chrono = { version = "0.4.39", features = ["serde"] } clap = { version = "4.5.30", features = ["derive"] } icu_locid = { version = "1.5.0", features = ["serde"] } serde = { version = "1.0.218", features = ["derive"] } +serde_json = "1.0.139" tempfile = "3.17.1" thiserror = "2.0.11" toml = "0.8.20" diff --git a/l10n-db/output.json b/l10n-db/output.json new file mode 100644 index 0000000..fe23c55 --- /dev/null +++ b/l10n-db/output.json @@ -0,0 +1 @@ +{"SaveSettings":"Save Settings","Welcome":"Welcome to FitnessTrax"} \ No newline at end of file diff --git a/l10n-db/src/bin/main.rs b/l10n-db/src/bin/main.rs index 1860136..a7880f8 100644 --- a/l10n-db/src/bin/main.rs +++ b/l10n-db/src/bin/main.rs @@ -7,7 +7,7 @@ use std::{ use clap::{Parser, Subcommand}; use icu_locid::{langid, LanguageIdentifier}; -use l10n_db::{self, read_file, Bundle, Editor, ReadError}; +use l10n_db::{self, export_file, read_file, Bundle, Editor, ReadError}; use serde::Deserialize; #[derive(Parser)] @@ -35,7 +35,7 @@ enum Commands { #[arg(short, long)] format: String, #[arg(short, long)] - locale: String, + locale: Option, }, } @@ -74,7 +74,10 @@ fn main() { println!("{}", key); } } - Some(Commands::Export { .. }) => todo!(), + Some(Commands::Export { format, locale }) => { + let locale = locale.as_ref().map(|l| l.clone().parse::().unwrap()).unwrap_or(langid!("en")); + export_file(&bundle, locale, &PathBuf::from("output.json")).unwrap(); + }, None => {} } } diff --git a/l10n-db/src/bundle.rs b/l10n-db/src/bundle.rs index 3144ea9..df1e6b1 100644 --- a/l10n-db/src/bundle.rs +++ b/l10n-db/src/bundle.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, fs::File, io::Write, path::PathBuf}; -use crate::Message; +use crate::{Message, WriteError}; pub struct Bundle { path: PathBuf, diff --git a/l10n-db/src/js_format.rs b/l10n-db/src/js_format.rs new file mode 100644 index 0000000..c8c5844 --- /dev/null +++ b/l10n-db/src/js_format.rs @@ -0,0 +1,22 @@ +use std::{collections::BTreeMap, fs::File, io::Write, path::Path}; + +use icu_locid::LanguageIdentifier; + +use crate::{Bundle, WriteError}; + +pub fn export_file(bundle: &Bundle, locale: LanguageIdentifier, path: &Path) -> Result<(), WriteError> { + let mut file = File::create(path).unwrap(); + export_fh(bundle, locale, &mut file) +} + +pub fn export_fh(bundle: &Bundle, locale: LanguageIdentifier, fh: &mut File) -> Result<(), WriteError> { + let messages = bundle.message_iter().map(|(key, message)| { + let content = message.variant(&locale).unwrap().content().to_owned(); + (key.to_owned(), content) + }).collect::>(); + + let messages: BTreeMap = messages.into_iter().collect(); + fh.write(serde_json::to_string(&messages).unwrap().as_bytes()).unwrap(); + + Ok(()) +} diff --git a/l10n-db/src/lib.rs b/l10n-db/src/lib.rs index ab1eec2..a5286f6 100644 --- a/l10n-db/src/lib.rs +++ b/l10n-db/src/lib.rs @@ -4,6 +4,9 @@ pub use bundle::Bundle; mod editor; pub use editor::Editor; +mod js_format; +pub use js_format::{export_file, export_fh}; + mod types; pub use types::{Message, Variant}; diff --git a/l10n-db/src/types.rs b/l10n-db/src/types.rs index 13b447d..ad5caa0 100644 --- a/l10n-db/src/types.rs +++ b/l10n-db/src/types.rs @@ -36,6 +36,10 @@ impl Message { &self.description } + pub fn variant(&self, locale: &LanguageIdentifier) -> Option<&Variant> { + self.variants.get(locale) + } + pub fn variant_mut(&mut self, locale: LanguageIdentifier) -> &mut Variant { self.variants.entry(locale.clone()).or_insert(Variant { locale, diff --git a/l10n-db/src/utils.rs b/l10n-db/src/utils.rs index b499b9d..82435a0 100644 --- a/l10n-db/src/utils.rs +++ b/l10n-db/src/utils.rs @@ -29,3 +29,9 @@ pub fn read_fh(file: &File) -> Result, ReadError> { reader.read_to_end(&mut content).map_err(|err| ReadError::Unhandled(err.kind()))?; Ok(content) } + +#[derive(Debug, Error)] +pub enum WriteError { + #[error("unhandled write error")] + Unhandled(ErrorKind), +}