mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
add static localization
This commit is contained in:
parent
556f0aaea5
commit
2b57e1c4f8
7 changed files with 200 additions and 1 deletions
9
locales/Cargo.toml
Normal file
9
locales/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
authors = ["bunnie <bunnie@kosagi.com>"]
|
||||||
|
description = "Localization string database (as static macro) for Xous"
|
||||||
|
edition = "2018"
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
name = "locales"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
1
locales/src/lib.rs
Executable file
1
locales/src/lib.rs
Executable file
|
|
@ -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") ; } }
|
||||||
22
services/gam/locales/i18n.json
Normal file
22
services/gam/locales/i18n.json
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
use core::convert::{TryFrom, TryInto};
|
use core::convert::{TryFrom, TryInto};
|
||||||
use core::num::{NonZeroU8, NonZeroUsize};
|
use core::num::{NonZeroU8, NonZeroUsize};
|
||||||
|
|
||||||
|
pub const LANG: &'static str = "zh";
|
||||||
|
|
||||||
pub type MemoryAddress = NonZeroUsize;
|
pub type MemoryAddress = NonZeroUsize;
|
||||||
pub type MemorySize = NonZeroUsize;
|
pub type MemorySize = NonZeroUsize;
|
||||||
pub type StackPointer = usize;
|
pub type StackPointer = usize;
|
||||||
|
|
|
||||||
|
|
@ -8,4 +8,12 @@ edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
#"ed25519-dalek" = "1"
|
#"ed25519-dalek" = "1"
|
||||||
#"pem" = "0.8"
|
#"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"
|
||||||
148
xtask/src/generate_locales.rs
Executable file
148
xtask/src/generate_locales.rs
Executable file
|
|
@ -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<Key, HashMap<Locale, Value>>;
|
||||||
|
|
||||||
|
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<String, HashMap<String, String>> =
|
||||||
|
serde_json::from_str(&content).expect("Cannot parse locale file");
|
||||||
|
translations.extend(res);
|
||||||
|
}
|
||||||
|
translations
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_vars(tr: &str) -> Vec<String> {
|
||||||
|
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::<Vec<String>>();
|
||||||
|
a.sort();
|
||||||
|
|
||||||
|
//println!("-----\n{:?}\n-----", &a);
|
||||||
|
a
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_vars_to_idents(vars: &Vec<String>) -> Vec<Ident> {
|
||||||
|
vars.iter()
|
||||||
|
.map(|var| Ident::new(&var[1..], Span::call_site()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_code(translations: Translations) -> proc_macro2::TokenStream {
|
||||||
|
let mut branches = Vec::<TokenStream>::new();
|
||||||
|
|
||||||
|
for (key, trs) in translations {
|
||||||
|
let mut langs = Vec::<TokenStream>::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);
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,9 @@ use std::{
|
||||||
process::Command,
|
process::Command,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mod generate_locales;
|
||||||
|
use generate_locales::*;
|
||||||
|
|
||||||
type DynError = Box<dyn std::error::Error>;
|
type DynError = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
const TARGET: &str = "riscv32imac-unknown-none-elf";
|
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-kernel") => update_usb(true, false, false)?,
|
||||||
Some("burn-loader") => update_usb(false, true, false)?,
|
Some("burn-loader") => update_usb(false, true, false)?,
|
||||||
Some("burn-soc") => update_usb(false, false, true)?,
|
Some("burn-soc") => update_usb(false, false, true)?,
|
||||||
|
Some("generate-locales") => generate_locales(),
|
||||||
_ => print_help(),
|
_ => print_help(),
|
||||||
}
|
}
|
||||||
Ok(())
|
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-kernel invoke the `usb_update.py` utility to burn the kernel
|
||||||
burn-loader invoke the `usb_update.py` utility to burn the loader
|
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
|
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`
|
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<String>,
|
kkey: Option<String>,
|
||||||
extra_args: Option<&[&str]>,
|
extra_args: Option<&[&str]>,
|
||||||
) -> Result<(), DynError> {
|
) -> Result<(), DynError> {
|
||||||
|
generate_locales();
|
||||||
|
|
||||||
let svd_file = match svd {
|
let svd_file = match svd {
|
||||||
Some(s) => s,
|
Some(s) => s,
|
||||||
None => return Err("svd file not specified".into()),
|
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> {
|
fn run(debug: bool, init: &[&str]) -> Result<(), DynError> {
|
||||||
|
generate_locales();
|
||||||
|
|
||||||
let stream = if debug { "debug" } else { "release" };
|
let stream = if debug { "debug" } else { "release" };
|
||||||
|
|
||||||
build(&init, debug, None, None, None)?;
|
build(&init, debug, None, None, None)?;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue