Compare commits
3 Commits
1c3d0711e1
...
44ee6ec8a5
Author | SHA1 | Date | |
---|---|---|---|
44ee6ec8a5 | |||
52a0d6e3f2 | |||
200c13a14e |
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -2418,7 +2418,9 @@ dependencies = [
|
||||
"clap",
|
||||
"icu_locid",
|
||||
"serde 1.0.218",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"thiserror 2.0.11",
|
||||
"toml",
|
||||
]
|
||||
|
||||
@ -3914,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",
|
||||
|
@ -8,7 +8,9 @@ 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"
|
||||
|
||||
# [lib]
|
||||
|
8
l10n-db/config.toml
Normal file
8
l10n-db/config.toml
Normal file
@ -0,0 +1,8 @@
|
||||
db_path = "./i18n"
|
||||
base_locale = "en"
|
||||
locales = [
|
||||
"en",
|
||||
"eo",
|
||||
"de",
|
||||
"es",
|
||||
]
|
@ -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"
|
7
l10n-db/i18n/SaveSettings.toml
Normal file
7
l10n-db/i18n/SaveSettings.toml
Normal file
@ -0,0 +1,7 @@
|
||||
key = "SaveSettings"
|
||||
description = "This is a label on a button which will save the settings when clicked"
|
||||
|
||||
[variants.en]
|
||||
locale = "en"
|
||||
content = "Save Settings"
|
||||
modified = "2025-02-22T23:44:18.874218939Z"
|
7
l10n-db/i18n/Welcome.toml
Normal file
7
l10n-db/i18n/Welcome.toml
Normal file
@ -0,0 +1,7 @@
|
||||
key = "Welcome"
|
||||
description = "This is a welcome content that will be shown on first app opening, before configuration."
|
||||
|
||||
[variants.en]
|
||||
locale = "en"
|
||||
content = "Welcome to FitnessTrax"
|
||||
modified = "2025-02-22T23:43:24.786544124Z"
|
1
l10n-db/output.json
Normal file
1
l10n-db/output.json
Normal file
@ -0,0 +1 @@
|
||||
{"SaveSettings":"Save Settings","Welcome":"Welcome to FitnessTrax"}
|
@ -1,9 +1,14 @@
|
||||
use std::{io::{BufReader, Read, Write}, path::PathBuf, process::Command};
|
||||
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};
|
||||
use l10n_db::{self, export_file, read_file, Bundle, Editor, ReadError};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
@ -15,7 +20,12 @@ struct Cli {
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Edit, potentially creating, a key
|
||||
EditKey { name: String, locale: String },
|
||||
EditKey {
|
||||
#[arg(short, long)]
|
||||
name: String,
|
||||
#[arg(short, long)]
|
||||
locale: String,
|
||||
},
|
||||
/// List al keys in the database
|
||||
ListKeys,
|
||||
// Search the database
|
||||
@ -25,10 +35,17 @@ enum Commands {
|
||||
#[arg(short, long)]
|
||||
format: String,
|
||||
#[arg(short, long)]
|
||||
locale: String
|
||||
locale: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Config {
|
||||
db_path: PathBuf,
|
||||
base_locale: LanguageIdentifier,
|
||||
locales: Vec<LanguageIdentifier>,
|
||||
}
|
||||
|
||||
fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor: &str) {
|
||||
let message = bundle.message(key);
|
||||
Editor::edit(message, locale, editor);
|
||||
@ -36,21 +53,31 @@ fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let editor = std::env::var("EDITOR").unwrap();
|
||||
let db_path = std::env::var("DB_PATH").unwrap();
|
||||
let editor = std::env::var("EDITOR").expect("Set EDITOR to the path to your favorite editor");
|
||||
|
||||
let config: Config = read_file(&PathBuf::from("./config.toml"))
|
||||
.and_then(|bytes| String::from_utf8(bytes).map_err(|_| ReadError::InvalidFormat))
|
||||
.and_then(|content| toml::from_str(&content).map_err(|_| ReadError::InvalidFormat))
|
||||
.unwrap();
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
let mut bundle = Bundle::load_from_disk(PathBuf::from(&db_path));
|
||||
let mut bundle = Bundle::load_from_disk(PathBuf::from(&config.db_path));
|
||||
|
||||
match &cli.command {
|
||||
Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), langid!("en-US"), &editor),
|
||||
Some(Commands::EditKey { name, locale }) => {
|
||||
let identifier = locale.parse::<LanguageIdentifier>().unwrap();
|
||||
edit_key(&mut bundle, name.to_owned(), identifier, &editor)
|
||||
}
|
||||
Some(Commands::ListKeys) => {
|
||||
for (key, _) in bundle.message_iter() {
|
||||
println!("{}", key);
|
||||
}
|
||||
}
|
||||
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();
|
||||
},
|
||||
Some(Commands::Export{..}) => todo!(),
|
||||
None => {},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -1,17 +1,19 @@
|
||||
use std::{io::{BufReader, Read, Write}, path::Path, process::Command};
|
||||
|
||||
use icu_locid::LanguageIdentifier;
|
||||
use icu_locid::{langid, LanguageIdentifier};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{Message, Variant};
|
||||
use crate::{read_fh, Message, Variant};
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
struct EditorMessage {
|
||||
description: String,
|
||||
source: String,
|
||||
content: String,
|
||||
}
|
||||
|
||||
/*
|
||||
impl EditorMessage {
|
||||
fn from_variant(description: String, variant: &Variant) -> Self {
|
||||
Self {
|
||||
@ -20,6 +22,7 @@ impl EditorMessage {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
pub struct Editor {
|
||||
}
|
||||
@ -27,8 +30,14 @@ pub struct Editor {
|
||||
impl Editor {
|
||||
pub fn edit(msg: &mut Message, locale: LanguageIdentifier, editor: &str) {
|
||||
let description = msg.description().to_owned();
|
||||
let source_string = msg.variant_mut(langid!("en")).content().to_owned();
|
||||
let variant = msg.variant_mut(locale);
|
||||
let editable_content = EditorMessage::from_variant(description, &variant);
|
||||
// let editable_content = EditorMessage::from_variant(description, &variant);
|
||||
let editable_content = EditorMessage {
|
||||
description,
|
||||
source: source_string,
|
||||
content: variant.content().to_owned(),
|
||||
};
|
||||
|
||||
let mut file = tempfile::NamedTempFile::new().unwrap();
|
||||
let _ = file.write(toml::to_string(&editable_content).unwrap().as_bytes());
|
||||
@ -36,12 +45,7 @@ impl Editor {
|
||||
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 content = read_fh(&file).unwrap();
|
||||
|
||||
let new_variant: EditorMessage = toml::from_str(&String::from_utf8(content).unwrap()).unwrap();
|
||||
|
||||
|
22
l10n-db/src/js_format.rs
Normal file
22
l10n-db/src/js_format.rs
Normal 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(())
|
||||
}
|
@ -4,9 +4,15 @@ 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};
|
||||
|
||||
mod utils;
|
||||
pub use utils::*;
|
||||
|
||||
/*
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
@ -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,
|
||||
@ -45,7 +49,7 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
pub struct Variant {
|
||||
locale: LanguageIdentifier,
|
||||
content: String,
|
||||
|
37
l10n-db/src/utils.rs
Normal file
37
l10n-db/src/utils.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use std::{fs::File, io::{BufReader, ErrorKind, Read}, path::Path};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ReadError {
|
||||
#[error("file not found")]
|
||||
FileNotFound,
|
||||
|
||||
#[error("invalid file format")]
|
||||
InvalidFormat,
|
||||
|
||||
#[error("unhandled read error")]
|
||||
Unhandled(ErrorKind),
|
||||
}
|
||||
|
||||
pub fn read_file(path: &Path) -> Result<Vec<u8>, ReadError> {
|
||||
let file = File::open(path).map_err(|err| {
|
||||
match err.kind() {
|
||||
ErrorKind::NotFound => ReadError::FileNotFound,
|
||||
_ => ReadError::Unhandled( err.kind()),
|
||||
}
|
||||
})?;
|
||||
read_fh(&file)
|
||||
}
|
||||
|
||||
pub fn read_fh(file: &File) -> Result<Vec<u8>, ReadError> {
|
||||
let mut content = Vec::new();
|
||||
let mut reader = BufReader::new(file);
|
||||
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),
|
||||
}
|
Loading…
Reference in New Issue
Block a user