1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// use log::{error, info};
// use rust_i18n::Backend;
// use std::{collections::HashMap, env, fs, path::PathBuf};
// use glob::glob;
// pub struct I18nBackend {
// trs: HashMap<String, HashMap<String, String>>,
// }
// impl I18nBackend {
// pub fn new() -> Self {
// let mut backend = Self {
// trs: HashMap::new(),
// };
// backend.load_metas();
// backend
// }
// fn load_metas(&mut self) -> Option<()> {
// let meta_dir = self.get_meta_dir()?;
// info!("load meta from dir: {:?}", meta_dir);
// let pattern = format!("{}/**/*.yaml", meta_dir.display());
// for entry in glob(&pattern).expect("Failed to read glob pattern") {
// match entry {
// Ok(path) => {
// let locale = path.file_stem()?.to_str()?.to_lowercase();
// let content = fs::read_to_string(&path).ok()?;
// let yaml: HashMap<String, String> = serde_yml::from_str(&content).ok()?;
// self.trs
// .entry(locale.to_string())
// .or_insert_with(HashMap::new)
// .extend(yaml);
// }
// Err(e) => {
// error!("Error reading path: {:?}", e);
// }
// }
// }
// Some(())
// }
// fn get_meta_dir(&self) -> Option<PathBuf> {
// let exe_path = env::current_exe().ok()?;
// let mut current_dir = exe_path.parent()?.to_path_buf();
// loop {
// let potential = current_dir.join("resources/meta");
// info!("try location meta dir: {:?} ...", potential);
// if potential.is_dir() {
// return Some(potential);
// }
// match current_dir.parent() {
// Some(parent) => current_dir = parent.to_path_buf(),
// None => break,
// }
// }
// None
// }
// }
// impl Backend for I18nBackend {
// fn available_locales(&self) -> Vec<&str> {
// return self.trs.keys().map(|k| k.as_str()).collect();
// }
// fn translate(&self, locale: &str, key: &str) -> Option<&str> {
// let locale = locale.to_lowercase();
// // Write your own lookup logic here.
// // For example load from database
// return self.trs.get(&locale)?.get(key).map(|k| k.as_str());
// }
// }