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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
use std::{
collections::{btree_map::Entry, BTreeMap},
fs::{read_dir, read_to_string},
sync::Arc,
};
use crate::fnv1a_64;
use super::log::Log;
/// Describes a language element
///
/// # Values
///
/// * `id: i64` - Language ID.
/// * `lang: String` - Languane name ISO 639-1: uk - ukrainian, en - english, en - english.
/// * `code: String` - Languane code ISO 3166 alpha-2: ua - Ukraine, us - USA, gb - United Kingdom.
/// * `name: String` - Native name of the language.
#[derive(Debug, Clone)]
pub struct LangItem {
/// Language ID
pub id: i64,
/// Languane name ISO 639-1: uk - ukrainian, en - english, en - english
pub lang: String,
/// Languane code ISO 3166 alpha-2: ua - Ukraine, us - USA, gb - United Kingdom
pub code: String,
/// Native name of the language
pub name: String,
}
/// I18n
///
/// # Index
///
/// * 1 - language ID
/// * 2 - Module ID
/// * 3 - Class ID
/// * 4 - Key ID
/// * 5 - Key value
type LangList = BTreeMap<i64, BTreeMap<i64, BTreeMap<i64, Arc<BTreeMap<i64, String>>>>>;
/// Descrives all languages
///
/// # Values
///
/// * `langs: Vec<LangItem>` - List of languages
/// * `list: LangList` - List of translations
/// * `default: usize` - Default language
#[derive(Debug)]
pub struct Lang {
/// List of languages
pub langs: Vec<LangItem>,
/// List of translations
pub list: LangList,
/// Default language
pub default: usize,
}
impl Lang {
/// Reads ./app/ and recognizes translations
///
/// # Description
///
/// In the root directory of the project (`Init::root_path`) the `app` directory is searched.
///
/// Translation files are logically located in this directory.
/// Each file must be named `LangItem::lang` and have the extension `.lang`
///
/// ## Example:
///
/// * English: ./app/module_name/class_name/en.lang
/// * Ukrainian: ./app/module_name/class_name/uk.lang
///
/// module_name - Name of the module <br />
/// class_name - Class name
///
/// For all controllers in the same class - one translation file in one language is used.
///
/// Each translation file is divided into lines.
/// Each line consists of a key and a translation.
///
/// ## Example:
///
/// `en.lang`<br />
/// about=About<br />
/// articles=Articles<br />
/// article=Article<br />
/// contact=Contact<br />
/// terms=Terms Conditions<br />
/// policy=Privacy Policy<br />
///
/// ## Use in the controller:
///
/// To get a translation, it is enough to set the `this.lang("contact")` function,
/// which will return the corresponding translation.<br />
/// If no translation is found, the key will be returned.
pub async fn new(root: &str, default_lang: &str, langs: Vec<LangItem>) -> Lang {
if langs.is_empty() {
Log::warning(1151, None);
return Lang {
langs: Vec::new(),
list: BTreeMap::new(),
default: 0,
};
}
let mut codes = BTreeMap::new();
let mut default = 0;
for item in &langs {
codes.insert(item.code.clone(), item.id);
if item.code == default_lang {
default = item.id as usize;
}
}
let path = format!("{}/app/", root);
let read_path = match read_dir(&path) {
Ok(r) => r,
Err(e) => {
Log::warning(1100, Some(e.to_string()));
return Lang {
langs,
list: BTreeMap::new(),
default,
};
}
};
// Read first level dir
let mut list = BTreeMap::new();
for entry in read_path {
let path = match entry {
Ok(e) => e.path(),
Err(e) => {
Log::warning(1101, Some(format!("{} ({})", e, path)));
continue;
}
};
if !path.is_dir() {
continue;
}
let module = match path.file_name() {
Some(m) => match m.to_str() {
Some(module) => module,
None => continue,
},
None => continue,
};
let read_path = match read_dir(&path) {
Ok(r) => r,
Err(e) => {
Log::warning(1102, Some(format!("{} ({})", e, path.display())));
continue;
}
};
// Read second level dir
for entry in read_path {
let path = match entry {
Ok(e) => e.path(),
Err(e) => {
Log::warning(1101, Some(format!("{} ({})", e, path.display())));
continue;
}
};
if !path.is_dir() {
continue;
}
let class = match path.file_name() {
Some(c) => match c.to_str() {
Some(class) => class,
None => continue,
},
None => continue,
};
let read_path = match read_dir(&path) {
Ok(r) => r,
Err(e) => {
Log::warning(1102, Some(format!("{} ({})", e, path.display())));
continue;
}
};
// Read third level dir
for entry in read_path {
let path = match entry {
Ok(e) => e.path(),
Err(e) => {
Log::warning(1101, Some(format!("{} ({})", e, path.display())));
continue;
}
};
if !path.is_file() {
continue;
}
let code = match path.file_name() {
Some(v) => match v.to_str() {
Some(view) => view,
None => continue,
},
None => continue,
};
if code.ends_with(".lang") && code.len() == 7 {
let code = &code[0..2];
if let Some(id) = codes.get(code) {
if let Ok(text) = read_to_string(&path) {
if !text.is_empty() {
for part in text.split('\n') {
let line = part.trim();
if !line.is_empty() && line.contains('=') {
let vals: Vec<&str> = line.splitn(2, '=').collect();
if vals.len() == 2 {
let key = vals[0].trim();
let val = vals[1].trim();
// lang_id
let l1 = match list.entry(*id) {
Entry::Vacant(v) => v.insert(BTreeMap::new()),
Entry::Occupied(o) => o.into_mut(),
};
// module
let l2 = match l1.entry(fnv1a_64(module)) {
Entry::Vacant(v) => v.insert(BTreeMap::new()),
Entry::Occupied(o) => o.into_mut(),
};
// class
let l3 = match l2.entry(fnv1a_64(class)) {
Entry::Vacant(v) => v.insert(BTreeMap::new()),
Entry::Occupied(o) => o.into_mut(),
};
l3.insert(fnv1a_64(key), val.to_owned());
}
}
}
}
}
}
}
}
}
}
// Add Arc to async operation
let mut list_lang = BTreeMap::new();
for (key_lang, item_lang) in list {
let mut list_module = BTreeMap::new();
for (key_module, item_module) in item_lang {
let mut list_class = BTreeMap::new();
for (key_class, item_class) in item_module {
list_class.insert(key_class, Arc::new(item_class));
}
list_module.insert(key_module, list_class);
}
list_lang.insert(key_lang, list_module);
}
Lang {
langs,
list: list_lang,
default,
}
}
}