doukutsu-rs/src/i18n.rs

70 lines
2.1 KiB
Rust
Raw Normal View History

2022-03-15 01:54:03 +00:00
use crate::framework::context::Context;
use crate::framework::filesystem;
use crate::shared_game_state::FontData;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Locale {
2022-08-26 00:17:45 +00:00
pub code: String,
pub name: String,
2022-03-15 01:54:03 +00:00
pub font: FontData,
2022-08-26 00:17:45 +00:00
strings: HashMap<String, String>,
2022-03-15 01:54:03 +00:00
}
impl Locale {
2022-08-26 00:17:45 +00:00
pub fn new(ctx: &mut Context, base_paths: &Vec<String>, code: &str) -> Locale {
let file = filesystem::open_find(ctx, base_paths, &format!("locale/{}.json", code)).unwrap();
2022-03-15 01:54:03 +00:00
let json: serde_json::Value = serde_json::from_reader(file).unwrap();
let strings = Locale::flatten(&json);
2022-08-26 00:17:45 +00:00
let name = strings["name"].clone();
let font_name = strings["font"].clone();
let font_scale = strings["font_scale"].parse::<f32>().unwrap_or(1.0);
let font = FontData::new(font_name, font_scale, 0.0);
Locale { code: code.to_string(), name, font, strings }
2022-03-15 01:54:03 +00:00
}
fn flatten(json: &serde_json::Value) -> HashMap<String, String> {
let mut strings = HashMap::new();
for (key, value) in json.as_object().unwrap() {
match value {
serde_json::Value::String(string) => {
strings.insert(key.to_owned(), string.to_owned());
}
serde_json::Value::Object(_) => {
let substrings = Locale::flatten(value);
for (sub_key, sub_value) in substrings.iter() {
strings.insert(format!("{}.{}", key, sub_key), sub_value.to_owned());
}
}
_ => {}
}
}
strings
}
pub fn t(&self, key: &str) -> String {
self.strings.get(key).unwrap_or(&key.to_owned()).to_owned()
}
2022-09-17 10:57:37 +00:00
pub fn tt(&self, key: &str, args: &[(&str, &str)]) -> String {
2022-03-15 01:54:03 +00:00
let mut string = self.t(key);
for (key, value) in args.iter() {
string = string.replace(&format!("{{{}}}", key), &value);
}
string
}
2022-08-26 00:17:45 +00:00
pub fn set_font(&mut self, font: FontData) {
self.font = font;
}
2022-03-15 01:54:03 +00:00
}