Export to json

This commit is contained in:
Savanni D'Gerinel 2025-02-22 19:11:29 -05:00
parent 52a0d6e3f2
commit 44ee6ec8a5
9 changed files with 47 additions and 6 deletions

5
Cargo.lock generated
View File

@ -2418,6 +2418,7 @@ dependencies = [
"clap", "clap",
"icu_locid", "icu_locid",
"serde 1.0.218", "serde 1.0.218",
"serde_json",
"tempfile", "tempfile",
"thiserror 2.0.11", "thiserror 2.0.11",
"toml", "toml",
@ -3915,9 +3916,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.136" version = "1.0.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "336a0c23cf42a38d9eaa7cd22c7040d04e1228a19a933890805ffd00a16437d2" checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",

View File

@ -8,6 +8,7 @@ chrono = { version = "0.4.39", features = ["serde"] }
clap = { version = "4.5.30", features = ["derive"] } clap = { version = "4.5.30", features = ["derive"] }
icu_locid = { version = "1.5.0", features = ["serde"] } icu_locid = { version = "1.5.0", features = ["serde"] }
serde = { version = "1.0.218", features = ["derive"] } serde = { version = "1.0.218", features = ["derive"] }
serde_json = "1.0.139"
tempfile = "3.17.1" tempfile = "3.17.1"
thiserror = "2.0.11" thiserror = "2.0.11"
toml = "0.8.20" toml = "0.8.20"

1
l10n-db/output.json Normal file
View File

@ -0,0 +1 @@
{"SaveSettings":"Save Settings","Welcome":"Welcome to FitnessTrax"}

View File

@ -7,7 +7,7 @@ use std::{
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use icu_locid::{langid, LanguageIdentifier}; 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; use serde::Deserialize;
#[derive(Parser)] #[derive(Parser)]
@ -35,7 +35,7 @@ enum Commands {
#[arg(short, long)] #[arg(short, long)]
format: String, format: String,
#[arg(short, long)] #[arg(short, long)]
locale: String, locale: Option<String>,
}, },
} }
@ -74,7 +74,10 @@ fn main() {
println!("{}", key); println!("{}", key);
} }
} }
Some(Commands::Export { .. }) => todo!(), Some(Commands::Export { format, locale }) => {
let locale = locale.as_ref().map(|l| l.clone().parse::<LanguageIdentifier>().unwrap()).unwrap_or(langid!("en"));
export_file(&bundle, locale, &PathBuf::from("output.json")).unwrap();
},
None => {} None => {}
} }
} }

View File

@ -1,6 +1,6 @@
use std::{collections::HashMap, fs::File, io::Write, path::PathBuf}; use std::{collections::HashMap, fs::File, io::Write, path::PathBuf};
use crate::Message; use crate::{Message, WriteError};
pub struct Bundle { pub struct Bundle {
path: PathBuf, path: PathBuf,

22
l10n-db/src/js_format.rs Normal file
View File

@ -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::<Vec<(String, String)>>();
let messages: BTreeMap<String, String> = messages.into_iter().collect();
fh.write(serde_json::to_string(&messages).unwrap().as_bytes()).unwrap();
Ok(())
}

View File

@ -4,6 +4,9 @@ pub use bundle::Bundle;
mod editor; mod editor;
pub use editor::Editor; pub use editor::Editor;
mod js_format;
pub use js_format::{export_file, export_fh};
mod types; mod types;
pub use types::{Message, Variant}; pub use types::{Message, Variant};

View File

@ -36,6 +36,10 @@ impl Message {
&self.description &self.description
} }
pub fn variant(&self, locale: &LanguageIdentifier) -> Option<&Variant> {
self.variants.get(locale)
}
pub fn variant_mut(&mut self, locale: LanguageIdentifier) -> &mut Variant { pub fn variant_mut(&mut self, locale: LanguageIdentifier) -> &mut Variant {
self.variants.entry(locale.clone()).or_insert(Variant { self.variants.entry(locale.clone()).or_insert(Variant {
locale, locale,

View File

@ -29,3 +29,9 @@ pub fn read_fh(file: &File) -> Result<Vec<u8>, ReadError> {
reader.read_to_end(&mut content).map_err(|err| ReadError::Unhandled(err.kind()))?; reader.read_to_end(&mut content).map_err(|err| ReadError::Unhandled(err.kind()))?;
Ok(content) Ok(content)
} }
#[derive(Debug, Error)]
pub enum WriteError {
#[error("unhandled write error")]
Unhandled(ErrorKind),
}