diff --git a/locales/Cargo.toml b/locales/Cargo.toml new file mode 100644 index 000000000..7fb7177a6 --- /dev/null +++ b/locales/Cargo.toml @@ -0,0 +1,9 @@ +[package] +authors = ["bunnie "] +description = "Localization string database (as static macro) for Xous" +edition = "2018" +license = "MIT OR Apache-2.0" +name = "locales" +version = "0.1.0" + +[dependencies] diff --git a/locales/src/lib.rs b/locales/src/lib.rs new file mode 100755 index 000000000..50ad8abce --- /dev/null +++ b/locales/src/lib.rs @@ -0,0 +1 @@ +# ! [cfg_attr (target_os = "none" , no_std)] # [macro_export] macro_rules ! t { ("mainmenu.backlighton" , $ lang : expr) => { match $ lang . as_ref () { "en" => "Backlight on" , "zh" => "่ƒŒๅ…‰ๅผ€ๅฏ" , "en-audio" => "๐Ÿ”‡" , e => panic ! ("Missing language: {}" , e) } } ; ("mainmenu.backlightoff" , $ lang : expr) => { match $ lang . as_ref () { "en-audio" => "๐Ÿ”‡" , "zh" => "่ƒŒๅ…‰ๅ…ณ้—ญ" , "en" => "Backlight off" , e => panic ! ("Missing language: {}" , e) } } ; ("mainmenu.closemenu" , $ lang : expr) => { match $ lang . as_ref () { "zh" => "ๅ…ณ้—ญๅŠŸ่ƒฝ่กจ" , "en-audio" => "Close menu" , "en" => "Close menu" , e => panic ! ("Missing language: {}" , e) } } ; ("mainmenu.sleep" , $ lang : expr) => { match $ lang . as_ref () { "en-audio" => "Sleep now" , "en" => "Sleep now" , "zh" => "็ก็œ ๆจกๅผ" , e => panic ! ("Missing language: {}" , e) } } ; ($ key : expr , $ lang : expr) => { compile_error ! ("Missing translation") ; } } \ No newline at end of file diff --git a/services/gam/locales/i18n.json b/services/gam/locales/i18n.json new file mode 100644 index 000000000..e550d70a7 --- /dev/null +++ b/services/gam/locales/i18n.json @@ -0,0 +1,22 @@ +{ + "mainmenu.sleep": { + "en": "Sleep now", + "zh": "็ก็œ ๆจกๅผ", + "en-audio": "Sleep now" + }, + "mainmenu.backlighton": { + "en": "Backlight on", + "zh": "่ƒŒๅ…‰ๅผ€ๅฏ", + "en-audio": "๐Ÿ”‡" + }, + "mainmenu.backlightoff": { + "en": "Backlight off", + "zh": "่ƒŒๅ…‰ๅ…ณ้—ญ", + "en-audio": "๐Ÿ”‡" + }, + "mainmenu.closemenu": { + "en": "Close menu", + "zh": "ๅ…ณ้—ญๅŠŸ่ƒฝ่กจ", + "en-audio": "Close menu" + } +} \ No newline at end of file diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 12ea819e2..a49a5c0e1 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -1,6 +1,8 @@ use core::convert::{TryFrom, TryInto}; use core::num::{NonZeroU8, NonZeroUsize}; +pub const LANG: &'static str = "zh"; + pub type MemoryAddress = NonZeroUsize; pub type MemorySize = NonZeroUsize; pub type StackPointer = usize; diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 7c5d06841..1f5f5e35d 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -8,4 +8,12 @@ edition = "2018" [dependencies] #"ed25519-dalek" = "1" -#"pem" = "0.8" \ No newline at end of file +#"pem" = "0.8" + +# locales generation +glob = "0.3.0" +quote = "1.0.2" +serde_json = "1.0.41" +proc-macro2 = "1.0" +regex = "1.3.1" +lazy_static = "1.4.0" \ No newline at end of file diff --git a/xtask/src/generate_locales.rs b/xtask/src/generate_locales.rs new file mode 100755 index 000000000..ef1e14523 --- /dev/null +++ b/xtask/src/generate_locales.rs @@ -0,0 +1,148 @@ + +// this code vendored in from https://github.com/terry90/internationalization-rs +// the original crate is targeted at a non-workspace, `std` project structure. The adaptations here +// make it suitable for `no_std` and workspace integration. + +use crate::project_root; + +use glob::glob; +use lazy_static::lazy_static; +use proc_macro2::{Ident, Span, TokenStream}; +use quote::quote; +use regex::Regex; +use std::collections::HashMap; +use std::fs::File; +use std::io::prelude::*; + +type Key = String; +type Locale = String; +type Value = String; +type Translations = HashMap>; + +fn read_locales() -> Translations { + let mut translations: Translations = HashMap::new(); + + let project_dir = project_root(); + let build_directory = project_dir.to_str().unwrap(); + let locales = format!("{}/**/locales/**/*.json", build_directory); + println!("Reading {}", &locales); + + for entry in glob(&locales).expect("Failed to read glob pattern") { + let entry = entry.unwrap(); + println!("cargo:rerun-if-changed={}", entry.display()); + let file = File::open(entry).expect("Failed to open the file"); + let mut reader = std::io::BufReader::new(file); + let mut content = String::new(); + reader + .read_to_string(&mut content) + .expect("Failed to read the file"); + let res: HashMap> = + serde_json::from_str(&content).expect("Cannot parse locale file"); + translations.extend(res); + } + translations +} + +fn extract_vars(tr: &str) -> Vec { + lazy_static! { + static ref RE: Regex = Regex::new("\\$[a-zA-Z0-9_-]+").unwrap(); + } + + let mut a = RE + .find_iter(tr) + .map(|mat| mat.as_str().to_owned()) + .collect::>(); + a.sort(); + + //println!("-----\n{:?}\n-----", &a); + a +} + +fn convert_vars_to_idents(vars: &Vec) -> Vec { + vars.iter() + .map(|var| Ident::new(&var[1..], Span::call_site())) + .collect() +} + +fn generate_code(translations: Translations) -> proc_macro2::TokenStream { + let mut branches = Vec::::new(); + + for (key, trs) in translations { + let mut langs = Vec::::new(); + let mut needs_interpolation = false; + let mut vars = Vec::new(); + for (lang, tr) in trs { + let lang_vars = extract_vars(&tr); + needs_interpolation = lang_vars.len() > 0; + + if needs_interpolation { + let idents = convert_vars_to_idents(&lang_vars); + vars.extend(lang_vars.clone()); + + langs.push(quote! { + #lang => #tr#(.replace(#lang_vars, $#idents))*, + }); + } else { + langs.push(quote! { + #lang => #tr, + }); + } + } + + vars.sort(); + vars.dedup(); + let vars_ident = convert_vars_to_idents(&vars); + if needs_interpolation { + branches.push(quote! { + (#key, #(#vars_ident: $#vars_ident:expr, )*$lang:expr) => { + match $lang.as_ref() { + #(#langs)* + e => panic!("Missing language: {}", e) + } + }; + }); + branches.push(quote! { + (#key, $($e:tt)*) => { + compile_error!(stringify!(Please provide: #(#vars_ident),* >> The order matters!)); + }; + }); + } else { + branches.push(quote! { + (#key, $lang:expr) => { + match $lang.as_ref() { + #(#langs)* + e => panic!("Missing language: {}", e) + } + }; + }); + } + } + + quote! { + #![cfg_attr(target_os = "none", no_std)] + #[macro_export] + macro_rules! t { + #(#branches)* + ($key:expr, $lang:expr) => { + compile_error!("Missing translation"); + } + } + } +} + +fn write_code(code: TokenStream) { + let mut dest = project_root(); + dest.push("locales"); + dest.push("src"); + let mut output = File::create(&std::path::Path::new(&dest).join("lib.rs")).unwrap(); + output + .write(code.to_string().as_bytes()) + .expect("Cannot write generated i18n code"); +} + +pub fn generate_locales() { + let translations = read_locales(); + let code = generate_code(translations); + //println!("{}", &code); + write_code(code); +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 94d198413..0983819d3 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -5,6 +5,9 @@ use std::{ process::Command, }; +mod generate_locales; +use generate_locales::*; + type DynError = Box; const TARGET: &str = "riscv32imac-unknown-none-elf"; @@ -164,6 +167,7 @@ fn try_main() -> Result<(), DynError> { Some("burn-kernel") => update_usb(true, false, false)?, Some("burn-loader") => update_usb(false, true, false)?, Some("burn-soc") => update_usb(false, false, true)?, + Some("generate-locales") => generate_locales(), _ => print_help(), } Ok(()) @@ -190,6 +194,7 @@ sr-test [soc.svd] builds the suspend/resume testing image burn-kernel invoke the `usb_update.py` utility to burn the kernel burn-loader invoke the `usb_update.py` utility to burn the loader burn-soc invoke the `usb_update.py` utility to burn the SoC gateware +generate-locales only generate the locales include for the language selected in xous-rs/src/definitions::LANG Please refer to tools/README_UPDATE.md for instructions on how to set up `usb_update.py` " @@ -260,6 +265,8 @@ fn build_hw_image( kkey: Option, extra_args: Option<&[&str]>, ) -> Result<(), DynError> { + generate_locales(); + let svd_file = match svd { Some(s) => s, None => return Err("svd file not specified".into()), @@ -436,6 +443,8 @@ fn renode_image(debug: bool, packages: &[&str]) -> Result<(), DynError> { } fn run(debug: bool, init: &[&str]) -> Result<(), DynError> { + generate_locales(); + let stream = if debug { "debug" } else { "release" }; build(&init, debug, None, None, None)?;