Unverified Commit b6bac9ac authored by Katharina Fey's avatar Katharina Fey 🏴
Browse files

Initial prototype for key generation

parent 947d208a
eval "$(lorri direnv)"
/target
Cargo.lock
assets
[package]
name = "test-cli-app"
version = "0.1.0"
authors = ["Katharina Fey <kookie@spacekookie.de>"]
edition = "2018"
[build-dependencies]
traduki = { path = "../.." }
\ No newline at end of file
//! Build support file that generates code to access translation data
fn main() {
traduki::bootstrap("assets").unwrap();
}
// Include the auto generated assets
include!(concat!(env!("OUT_DIR"), "/traduki.rs"));
fn main() {
println!("{}", test_key());
}
with import <nixpkgs> {
config.android_sdk.accept_license = true;
};
stdenv.mkDerivation {
name = "qaul";
buildInputs = with pkgs; [
# General rust stuff
rustracer rustup clangStdenv cargo-watch
# Required for the docs
mdbook graphviz
# Required for Android integration
cmake
# Required for Android builds
# androidenv.androidPkgs_9_0.androidsdk
# Required for libqaul-voice
libopus pkg-config
# Required for the code coverage and stuff
openssl
];
}
//! Generate key-data
use std::collections::BTreeMap;
pub(crate) struct Key<'n, 'l, 'v> {
name: &'n str,
value: BTreeMap<&'l str, &'v str>,
}
impl<'n, 'l, 'v> Key<'n, 'l, 'v> {
pub(crate) fn new(name: &'n str) -> Self {
Self {
name,
value: BTreeMap::new(),
}
}
/// Add a value for a specific language
pub(crate) fn add_value(mut self, lang: &'l str, value: &'v str) -> Self {
self.value.insert(lang, value);
self
}
/// Generate code for this key
pub(crate) fn generate(self) -> String {
format!(
r#"
pub(crate) fn {fun_name}() -> &'static str {{
match std::env::var("LANG").as_ref().map(|s| s.as_str()) {{
{lang_section}
{default_section}
}}
}}
"#,
fun_name = self.name,
lang_section = {
self.value
.iter()
.map(|(k, v)| format!(r#"Ok("{}") => "{}""#, k, v))
.fold(String::new(), |acc, section| {
format!("{}\n{},", acc, section)
})
},
default_section = format!(r#"_ => "{}""#, self
.value
.get(
std::env::var("TRADUKI_DEFAULT_LANG")
.expect("No default translation language set! You should set the key \
`TRADUKI_DEFAULT_LANG` to whatever you want the fallback language to be.")
.as_str()
)
.expect("Default translation language missing for key"))
)
}
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
//! traduki translation assets management toolkit
mod keys;
use std::{
env,
fs::{self, File},
io::Write,
path::Path,
};
pub fn bootstrap<S: Into<String>>(_: S) -> Result<(), &'static str> {
let env_var = env::var("OUT_DIR");
let out = Path::new(env_var.as_ref().unwrap());
env::set_var("TRADUKI_DEFAULT_LANG", "en_GB");
let _ = fs::remove_dir_all(out);
fs::create_dir_all(out).unwrap();
let mut f = File::create(out.join("traduki.rs")).unwrap();
f.write_all(
keys::Key::new("test_key")
.add_value("en_GB", "hello world")
.add_value("eo", "saluton mondo")
.generate()
.as_bytes(),
)
.unwrap();
Ok(())
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment