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

Fixing some bugs, improving the example

parent 3b3b2614
[package]
name = "traduki"
descrption = "Integrate translated assets into your application or library"
description = "Integrate translated assets into your application or library"
version = "0.1.0"
authors = ["Katharina Fey <kookie@spacekookie.de>"]
edition = "2018"
......
......@@ -4,5 +4,8 @@ version = "0.1.0"
authors = ["Katharina Fey <kookie@spacekookie.de>"]
edition = "2018"
[dependencies]
clap = "2.0"
[build-dependencies]
traduki = { path = "../.." }
\ No newline at end of file
name: "traduki"
descrption: "Translate assets for you application or library"
description: "Translate assets for you application or library"
other_key:
text: |
This is a random key that can be used anywhere, really!
Yaml was chosen as a format because it's a key-value representation,
but can also pretty easily be used to write long multi-line strings.
reflow: false # this setting specifies whether "\n" characters should be stripped from the text
error: "An error has occured and it's probably MY fault!"
error: An error has occured and it's probably MY fault!
name: "traduki"
descrption: "Traduki akiroj por via programo biblioteko"
description: "Traduki akiroj por via programo biblioteko"
other_key:
text: |
Ĉi tio estas sencela indexo, kiu povas esti utiza ie ajn, vere.
......
//! Build support file that generates code to access translation data
use std::env::set_var;
fn main() {
std::env::set_var("TRADUKI_DEFAULT_LANG", "en_GB");
traduki::bootstrap("assets").unwrap();
set_var("TRADUKI_DEFAULT_LANG", "en_GB");
traduki::bootstrap("assets");
}
// Include the auto generated assets
include!(concat!(env!("OUT_DIR"), "/traduki.rs"));
use clap::{App, Arg};
fn main() {
println!("{}", concat!(env!("OUT_DIR"), "/traduki.rs"));
// println!("{}", clap::test_key());
use traduki::app;
let matches = App::new(app::name())
.about(app::description())
.arg(
Arg::with_name("ERROR")
.help(app::error())
.short("-e")
.long("--error"),
)
.get_matches();
}
......@@ -24,6 +24,10 @@ fn asset_dir<S: Into<String>>(s: S) -> PathBuf {
Path::new(&s).into()
}
fn wrap_string(s: String) -> String {
format!(r#"pub(crate) mod traduki {{ {} }}"#, s)
}
pub fn bootstrap<S: Into<String>>(assets: S) {
let out = out_dir();
let assets = asset_dir(assets);
......@@ -34,14 +38,13 @@ pub fn bootstrap<S: Into<String>>(assets: S) {
// Create a new file for translations
let mut f = File::create(out.join("traduki.rs")).expect("Write error: Failed to create file");
f.write_all(
parser::handle_root(&assets)
.expect("Write error: failed to read asset directory tree. Do you have permissions?")
.into_iter()
.fold(String::new(), |prev, section| {
format!("{}\n{}", prev, section.generate())
})
.as_bytes(),
)
.expect("Write error: failed to write generated translations file!");
let parse_output = parser::handle_root(&assets)
.expect("Write error: failed to read asset directory tree. Do you have permissions?")
.into_iter()
.fold(String::new(), |prev, section| {
format!("{}\n{}", prev, section.generate())
});
f.write_all(wrap_string(parse_output).as_bytes())
.expect("Write error: failed to write generated translations file!");
}
......@@ -87,8 +87,6 @@ fn load_keys(section: &str, path: Vec<&Path>) -> Vec<Key> {
map
});
println!("cargo:warning={:?}", map);
let mut default = parse_hash_tree(
&def,
map.remove(&format!("{}.yml", def))
......@@ -101,6 +99,7 @@ fn load_keys(section: &str, path: Vec<&Path>) -> Vec<Key> {
// Build Key types, and merge them into the default key
for (lang, mut yaml) in map.into_iter() {
let lang = lang.replace(".yml", "");
parse_hash_tree(&lang, yaml.remove(0))
.into_iter()
.for_each(|(k, key)| {
......@@ -110,8 +109,7 @@ The default language must contain all keys. Either remove the key from the seco
or switch the default language!",
k
));
def_key.merge(&lang, key);
def_key.merge(&lang, key)
});
}
......
......@@ -31,7 +31,7 @@ impl Section {
format!(
r"
#[allow(unused)]
mod {section_name} {{
pub(crate) mod {section_name} {{
{keys}
{children}
......
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