Compare commits
No commits in common. "l10n-db" and "main" have entirely different histories.
623
Cargo.lock
generated
623
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -33,4 +33,4 @@ members = [
|
|||||||
"tree",
|
"tree",
|
||||||
"visions/server",
|
"visions/server",
|
||||||
"gm-dash/server"
|
"gm-dash/server"
|
||||||
, "l10n-db"]
|
]
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "l10n-db"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
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"] }
|
|
||||||
tempfile = "3.17.1"
|
|
||||||
toml = "0.8.20"
|
|
||||||
|
|
||||||
# [lib]
|
|
||||||
# name = "l10n_db"
|
|
||||||
# path = "src/lib.rs"
|
|
||||||
#
|
|
||||||
# [[bin]]
|
|
||||||
# name = "l10n-db"
|
|
||||||
# path = "src/main.rs"
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
|||||||
key = "OpenSandbox"
|
|
||||||
description = "A sandbox vault refers to a stock vault that contains test data and allows the user to make edits and run experiments on test data."
|
|
||||||
|
|
||||||
[variants.en-US]
|
|
||||||
locale = "en-US"
|
|
||||||
content = "Open Sandbox vault"
|
|
||||||
modified = "2025-02-22T21:35:02.032300406Z"
|
|
@ -1,56 +0,0 @@
|
|||||||
use std::{io::{BufReader, Read, Write}, path::PathBuf, process::Command};
|
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
|
||||||
|
|
||||||
use icu_locid::{langid, LanguageIdentifier};
|
|
||||||
use l10n_db::{self, Bundle, Editor};
|
|
||||||
|
|
||||||
#[derive(Parser)]
|
|
||||||
#[command(version, about, long_about = None)]
|
|
||||||
struct Cli {
|
|
||||||
#[command(subcommand)]
|
|
||||||
command: Option<Commands>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
|
||||||
enum Commands {
|
|
||||||
/// Edit, potentially creating, a key
|
|
||||||
EditKey { name: String, locale: String },
|
|
||||||
/// List al keys in the database
|
|
||||||
ListKeys,
|
|
||||||
// Search the database
|
|
||||||
// Search { },
|
|
||||||
/// Export the database
|
|
||||||
Export {
|
|
||||||
#[arg(short, long)]
|
|
||||||
format: String,
|
|
||||||
#[arg(short, long)]
|
|
||||||
locale: String
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor: &str) {
|
|
||||||
let message = bundle.message(key);
|
|
||||||
Editor::edit(message, locale, editor);
|
|
||||||
bundle.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let editor = std::env::var("EDITOR").unwrap();
|
|
||||||
let db_path = std::env::var("DB_PATH").unwrap();
|
|
||||||
|
|
||||||
let cli = Cli::parse();
|
|
||||||
|
|
||||||
let mut bundle = Bundle::load_from_disk(PathBuf::from(&db_path));
|
|
||||||
|
|
||||||
match &cli.command {
|
|
||||||
Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), langid!("en-US"), &editor),
|
|
||||||
Some(Commands::ListKeys) => {
|
|
||||||
for (key, _) in bundle.message_iter() {
|
|
||||||
println!("{}", key);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Some(Commands::Export{..}) => todo!(),
|
|
||||||
None => {},
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
use std::{collections::HashMap, fs::File, io::Write, path::PathBuf};
|
|
||||||
|
|
||||||
use crate::Message;
|
|
||||||
|
|
||||||
pub struct Bundle {
|
|
||||||
path: PathBuf,
|
|
||||||
messages: HashMap<String, Message>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Bundle {
|
|
||||||
pub fn load_from_disk(path: PathBuf) -> Self {
|
|
||||||
let mut messages = HashMap::new();
|
|
||||||
if path.is_dir() {
|
|
||||||
for entry in std::fs::read_dir(&path).unwrap() {
|
|
||||||
let entry = entry.unwrap();
|
|
||||||
let path = entry.path().clone();
|
|
||||||
let key = path.file_stem().unwrap();
|
|
||||||
|
|
||||||
let message = Message::from_file(&entry.path());
|
|
||||||
messages.insert(key.to_str().unwrap().to_owned(), message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Self { path, messages }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn message_iter(&self) -> impl Iterator<Item = (&String, &Message)> {
|
|
||||||
self.messages.iter()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn message(&mut self, name: String) -> &mut Message {
|
|
||||||
self.messages
|
|
||||||
.entry(name.to_owned())
|
|
||||||
.or_insert(Message::new(name.to_owned()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save(&self) {
|
|
||||||
self.messages.iter().for_each(|(key, value)| {
|
|
||||||
let mut path = self.path.clone();
|
|
||||||
path.push(key);
|
|
||||||
path.set_extension("toml");
|
|
||||||
save_file(&path, toml::to_string(value).unwrap().as_bytes());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn save_file(path: &PathBuf, s: &[u8]) {
|
|
||||||
let mut f = File::create(path).unwrap();
|
|
||||||
f.write(s).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {}
|
|
@ -1,52 +0,0 @@
|
|||||||
use std::{io::{BufReader, Read, Write}, path::Path, process::Command};
|
|
||||||
|
|
||||||
use icu_locid::LanguageIdentifier;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
use crate::{Message, Variant};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
struct EditorMessage {
|
|
||||||
description: String,
|
|
||||||
content: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EditorMessage {
|
|
||||||
fn from_variant(description: String, variant: &Variant) -> Self {
|
|
||||||
Self {
|
|
||||||
description,
|
|
||||||
content: variant.content().to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Editor {
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Editor {
|
|
||||||
pub fn edit(msg: &mut Message, locale: LanguageIdentifier, editor: &str) {
|
|
||||||
let description = msg.description().to_owned();
|
|
||||||
let variant = msg.variant_mut(locale);
|
|
||||||
let editable_content = EditorMessage::from_variant(description, &variant);
|
|
||||||
|
|
||||||
let mut file = tempfile::NamedTempFile::new().unwrap();
|
|
||||||
let _ = file.write(toml::to_string(&editable_content).unwrap().as_bytes());
|
|
||||||
let _ = file.flush();
|
|
||||||
let mut cmd = Command::new(editor).args([file.path()]).spawn().unwrap();
|
|
||||||
cmd.wait().unwrap();
|
|
||||||
let file = file.reopen().unwrap();
|
|
||||||
let mut reader = BufReader::new(file);
|
|
||||||
let mut content = Vec::new();
|
|
||||||
let _ = reader.read_to_end(&mut content);
|
|
||||||
|
|
||||||
println!("content");
|
|
||||||
println!("{}", String::from_utf8(content.clone()).unwrap());
|
|
||||||
|
|
||||||
let new_variant: EditorMessage = toml::from_str(&String::from_utf8(content).unwrap()).unwrap();
|
|
||||||
|
|
||||||
variant.set_content(new_variant.content);
|
|
||||||
msg.set_description(new_variant.description);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
|||||||
mod bundle;
|
|
||||||
pub use bundle::Bundle;
|
|
||||||
|
|
||||||
mod editor;
|
|
||||||
pub use editor::Editor;
|
|
||||||
|
|
||||||
mod types;
|
|
||||||
pub use types::{Message, Variant};
|
|
||||||
|
|
||||||
/*
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
#[test]
|
|
||||||
fn it_can_represent_an_untranslated_message() {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_can_represent_a_partially_translated_message() {
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
@ -1,79 +0,0 @@
|
|||||||
use std::{collections::HashMap, io::{BufReader, Read}, path::Path};
|
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use icu_locid::LanguageIdentifier;
|
|
||||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
|
||||||
pub struct Message {
|
|
||||||
key: String,
|
|
||||||
description: String,
|
|
||||||
variants: HashMap<LanguageIdentifier, Variant>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Message {
|
|
||||||
pub fn new(key: String) -> Self {
|
|
||||||
Self {
|
|
||||||
key,
|
|
||||||
description: "".to_owned(),
|
|
||||||
variants: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_file(path: &Path) -> Message {
|
|
||||||
let file = std::fs::File::open(path).unwrap();
|
|
||||||
let mut content = Vec::new();
|
|
||||||
let mut reader = BufReader::new(file);
|
|
||||||
let _ = reader.read_to_end(&mut content);
|
|
||||||
toml::from_str(&String::from_utf8(content).unwrap()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_description(&mut self, desc: String) {
|
|
||||||
self.description = desc;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn description(&self) -> &str {
|
|
||||||
&self.description
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn variant_mut(&mut self, locale: LanguageIdentifier) -> &mut Variant {
|
|
||||||
self.variants.entry(locale.clone()).or_insert(Variant {
|
|
||||||
locale,
|
|
||||||
content: "".to_owned(),
|
|
||||||
modified: Utc::now(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Debug)]
|
|
||||||
pub struct Variant {
|
|
||||||
locale: LanguageIdentifier,
|
|
||||||
content: String,
|
|
||||||
modified: DateTime<Utc>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Variant {
|
|
||||||
pub fn content(&self) -> &str {
|
|
||||||
&self.content
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_content(&mut self, content: String) {
|
|
||||||
self.content = content;
|
|
||||||
self.modified = Utc::now();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
fn time_to_number<S>(time: &DateTime, s: S) -> Result<S::Ok, S::Error>
|
|
||||||
where S: Serializer {
|
|
||||||
let seconds: u64 = time.as_secs();
|
|
||||||
s.serialize_u64(seconds)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn number_to_time<'de, D>(d: D) -> Result<DateTime, D::Error>
|
|
||||||
where D: Deserializer<'de> {
|
|
||||||
let buf = String::deserialize(d)?;
|
|
||||||
let num = buf.parse::<u64>().unwrap();
|
|
||||||
Ok(DateTime::try_from(num).unwrap())
|
|
||||||
}
|
|
||||||
*/
|
|
Loading…
Reference in New Issue
Block a user