186 lines
7.2 KiB
Rust
186 lines
7.2 KiB
Rust
//! CAI-Watchdog localization module
|
|
//! Author: Alexander I. Chebykin (CAI) <alex.chebykin@gmail.com>
|
|
//!
|
|
//! External localizations stored in /locales/ directory
|
|
|
|
use std::borrow::Borrow;
|
|
use std::path::Path;
|
|
|
|
use crate::mod_fs;
|
|
use sys_locale::get_locale;
|
|
|
|
pub struct Lang {
|
|
pub cant_find_config_file: String,
|
|
pub check_failed: String,
|
|
pub check_is_ok: String,
|
|
pub configuration_file: String,
|
|
pub error: String,
|
|
pub execute: String,
|
|
pub exiting: String,
|
|
pub failed_to_execute_process: String,
|
|
pub is_not_running: String,
|
|
pub is_not_running_now: String,
|
|
pub is_offline: String,
|
|
pub is_offline_now: String,
|
|
pub is_online: String,
|
|
pub is_online_now: String,
|
|
pub is_running: String,
|
|
pub is_running_now: String,
|
|
pub process: String,
|
|
pub service: String,
|
|
pub service_started: String,
|
|
pub state_changed_to_false: String,
|
|
pub state_changed_to_true: String,
|
|
pub this_help_message: String,
|
|
pub usage: String,
|
|
pub ver: String,
|
|
pub version_info: String
|
|
|
|
}
|
|
|
|
pub struct Locale {
|
|
pub locale:Lang,
|
|
}
|
|
|
|
impl Locale {
|
|
pub fn new() -> Self {
|
|
let mut l:Lang = Lang{
|
|
cant_find_config_file: "Can't find configuration file".to_string(),
|
|
check_failed: "check failed".to_string(),
|
|
check_is_ok: "check is ok".to_string(),
|
|
configuration_file: "Configuration file".to_string(),
|
|
error: "Error".to_string(),
|
|
execute: "Execute".to_string(),
|
|
exiting: "Exiting...".to_string(),
|
|
failed_to_execute_process: "Can't execute process".to_string(),
|
|
is_not_running: "is not running".to_string(),
|
|
is_not_running_now: "is not running now".to_string(),
|
|
is_offline: "is offline".to_string(),
|
|
is_offline_now: "is offline now".to_string(),
|
|
is_online: "is online".to_string(),
|
|
is_online_now: "is online now".to_string(),
|
|
is_running: "is running".to_string(),
|
|
is_running_now: "is running now".to_string(),
|
|
process: "Process".to_string(),
|
|
service: "Service".to_string(),
|
|
service_started: "Service started".to_string(),
|
|
state_changed_to_false: "state changed to false".to_string(),
|
|
state_changed_to_true: "state changed to true".to_string(),
|
|
this_help_message: "This help message".to_string(),
|
|
usage: "Usage".to_string(),
|
|
ver: "ver".to_string(),
|
|
version_info: "Version info".to_string()
|
|
};
|
|
|
|
let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
|
|
|
|
let mut locale_file = mod_fs::get_exe_path();
|
|
|
|
locale_file = if cfg!(windows) {
|
|
format!("{}\\locales\\{}.conf", locale_file, locale)
|
|
} else {
|
|
format!("/etc/cai-watchdog/locales/{}.conf", locale)
|
|
};
|
|
|
|
if Path::new(&locale_file).exists() {
|
|
let cfg = ini!(&locale_file);
|
|
|
|
if cfg["rules"].contains_key("cant_find_config_file") {
|
|
l.cant_find_config_file = cfg["rules"]["cant_find_config_file"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("check_failed") {
|
|
l.check_failed = cfg["rules"]["check_failed"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("check_is_ok") {
|
|
l.check_is_ok = cfg["rules"]["check_is_ok"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("configuration_file") {
|
|
l.configuration_file = cfg["rules"]["configuration_file"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("error") {
|
|
l.error = cfg["rules"]["error"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("execute") {
|
|
l.execute = cfg["rules"]["execute"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("exiting") {
|
|
l.exiting = cfg["rules"]["exiting"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("failed_to_execute_process") {
|
|
l.failed_to_execute_process = cfg["rules"]["failed_to_execute_process"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_not_running") {
|
|
l.is_not_running = cfg["rules"]["is_not_running"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_not_running_now") {
|
|
l.is_not_running_now = cfg["rules"]["is_not_running_now"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_offline") {
|
|
l.is_offline = cfg["rules"]["is_offline"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_offline_now") {
|
|
l.is_offline_now = cfg["rules"]["is_offline_now"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_online") {
|
|
l.is_online = cfg["rules"]["is_online"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_online_now") {
|
|
l.is_online_now = cfg["rules"]["is_online_now"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_running") {
|
|
l.is_running = cfg["rules"]["is_running"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("is_running_now") {
|
|
l.is_running_now = cfg["rules"]["is_running_now"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("process") {
|
|
l.process = cfg["rules"]["process"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("service") {
|
|
l.service = cfg["rules"]["service"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("service_started") {
|
|
l.service_started = cfg["rules"]["service_started"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("state_changed_to_false") {
|
|
l.state_changed_to_false = cfg["rules"]["state_changed_to_false"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("state_changed_to_true") {
|
|
l.state_changed_to_true = cfg["rules"]["state_changed_to_true"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("this_help_message") {
|
|
l.this_help_message = cfg["rules"]["this_help_message"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("usage") {
|
|
l.usage = cfg["rules"]["usage"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("ver") {
|
|
l.ver = cfg["rules"]["ver"].clone().unwrap();
|
|
}
|
|
if cfg["rules"].contains_key("version_info") {
|
|
l.version_info = cfg["rules"]["version_info"].clone().unwrap();
|
|
}
|
|
}
|
|
|
|
Self {
|
|
locale: l
|
|
}
|
|
}
|
|
|
|
/// Return object with locale data
|
|
///
|
|
/// If there is no localization file for current system locale, fallback (English) locale will be returned
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```
|
|
/// let locale = mod_locales::Locale::new();
|
|
///
|
|
/// println!("\u{26a0} {}! {}.", locale.t().error, locale.t().cant_find_config_file);
|
|
/// println!("{}", locale.t().exiting);
|
|
/// ```
|
|
pub fn t(&self) -> &Lang {
|
|
return self.locale.borrow().clone();
|
|
}
|
|
}
|