2022-11-19 17:20:03 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-03-15 01:54:03 +00:00
|
|
|
use crate::framework::context::Context;
|
|
|
|
use crate::framework::filesystem;
|
2022-11-19 17:20:03 +00:00
|
|
|
use crate::game::shared_game_state::FontData;
|
2022-03-15 01:54:03 +00:00
|
|
|
|
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2022-11-20 19:38:36 +00:00
|
|
|
impl Default for Locale {
|
|
|
|
fn default() -> Self {
|
|
|
|
Locale {
|
|
|
|
code: "en".to_owned(),
|
|
|
|
name: "English".to_owned(),
|
|
|
|
font: FontData {
|
|
|
|
path: String::new(),
|
|
|
|
scale: 1.0,
|
|
|
|
space_offset: 0.0
|
|
|
|
},
|
|
|
|
strings: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-20 19:38:36 +00:00
|
|
|
pub fn t<'a: 'b, 'b>(&'a self, key: &'b str) -> &'b str {
|
|
|
|
if let Some(str) = self.strings.get(key) {
|
|
|
|
str
|
|
|
|
} else {
|
|
|
|
key
|
|
|
|
}
|
2022-03-15 01:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 10:57:37 +00:00
|
|
|
pub fn tt(&self, key: &str, args: &[(&str, &str)]) -> String {
|
2022-11-20 19:38:36 +00:00
|
|
|
let mut string = self.t(key).to_owned();
|
2022-03-15 01:54:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|